Skip to content

Commit 185440a

Browse files
authored
Rollup merge of #134094 - estebank:const-trait-errors, r=compiler-errors
Tweak wording of non-const traits used as const bounds Use verbose suggestions and add additional labels/notes. r? ``@compiler-errors``
2 parents 0064e73 + 4007fc9 commit 185440a

File tree

49 files changed

+1122
-195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1122
-195
lines changed

compiler/rustc_hir_analysis/messages.ftl

+8-6
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ hir_analysis_coercion_between_struct_same_note = expected coercion between the s
9696
9797
hir_analysis_coercion_between_struct_single_note = expected a single field to be coerced, none found
9898
99-
hir_analysis_const_bound_for_non_const_trait =
100-
`{$modifier}` can only be applied to `#[const_trait]` traits
101-
102-
hir_analysis_const_impl_for_non_const_trait =
103-
const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
104-
.suggestion = mark `{$trait_name}` as const
99+
hir_analysis_const_bound_for_non_const_trait = `{$modifier}` can only be applied to `#[const_trait]` traits
100+
.label = can't be applied to `{$trait_name}`
101+
.note = `{$trait_name}` can't be used with `{$modifier}` because it isn't annotated with `#[const_trait]`
102+
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
103+
104+
hir_analysis_const_impl_for_non_const_trait = const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
105+
.label = this trait is not `const`
106+
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
105107
.note = marking a trait with `#[const_trait]` ensures all default method bodies are `const`
106108
.adding = adding a non-const method body in the future would be a breaking change
107109

compiler/rustc_hir_analysis/src/collect.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1638,11 +1638,23 @@ fn check_impl_constness(
16381638
}
16391639

16401640
let trait_name = tcx.item_name(trait_def_id).to_string();
1641+
let (local_trait_span, suggestion_pre) =
1642+
match (trait_def_id.is_local(), tcx.sess.is_nightly_build()) {
1643+
(true, true) => (
1644+
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
1645+
if tcx.features().const_trait_impl() {
1646+
""
1647+
} else {
1648+
"enable `#![feature(const_trait_impl)]` in your crate and "
1649+
},
1650+
),
1651+
(false, _) | (_, false) => (None, ""),
1652+
};
16411653
Some(tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
16421654
trait_ref_span: hir_trait_ref.path.span,
16431655
trait_name,
1644-
local_trait_span:
1645-
trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
1656+
local_trait_span,
1657+
suggestion_pre,
16461658
marking: (),
16471659
adding: (),
16481660
}))

compiler/rustc_hir_analysis/src/errors.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,16 @@ pub(crate) struct GenericArgsOnOverriddenImpl {
530530
#[diag(hir_analysis_const_impl_for_non_const_trait)]
531531
pub(crate) struct ConstImplForNonConstTrait {
532532
#[primary_span]
533+
#[label]
533534
pub trait_ref_span: Span,
534535
pub trait_name: String,
535-
#[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
536+
#[suggestion(
537+
applicability = "machine-applicable",
538+
code = "#[const_trait] ",
539+
style = "verbose"
540+
)]
536541
pub local_trait_span: Option<Span>,
542+
pub suggestion_pre: &'static str,
537543
#[note]
538544
pub marking: (),
539545
#[note(hir_analysis_adding)]
@@ -544,8 +550,19 @@ pub(crate) struct ConstImplForNonConstTrait {
544550
#[diag(hir_analysis_const_bound_for_non_const_trait)]
545551
pub(crate) struct ConstBoundForNonConstTrait {
546552
#[primary_span]
553+
#[label]
547554
pub span: Span,
548555
pub modifier: &'static str,
556+
#[note]
557+
pub def_span: Option<Span>,
558+
pub suggestion_pre: &'static str,
559+
#[suggestion(
560+
applicability = "machine-applicable",
561+
code = "#[const_trait] ",
562+
style = "verbose"
563+
)]
564+
pub suggestion: Option<Span>,
565+
pub trait_name: String,
549566
}
550567

