Skip to content

Commit a5c24ef

Browse files
committed
Tweak wording of non-const traits used as const bounds
Use verbose suggestions and add additional labels/notes.
1 parent f6cb952 commit a5c24ef

36 files changed

+401
-121
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 = mark `{$trait_name}` as `const`
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 = not marked with `#[const_trait]`
106+
.suggestion = mark `{$trait_name}` as `const`
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/errors.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,14 @@ 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>,
537542
#[note]
538543
pub marking: (),
@@ -544,8 +549,18 @@ pub(crate) struct ConstImplForNonConstTrait {
544549
#[diag(hir_analysis_const_bound_for_non_const_trait)]
545550
pub(crate) struct ConstBoundForNonConstTrait {
546551
#[primary_span]
552+
#[label]
547553
pub span: Span,
548554
pub modifier: &'static str,
555+
#[note]
556+
pub def_span: Option<Span>,
557+
#[suggestion(
558+
applicability = "machine-applicable",
559+
code = "#[const_trait] ",
560+
style = "verbose"
561+
)]
562+
pub suggestion: Option<Span>,
563+
pub trait_name: String,
549564
}
550565

551566
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,17 @@ 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) = if trait_def_id.is_local() {
741+
(None, Some(tcx.def_span(trait_def_id).shrink_to_lo()))
742+
} else {
743+
(Some(tcx.def_span(trait_def_id)), None)
744+
};
740745
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
741746
span,
742747
modifier: constness.as_str(),
748+
def_span,
749+
trait_name: self.tcx().def_path_str(trait_def_id),
750+
suggestion,
743751
});
744752
}
745753

