Skip to content

Commit fd07c3f

Browse files
committed
Account for #![feature(const_trait_impl)] state
1 parent 90ff38b commit fd07c3f

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ fn build_error_for_const_call<'tcx>(
393393
`{trait_name}` is not const",
394394
),
395395
);
396-
if parent.is_local() {
396+
if parent.is_local() && ccx.tcx.sess.is_nightly_build() {
397+
if !ccx.tcx.features().const_trait_impl() {
398+
err.help(
399+
"add `#![feature(const_trait_impl)]` to the crate attributes to \
400+
enable `#[const_trait]`",
401+
);
402+
}
397403
let indentation = ccx
398404
.tcx
399405
.sess
@@ -406,6 +412,8 @@ fn build_error_for_const_call<'tcx>(
406412
format!("#[const_trait]\n{indentation}"),
407413
Applicability::MachineApplicable,
408414
);
415+
} else if !ccx.tcx.sess.is_nightly_build() {
416+
err.help("const traits are not yet supported on stable Rust");
409417
}
410418
}
411419
} else if ccx.tcx.constness(callee) != hir::Constness::Const {

tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,24 @@ LL | #[const_trait] trait Bar: ~const Foo {}
5555
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
5656
--> const-super-trait.rs:10:7
5757
|
58+
LL | const fn foo<T: ~const Bar>(x: &T) {
59+
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
5860
LL | x.a();
5961
| ^^^
6062
|
61-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
63+
note: method `a` is not const because trait `Foo` is not const
64+
--> const-super-trait.rs:3:1
65+
|
66+
LL | trait Foo {
67+
| ^^^^^^^^^ this trait is not const
68+
LL | fn a(&self);
69+
| ------------ this method is not const
70+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
71+
help: consider making trait `Foo` const
72+
|
73+
LL + #[const_trait]
74+
LL | trait Foo {
75+
|
6276

6377
error: aborting due to 6 previous errors
6478

tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@ LL | #[const_trait] trait Bar: ~const Foo {}
3535
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
3636
--> const-super-trait.rs:10:7
3737
|
38+
LL | const fn foo<T: ~const Bar>(x: &T) {
39+
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
3840
LL | x.a();
3941
| ^^^
4042
|
41-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
43+
note: method `a` is not const because trait `Foo` is not const
44+
--> const-super-trait.rs:3:1
45+
|
46+
LL | trait Foo {
47+
| ^^^^^^^^^ this trait is not const
48+
LL | fn a(&self);
49+
| ------------ this method is not const
50+
help: consider making trait `Foo` const
51+
|
52+
LL + #[const_trait]
53+
LL | trait Foo {
54+
|
4255

4356
error: aborting due to 4 previous errors
4457

tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ note: `Bar` can't be used with `[const]` because it isn't `const`
5353
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
5454
--> const-super-trait.rs:10:7
5555
|
56+
9 | const fn foo<T: ~const Bar>(x: &T) {
57+
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
5658
10 | x.a();
5759
| ^^^
5860
|
59-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
61+
note: method `a` is not const because trait `Foo` is not const
62+
--> const-super-trait.rs:3:1
63+
|
64+
3 | trait Foo {
65+
| ^^^^^^^^^ this trait is not const
66+
4 | fn a(&self);
67+
| ------------ this method is not const
68+
= help: const traits are not yet supported on stable Rust
6069

6170
error: aborting due to 6 previous errors
6271

tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ note: `Bar` can't be used with `[const]` because it isn't `const`
4343
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
4444
--> const-super-trait.rs:10:7
4545
|
46+
9 | const fn foo<T: ~const Bar>(x: &T) {
47+
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
4648
10 | x.a();
4749
| ^^^
4850
|
49-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
51+
note: method `a` is not const because trait `Foo` is not const
52+
--> const-super-trait.rs:3:1
53+
|
54+
3 | trait Foo {
55+
| ^^^^^^^^^ this trait is not const
56+
4 | fn a(&self);
57+
| ------------ this method is not const
58+
= help: const traits are not yet supported on stable Rust
5059

5160
error: aborting due to 5 previous errors
5261

tests/ui/resolve/issue-39559-2.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | trait Dim {
1111
| ^^^^^^^^^ this trait is not const
1212
LL | fn dim() -> usize;
1313
| ------------------ this associated function is not const
14+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
1415
help: consider making trait `Dim` const
1516
|
1617
LL + #[const_trait]
@@ -30,6 +31,7 @@ LL | trait Dim {
3031
| ^^^^^^^^^ this trait is not const
3132
LL | fn dim() -> usize;
3233
| ------------------ this associated function is not const
34+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
3335
help: consider making trait `Dim` const
3436
|
3537
LL + #[const_trait]

tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ LL | trait Foo {
104104
| ^^^^^^^^^ this trait is not const
105105
LL | fn a(&self);
106106
| ------------ this method is not const
107+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
107108
help: consider making trait `Foo` const
108109
|
109110
LL + #[const_trait]

tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ LL | trait Foo {
104104
| ^^^^^^^^^ this trait is not const
105105
LL | fn a(&self);
106106
| ------------ this method is not const
107+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
107108
help: consider making trait `Foo` const
108109
|
109110
LL + #[const_trait]

0 commit comments

Comments
 (0)