551568
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,26 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
737737
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
738738
&& !self.tcx().is_const_trait(trait_def_id)
739739
{
740+
let (def_span, suggestion, suggestion_pre) =
741+
match (trait_def_id.is_local(), self.tcx().sess.is_nightly_build()) {
742+
(true, true) => (
743+
None,
744+
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
745+
if self.tcx().features().const_trait_impl() {
746+
""
747+
} else {
748+
"enable `#![feature(const_trait_impl)]` in your crate and "
749+
},
750+
),
751+
(false, _) | (_, false) => (Some(tcx.def_span(trait_def_id)), None, ""),
752+
};
740753
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
741754
span,
742755
modifier: constness.as_str(),
756+
def_span,
757+
trait_name: self.tcx().def_path_str(trait_def_id),
758+
suggestion_pre,
759+
suggestion,
743760
});
744761
}
745762

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error: `~const` is not allowed here
2+
--> const-super-trait.rs:7:12
3+
|
4+
LL | trait Bar: ~const Foo {}
5+
| ^^^^^^
6+
|
7+
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
8+
--> const-super-trait.rs:7:1
9+
|
10+
LL | trait Bar: ~const Foo {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0658]: const trait impls are experimental
14+
--> const-super-trait.rs:7:12
15+
|
16+
LL | trait Bar: ~const Foo {}
17+
| ^^^^^^
18+
|
19+
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
20+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
21+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
22+
23+
error[E0658]: const trait impls are experimental
24+
--> const-super-trait.rs:9:17
25+
|
26+
LL | const fn foo<T: ~const Bar>(x: &T) {
27+
| ^^^^^^
28+
|
29+
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
30+
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
31+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
32+
33+
error: `~const` can only be applied to `#[const_trait]` traits
34+
--> const-super-trait.rs:7:12
35+
|
36+
LL | trait Bar: ~const Foo {}
37+
| ^^^^^^ can't be applied to `Foo`
38+
|
39+
help: enable `#![feature(const_trait_impl)]` in your crate and mark `Foo` as `#[const_trait]` to allow it to have `const` implementations
40+
|
41+
LL | #[const_trait] trait Foo {
42+
| ++++++++++++++
43+
44+
error: `~const` can only be applied to `#[const_trait]` traits
45+
--> const-super-trait.rs:9:17
46+
|
47+
LL | const fn foo<T: ~const Bar>(x: &T) {
48+
| ^^^^^^ can't be applied to `Bar`
49+
|
50+
help: enable `#![feature(const_trait_impl)]` in your crate and mark `Bar` as `#[const_trait]` to allow it to have `const` implementations
51+
|
52+
LL | #[const_trait] trait Bar: ~const Foo {}
53+
| ++++++++++++++
54+
55+
error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
56+
--> const-super-trait.rs:10:7
57+
|
58+
LL | x.a();
59+
| ^^^
60+
|
61+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
62+
63+
error: aborting due to 6 previous errors
64+
65+
Some errors have detailed explanations: E0015, E0658.
66+
For more information about an error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: `~const` is not allowed here
2+
--> const-super-trait.rs:7:12
3+
|
4+
LL | trait Bar: ~const Foo {}
5+
| ^^^^^^
6+
|
7+
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
8+
--> const-super-trait.rs:7:1
9+
|
10+
LL | trait Bar: ~const Foo {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: `~const` can only be applied to `#[const_trait]` traits
14+
--> const-super-trait.rs:7:12
15+
|
16+
LL | trait Bar: ~const Foo {}
17+
| ^^^^^^ can't be applied to `Foo`
18+
|
19+
help: mark `Foo` as `#[const_trait]` to allow it to have `const` implementations
20+
|
21+
LL | #[const_trait] trait Foo {
22+
| ++++++++++++++
23+
24+
error: `~const` can only be applied to `#[const_trait]` traits
25+
--> const-super-trait.rs:9:17
26+
|
27+
LL | const fn foo<T: ~const Bar>(x: &T) {
28+
| ^^^^^^ can't be applied to `Bar`
29+
|
30+
help: mark `Bar` as `#[const_trait]` to allow it to have `const` implementations
31+
|
32+
LL | #[const_trait] trait Bar: ~const Foo {}
33+
| ++++++++++++++
34+
35+
error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
36+
--> const-super-trait.rs:10:7
37+
|
38+
LL | x.a();
39+
| ^^^
40+
|
41+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
42+
43+
error: aborting due to 4 previous errors
44+
45+
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error: `~const` is not allowed here
2+
--> const-super-trait.rs:7:12
3+
|
4+
7 | trait Bar: ~const Foo {}
5+
| ^^^^^^
6+
|
7+
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
8+
--> const-super-trait.rs:7:1
9+
|
10+
7 | trait Bar: ~const Foo {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0658]: const trait impls are experimental
14+
--> const-super-trait.rs:7:12
15+
|
16+
7 | trait Bar: ~const Foo {}
17+
| ^^^^^^
18+
|
19+
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
20+
21+
error[E0658]: const trait impls are experimental
22+
--> const-super-trait.rs:9:17
23+
|
24+
9 | const fn foo<T: ~const Bar>(x: &T) {
25+
| ^^^^^^
26+
|
27+
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
28+
29+
error: `~const` can only be applied to `#[const_trait]` traits
30+
--> const-super-trait.rs:7:12
31+
|
32+
7 | trait Bar: ~const Foo {}
33+
| ^^^^^^ can't be applied to `Foo`
34+
|
35+
note: `Foo` can't be used with `~const` because it isn't annotated with `#[const_trait]`
36+
--> const-super-trait.rs:3:1
37+
|
38+
3 | trait Foo {
39+
| ^^^^^^^^^
40+
41+
error: `~const` can only be applied to `#[const_trait]` traits
42+
--> const-super-trait.rs:9:17
43+
|
44+
9 | const fn foo<T: ~const Bar>(x: &T) {
45+
| ^^^^^^ can't be applied to `Bar`
46+
|
47+
note: `Bar` can't be used with `~const` because it isn't annotated with `#[const_trait]`
48+
--> const-super-trait.rs:7:1
49+
|
50+
7 | trait Bar: ~const Foo {}
51+
| ^^^^^^^^^^^^^^^^^^^^^
52+
53+
error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
54+
--> const-super-trait.rs:10:7
55+
|
56+
10 | x.a();
57+
| ^^^
58+
|
59+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
60+
61+
error: aborting due to 6 previous errors
62+
63+
Some errors have detailed explanations: E0015, E0658.
64+
For more information about an error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: `~const` is not allowed here
2+
--> const-super-trait.rs:7:12
3+
|
4+
7 | trait Bar: ~const Foo {}
5+
| ^^^^^^
6+
|
7+
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
8+
--> const-super-trait.rs:7:1
9+
|
10+
7 | trait Bar: ~const Foo {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0554]: `#![feature]` may not be used on the NIGHTLY release channel
14+
--> const-super-trait.rs:1:30
15+
|
16+
1 | #![cfg_attr(feature_enabled, feature(const_trait_impl))]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: `~const` can only be applied to `#[const_trait]` traits
20+
--> const-super-trait.rs:7:12
21+
|
22+
7 | trait Bar: ~const Foo {}
23+
| ^^^^^^ can't be applied to `Foo`
24+
|
25+
note: `Foo` can't be used with `~const` because it isn't annotated with `#[const_trait]`
26+
--> const-super-trait.rs:3:1
27+
|
28+
3 | trait Foo {
29+
| ^^^^^^^^^
30+
31+
error: `~const` can only be applied to `#[const_trait]` traits
32+
--> const-super-trait.rs:9:17
33+
|
34+
9 | const fn foo<T: ~const Bar>(x: &T) {
35+
| ^^^^^^ can't be applied to `Bar`
36+
|
37+
note: `Bar` can't be used with `~const` because it isn't annotated with `#[const_trait]`
38+
--> const-super-trait.rs:7:1
39+
|
40+
7 | trait Bar: ~const Foo {}
41+
| ^^^^^^^^^^^^^^^^^^^^^
42+
43+
error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
44+
--> const-super-trait.rs:10:7
45+
|
46+
10 | x.a();
47+
| ^^^
48+
|
49+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
50+
51+
error: aborting due to 5 previous errors
52+
53+
Some errors have detailed explanations: E0015, E0554.
54+
For more information about an error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![cfg_attr(feature_enabled, feature(const_trait_impl))]
2+
3+
trait Foo {
4+
fn a(&self);
5+
}
6+
7+
trait Bar: ~const Foo {}
8+
9+
const fn foo<T: ~const Bar>(x: &T) {
10+
x.a();
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)