tests/ui/consts/const-try.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: const `impl` for trait `FromResidual` which is not marked with `#[const_t
22
--> $DIR/const-try.rs:15:12
33
|
44
LL | impl const FromResidual<Error> for TryMe {
5-
| ^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^ not marked with `#[const_trait]`
66
|
77
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
88
= note: adding a non-const method body in the future would be a breaking change
@@ -11,7 +11,7 @@ error: const `impl` for trait `Try` which is not marked with `#[const_trait]`
1111
--> $DIR/const-try.rs:22:12
1212
|
1313
LL | impl const Try for TryMe {
14-
| ^^^
14+
| ^^^ not marked with `#[const_trait]`
1515
|
1616
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
1717
= note: adding a non-const method body in the future would be a breaking change

tests/ui/consts/fn_trait_refs.stderr

+50-15
Original file line numberDiff line numberDiff line change
@@ -14,110 +14,145 @@ error: `~const` can only be applied to `#[const_trait]` traits
1414
--> $DIR/fn_trait_refs.rs:14:8
1515
|
1616
LL | T: ~const Fn<()> + ~const Destruct,
17-
| ^^^^^^
17+
| ^^^^^^ can't be applied to `Fn`
18+
|
19+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
20+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
1821

1922
error: `~const` can only be applied to `#[const_trait]` traits
2023
--> $DIR/fn_trait_refs.rs:14:8
2124
|
2225
LL | T: ~const Fn<()> + ~const Destruct,
23-
| ^^^^^^
26+
| ^^^^^^ can't be applied to `Fn`
2427
|
28+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
29+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
2530
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2631

2732
error: `~const` can only be applied to `#[const_trait]` traits
2833
--> $DIR/fn_trait_refs.rs:14:8
2934
|
3035
LL | T: ~const Fn<()> + ~const Destruct,
31-
| ^^^^^^
36+
| ^^^^^^ can't be applied to `Fn`
3237
|
38+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
39+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
3340
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3441

3542
error: `~const` can only be applied to `#[const_trait]` traits
3643
--> $DIR/fn_trait_refs.rs:21:8
3744
|
3845
LL | T: ~const FnMut<()> + ~const Destruct,
39-
| ^^^^^^
46+
| ^^^^^^ can't be applied to `FnMut`
47+
|
48+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
49+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
4050

4151
error: `~const` can only be applied to `#[const_trait]` traits
4252
--> $DIR/fn_trait_refs.rs:21:8
4353
|
4454
LL | T: ~const FnMut<()> + ~const Destruct,
45-
| ^^^^^^
55+
| ^^^^^^ can't be applied to `FnMut`
4656
|
57+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
58+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
4759
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4860

4961
error: `~const` can only be applied to `#[const_trait]` traits
5062
--> $DIR/fn_trait_refs.rs:21:8
5163
|
5264
LL | T: ~const FnMut<()> + ~const Destruct,
53-
| ^^^^^^
65+
| ^^^^^^ can't be applied to `FnMut`
5466
|
67+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
68+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
5569
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
5670

5771
error: `~const` can only be applied to `#[const_trait]` traits
5872
--> $DIR/fn_trait_refs.rs:28:8
5973
|
6074
LL | T: ~const FnOnce<()>,
61-
| ^^^^^^
75+
| ^^^^^^ can't be applied to `FnOnce`
76+
|
77+
note: `FnOnce` can't be used with `~const` because it isn't annotated with `#[const_trait]`
78+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
6279

6380
error: `~const` can only be applied to `#[const_trait]` traits
6481
--> $DIR/fn_trait_refs.rs:28:8
6582
|
6683
LL | T: ~const FnOnce<()>,
67-
| ^^^^^^
84+
| ^^^^^^ can't be applied to `FnOnce`
6885
|
86+
note: `FnOnce` can't be used with `~const` because it isn't annotated with `#[const_trait]`
87+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
6988
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
7089

7190
error: `~const` can only be applied to `#[const_trait]` traits
7291
--> $DIR/fn_trait_refs.rs:28:8
7392
|
7493
LL | T: ~const FnOnce<()>,
75-
| ^^^^^^
94+
| ^^^^^^ can't be applied to `FnOnce`
7695
|
96+
note: `FnOnce` can't be used with `~const` because it isn't annotated with `#[const_trait]`
97+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
7798
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
7899

79100
error: `~const` can only be applied to `#[const_trait]` traits
80101
--> $DIR/fn_trait_refs.rs:35:8
81102
|
82103
LL | T: ~const Fn<()> + ~const Destruct,
83-
| ^^^^^^
104+
| ^^^^^^ can't be applied to `Fn`
105+
|
106+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
107+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
84108

85109
error: `~const` can only be applied to `#[const_trait]` traits
86110
--> $DIR/fn_trait_refs.rs:35:8
87111
|
88112
LL | T: ~const Fn<()> + ~const Destruct,
89-
| ^^^^^^
113+
| ^^^^^^ can't be applied to `Fn`
90114
|
115+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
116+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
91117
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
92118

93119
error: `~const` can only be applied to `#[const_trait]` traits
94120
--> $DIR/fn_trait_refs.rs:35:8
95121
|
96122
LL | T: ~const Fn<()> + ~const Destruct,
97-
| ^^^^^^
123+
| ^^^^^^ can't be applied to `Fn`
98124
|
125+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
126+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
99127
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
100128

101129
error: `~const` can only be applied to `#[const_trait]` traits
102130
--> $DIR/fn_trait_refs.rs:49:8
103131
|
104132
LL | T: ~const FnMut<()> + ~const Destruct,
105-
| ^^^^^^
133+
| ^^^^^^ can't be applied to `FnMut`
134+
|
135+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
136+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
106137

107138
error: `~const` can only be applied to `#[const_trait]` traits
108139
--> $DIR/fn_trait_refs.rs:49:8
109140
|
110141
LL | T: ~const FnMut<()> + ~const Destruct,
111-
| ^^^^^^
142+
| ^^^^^^ can't be applied to `FnMut`
112143
|
144+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
145+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
113146
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
114147

115148
error: `~const` can only be applied to `#[const_trait]` traits
116149
--> $DIR/fn_trait_refs.rs:49:8
117150
|
118151
LL | T: ~const FnMut<()> + ~const Destruct,
119-
| ^^^^^^
152+
| ^^^^^^ can't be applied to `FnMut`
120153
|
154+
note: `FnMut` can't be used with `~const` because it isn't annotated with `#[const_trait]`
155+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
121156
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
122157

123158
error[E0277]: the trait bound `fn() -> i32 {one}: const Destruct` is not satisfied

tests/ui/consts/rustc-impl-const-stability.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: const `impl` for trait `Default` which is not marked with `#[const_trait]
22
--> $DIR/rustc-impl-const-stability.rs:15:12
33
|
44
LL | impl const Default for Data {
5-
| ^^^^^^^
5+
| ^^^^^^^ not marked with `#[const_trait]`
66
|
77
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
88
= note: adding a non-const method body in the future would be a breaking change

tests/ui/consts/unstable-const-fn-in-libcore.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ error: `~const` can only be applied to `#[const_trait]` traits
22
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
33
|
44
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
5-
| ^^^^^^
5+
| ^^^^^^ can't be applied to `FnOnce`
6+
|
7+
note: `FnOnce` can't be used with `~const` because it isn't annotated with `#[const_trait]`
8+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
69

710
error: `~const` can only be applied to `#[const_trait]` traits
811
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
912
|
1013
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
11-
| ^^^^^^
14+
| ^^^^^^ can't be applied to `FnOnce`
1215
|
16+
note: `FnOnce` can't be used with `~const` because it isn't annotated with `#[const_trait]`
17+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
1318
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1419

1520
error[E0015]: cannot call non-const closure in constant functions

tests/ui/impl-trait/normalize-tait-in-const.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ error: `~const` can only be applied to `#[const_trait]` traits
22
--> $DIR/normalize-tait-in-const.rs:26:35
33
|
44
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
5-
| ^^^^^^
5+
| ^^^^^^ can't be applied to `Fn`
6+
|
7+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
8+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
69

710
error: `~const` can only be applied to `#[const_trait]` traits
811
--> $DIR/normalize-tait-in-const.rs:26:35
912
|
1013
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
11-
| ^^^^^^
14+
| ^^^^^^ can't be applied to `Fn`
1215
|
16+
note: `Fn` can't be used with `~const` because it isn't annotated with `#[const_trait]`
17+
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
1318
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1419

1520
error[E0277]: the trait bound `for<'a, 'b> fn(&'a foo::Alias<'b>) {foo}: const Destruct` is not satisfied

tests/ui/specialization/const_trait_impl.stderr

+21-6
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,57 @@ error: `~const` can only be applied to `#[const_trait]` traits
22
--> $DIR/const_trait_impl.rs:34:9
33
|
44
LL | impl<T: ~const Default> const A for T {
5-
| ^^^^^^
5+
| ^^^^^^ can't be applied to `Default`
6+
|
7+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
8+
--> $SRC_DIR/core/src/default.rs:LL:COL
69

710
error: `~const` can only be applied to `#[const_trait]` traits
811
--> $DIR/const_trait_impl.rs:40:9
912
|
1013
LL | impl<T: ~const Default + ~const Sup> const A for T {
11-
| ^^^^^^
14+
| ^^^^^^ can't be applied to `Default`
15+
|
16+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
17+
--> $SRC_DIR/core/src/default.rs:LL:COL
1218

1319
error: `~const` can only be applied to `#[const_trait]` traits
1420
--> $DIR/const_trait_impl.rs:46:9
1521
|
1622
LL | impl<T: ~const Default + ~const Sub> const A for T {
17-
| ^^^^^^
23+
| ^^^^^^ can't be applied to `Default`
24+
|
25+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
26+
--> $SRC_DIR/core/src/default.rs:LL:COL
1827

1928
error: `~const` can only be applied to `#[const_trait]` traits
2029
--> $DIR/const_trait_impl.rs:40:9
2130
|
2231
LL | impl<T: ~const Default + ~const Sup> const A for T {
23-
| ^^^^^^
32+
| ^^^^^^ can't be applied to `Default`
2433
|
34+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
35+
--> $SRC_DIR/core/src/default.rs:LL:COL
2536
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2637

2738
error: `~const` can only be applied to `#[const_trait]` traits
2839
--> $DIR/const_trait_impl.rs:34:9
2940
|
3041
LL | impl<T: ~const Default> const A for T {
31-
| ^^^^^^
42+
| ^^^^^^ can't be applied to `Default`
3243
|
44+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
45+
--> $SRC_DIR/core/src/default.rs:LL:COL
3346
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3447

3548
error: `~const` can only be applied to `#[const_trait]` traits
3649
--> $DIR/const_trait_impl.rs:46:9
3750
|
3851
LL | impl<T: ~const Default + ~const Sub> const A for T {
39-
| ^^^^^^
52+
| ^^^^^^ can't be applied to `Default`
4053
|
54+
note: `Default` can't be used with `~const` because it isn't annotated with `#[const_trait]`
55+
--> $SRC_DIR/core/src/default.rs:LL:COL
4156
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4257

4358
error: aborting due to 6 previous errors

tests/ui/traits/const-traits/call-const-trait-method-pass.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: const `impl` for trait `PartialEq` which is not marked with `#[const_trai
22
--> $DIR/call-const-trait-method-pass.rs:15:12
33
|
44
LL | impl const PartialEq for Int {
5-
| ^^^^^^^^^
5+
| ^^^^^^^^^ not marked with `#[const_trait]`
66
|
77
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
88
= note: adding a non-const method body in the future would be a breaking change

0 commit comments

Comments
 (0)