Skip to content

Commit 74b0ef0

Browse files
committed
Tweak trait modifier errors
1 parent 648abd3 commit 74b0ef0

15 files changed

+61
-66
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,10 @@ parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
858858
parse_trait_alias_cannot_be_const = trait aliases cannot be `const`
859859
parse_trait_alias_cannot_be_unsafe = trait aliases cannot be `unsafe`
860860
861-
parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$annotation}
862-
.because = {$annotation} because of this
861+
parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$modifier_name}
862+
.because = {$modifier_name} because of this
863863
.type = inherent impl for this type
864-
.only_trait = only trait implementations may be annotated with {$annotation}
864+
.note = only trait implementations may be annotated with `{$modifier}`
865865
866866
parse_transpose_dyn_or_impl = `for<...>` expected after `{$kw}`, not before
867867
.suggestion = move `{$kw}` before the `for<...>`

compiler/rustc_parse/src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ pub(crate) struct BadQPathStage2 {
7373

7474
#[derive(Diagnostic)]
7575
#[diag(parse_trait_impl_modifier_in_inherent_impl)]
76-
pub(crate) struct TraitImplModifierInInherentImpl<'a> {
76+
#[note]
77+
pub(crate) struct TraitImplModifierInInherentImpl {
7778
#[primary_span]
7879
pub span: Span,
80+
pub modifier: &'static str,
81+
pub modifier_name: &'static str,
7982
#[label(parse_because)]
80-
pub annotation_span: Span,
81-
pub annotation: &'a str,
83+
pub modifier_span: Span,
8284
#[label(parse_type)]
8385
pub self_ty: Span,
84-
#[note(parse_only_trait)]
85-
pub only_trait: bool,
8686
}
8787

8888
#[derive(Subdiagnostic)]

compiler/rustc_parse/src/parser/item.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -668,36 +668,27 @@ impl<'a> Parser<'a> {
668668
}
669669
None => {
670670
let self_ty = ty_first;
671-
let error = |annotation_span, annotation, only_trait| {
672-
errors::TraitImplModifierInInherentImpl {
671+
let error = |modifier, modifier_name, modifier_span| {
672+
self.dcx().create_err(errors::TraitImplModifierInInherentImpl {
673673
span: self_ty.span,
674-
annotation_span,
675-
annotation,
674+
modifier,
675+
modifier_name,
676+
modifier_span,
676677
self_ty: self_ty.span,
677-
only_trait,
678-
}
678+
})
679679
};
680680

681681
if let Safety::Unsafe(span) = safety {
682-
self.dcx()
683-
.create_err(errors::TraitImplModifierInInherentImpl {
684-
span: self_ty.span,
685-
annotation_span: span,
686-
annotation: "unsafe",
687-
self_ty: self_ty.span,
688-
only_trait: true,
689-
})
690-
.with_code(E0197)
691-
.emit();
682+
error("unsafe", "unsafe", span).with_code(E0197).emit();
692683
}
693684
if let ImplPolarity::Negative(span) = polarity {
694-
self.dcx().emit_err(error(span, "negative", false));
685+
error("!", "negative", span).emit();
695686
}
696687
if let Defaultness::Default(def_span) = defaultness {
697-
self.dcx().emit_err(error(def_span, "`default`", true));
688+
error("default", "default", def_span).emit();
698689
}
699690
if let Const::Yes(span) = constness {
700-
self.dcx().emit_err(error(span, "`const`", true));
691+
error("const", "const", span).emit();
701692
}
702693
(None, self_ty)
703694
}

tests/ui/error-codes/E0197.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | unsafe impl Foo { }
66
| |
77
| unsafe because of this
88
|
9-
= note: only trait implementations may be annotated with unsafe
9+
= note: only trait implementations may be annotated with `unsafe`
1010

1111
error: aborting due to 1 previous error
1212

tests/ui/parser/default-on-wrong-item-kind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod free_items {
1919
default union foo {} //~ ERROR a union cannot be `default`
2020
default trait foo {} //~ ERROR a trait cannot be `default`
2121
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
22-
default impl foo {} //~ ERROR inherent impls cannot be `default`
22+
default impl foo {} //~ ERROR inherent impls cannot be default
2323
default!();
2424
default::foo::bar!();
2525
default default!(); //~ ERROR an item macro invocation cannot be `default`
@@ -53,7 +53,7 @@ extern "C" {
5353
//~^ ERROR trait is not supported in `extern` blocks
5454
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
5555
//~^ ERROR trait alias is not supported in `extern` blocks
56-
default impl foo {} //~ ERROR inherent impls cannot be `default`
56+
default impl foo {} //~ ERROR inherent impls cannot be default
5757
//~^ ERROR implementation is not supported in `extern` blocks
5858
default!();
5959
default::foo::bar!();
@@ -90,7 +90,7 @@ impl S {
9090
//~^ ERROR trait is not supported in `trait`s or `impl`s
9191
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
9292
//~^ ERROR trait alias is not supported in `trait`s or `impl`s
93-
default impl foo {} //~ ERROR inherent impls cannot be `default`
93+
default impl foo {} //~ ERROR inherent impls cannot be default
9494
//~^ ERROR implementation is not supported in `trait`s or `impl`s
9595
default!();
9696
default::foo::bar!();
@@ -127,7 +127,7 @@ trait T {
127127
//~^ ERROR trait is not supported in `trait`s or `impl`s
128128
default trait foo = Ord; //~ ERROR a trait alias cannot be `default`
129129
//~^ ERROR trait alias is not supported in `trait`s or `impl`s
130-
default impl foo {} //~ ERROR inherent impls cannot be `default`
130+
default impl foo {} //~ ERROR inherent impls cannot be default
131131
//~^ ERROR implementation is not supported in `trait`s or `impl`s
132132
default!();
133133
default::foo::bar!();

tests/ui/parser/default-on-wrong-item-kind.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ LL | default trait foo = Ord;
7878
|
7979
= note: only associated `fn`, `const`, and `type` items can be `default`
8080

81-
error: inherent impls cannot be `default`
81+
error: inherent impls cannot be default
8282
--> $DIR/default-on-wrong-item-kind.rs:22:18
8383
|
8484
LL | default impl foo {}
8585
| ------- ^^^ inherent impl for this type
8686
| |
87-
| `default` because of this
87+
| default because of this
8888
|
8989
= note: only trait implementations may be annotated with `default`
9090

@@ -285,13 +285,13 @@ LL | default trait foo = Ord;
285285
|
286286
= help: consider moving the trait alias out to a nearby module scope
287287

288-
error: inherent impls cannot be `default`
288+
error: inherent impls cannot be default
289289
--> $DIR/default-on-wrong-item-kind.rs:56:18
290290
|
291291
LL | default impl foo {}
292292
| ------- ^^^ inherent impl for this type
293293
| |
294-
| `default` because of this
294+
| default because of this
295295
|
296296
= note: only trait implementations may be annotated with `default`
297297

@@ -509,13 +509,13 @@ LL | default trait foo = Ord;
509509
|
510510
= help: consider moving the trait alias out to a nearby module scope
511511

512-
error: inherent impls cannot be `default`
512+
error: inherent impls cannot be default
513513
--> $DIR/default-on-wrong-item-kind.rs:93:18
514514
|
515515
LL | default impl foo {}
516516
| ------- ^^^ inherent impl for this type
517517
| |
518-
| `default` because of this
518+
| default because of this
519519
|
520520
= note: only trait implementations may be annotated with `default`
521521

@@ -733,13 +733,13 @@ LL | default trait foo = Ord;
733733
|
734734
= help: consider moving the trait alias out to a nearby module scope
735735

736-
error: inherent impls cannot be `default`
736+
error: inherent impls cannot be default
737737
--> $DIR/default-on-wrong-item-kind.rs:130:18
738738
|
739739
LL | default impl foo {}
740740
| ------- ^^^ inherent impl for this type
741741
| |
742-
| `default` because of this
742+
| default because of this
743743
|
744744
= note: only trait implementations may be annotated with `default`
745745

tests/ui/specialization/defaultimpl/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
struct S;
55
struct Z;
66

7-
default impl S {} //~ ERROR inherent impls cannot be `default`
7+
default impl S {} //~ ERROR inherent impls cannot be default
88

99
default unsafe impl Send for S {}
1010
//~^ ERROR impls of auto traits cannot be default

tests/ui/specialization/defaultimpl/validation.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: inherent impls cannot be `default`
1+
error: inherent impls cannot be default
22
--> $DIR/validation.rs:7:14
33
|
44
LL | default impl S {}
55
| ------- ^ inherent impl for this type
66
| |
7-
| `default` because of this
7+
| default because of this
88
|
99
= note: only trait implementations may be annotated with `default`
1010

tests/ui/traits/const-traits/inherent-impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ struct S;
55
trait T {}
66

77
impl const S {}
8-
//~^ ERROR inherent impls cannot be `const`
8+
//~^ ERROR inherent impls cannot be const
99

1010
impl const dyn T {}
11-
//~^ ERROR inherent impls cannot be `const`
11+
//~^ ERROR inherent impls cannot be const
1212

1313
fn main() {}

tests/ui/traits/const-traits/inherent-impl.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error: inherent impls cannot be `const`
1+
error: inherent impls cannot be const
22
--> $DIR/inherent-impl.rs:7:12
33
|
44
LL | impl const S {}
55
| ----- ^ inherent impl for this type
66
| |
7-
| `const` because of this
7+
| const because of this
88
|
99
= note: only trait implementations may be annotated with `const`
1010

11-
error: inherent impls cannot be `const`
11+
error: inherent impls cannot be const
1212
--> $DIR/inherent-impl.rs:10:12
1313
|
1414
LL | impl const dyn T {}
1515
| ----- ^^^^^ inherent impl for this type
1616
| |
17-
| `const` because of this
17+
| const because of this
1818
|
1919
= note: only trait implementations may be annotated with `const`
2020

0 commit comments

Comments
 (0)