From 3aa0ac0a8af32f2c5da106c5e89bd9712d5a9655 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Wed, 30 Jul 2025 18:00:53 -0500 Subject: [PATCH] Tweak trait modifier errors --- compiler/rustc_parse/messages.ftl | 6 ++-- compiler/rustc_parse/src/errors.rs | 10 +++---- compiler/rustc_parse/src/parser/item.rs | 29 +++++++------------ tests/ui/error-codes/E0197.stderr | 2 +- tests/ui/parser/default-on-wrong-item-kind.rs | 8 ++--- .../parser/default-on-wrong-item-kind.stderr | 16 +++++----- .../specialization/defaultimpl/validation.rs | 2 +- .../defaultimpl/validation.stderr | 4 +-- tests/ui/traits/const-traits/inherent-impl.rs | 4 +-- .../traits/const-traits/inherent-impl.stderr | 8 ++--- ...-trait-bound-theoretical-regression.stderr | 4 +-- .../const-traits/span-bug-issue-121418.rs | 2 +- .../const-traits/span-bug-issue-121418.stderr | 4 +-- tests/ui/traits/safety-inherent-impl.stderr | 2 +- tests/ui/traits/syntax-trait-polarity.stderr | 4 +++ 15 files changed, 50 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 7970d8d552f9..3a21eea3d0ae 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -869,10 +869,10 @@ parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto` parse_trait_alias_cannot_be_const = trait aliases cannot be `const` parse_trait_alias_cannot_be_unsafe = trait aliases cannot be `unsafe` -parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$annotation} - .because = {$annotation} because of this +parse_trait_impl_modifier_in_inherent_impl = inherent impls cannot be {$modifier_name} + .because = {$modifier_name} because of this .type = inherent impl for this type - .only_trait = only trait implementations may be annotated with {$annotation} + .note = only trait implementations may be annotated with `{$modifier}` parse_transpose_dyn_or_impl = `for<...>` expected after `{$kw}`, not before .suggestion = move `{$kw}` before the `for<...>` diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 2a704ee61ecd..a07d0606fd0f 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -73,16 +73,16 @@ pub(crate) struct BadQPathStage2 { #[derive(Diagnostic)] #[diag(parse_trait_impl_modifier_in_inherent_impl)] -pub(crate) struct TraitImplModifierInInherentImpl<'a> { +#[note] +pub(crate) struct TraitImplModifierInInherentImpl { #[primary_span] pub span: Span, + pub modifier: &'static str, + pub modifier_name: &'static str, #[label(parse_because)] - pub annotation_span: Span, - pub annotation: &'a str, + pub modifier_span: Span, #[label(parse_type)] pub self_ty: Span, - #[note(parse_only_trait)] - pub only_trait: bool, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 96d7120e21ec..75cb103d5d63 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -667,36 +667,27 @@ fn parse_item_impl( } None => { let self_ty = ty_first; - let error = |annotation_span, annotation, only_trait| { - errors::TraitImplModifierInInherentImpl { + let error = |modifier, modifier_name, modifier_span| { + self.dcx().create_err(errors::TraitImplModifierInInherentImpl { span: self_ty.span, - annotation_span, - annotation, + modifier, + modifier_name, + modifier_span, self_ty: self_ty.span, - only_trait, - } + }) }; if let Safety::Unsafe(span) = safety { - self.dcx() - .create_err(errors::TraitImplModifierInInherentImpl { - span: self_ty.span, - annotation_span: span, - annotation: "unsafe", - self_ty: self_ty.span, - only_trait: true, - }) - .with_code(E0197) - .emit(); + error("unsafe", "unsafe", span).with_code(E0197).emit(); } if let ImplPolarity::Negative(span) = polarity { - self.dcx().emit_err(error(span, "negative", false)); + error("!", "negative", span).emit(); } if let Defaultness::Default(def_span) = defaultness { - self.dcx().emit_err(error(def_span, "`default`", true)); + error("default", "default", def_span).emit(); } if let Const::Yes(span) = constness { - self.dcx().emit_err(error(span, "`const`", true)); + error("const", "const", span).emit(); } (None, self_ty) } diff --git a/tests/ui/error-codes/E0197.stderr b/tests/ui/error-codes/E0197.stderr index e3d3c7dde9c3..04c6174b9f1d 100644 --- a/tests/ui/error-codes/E0197.stderr +++ b/tests/ui/error-codes/E0197.stderr @@ -6,7 +6,7 @@ LL | unsafe impl Foo { } | | | unsafe because of this | - = note: only trait implementations may be annotated with unsafe + = note: only trait implementations may be annotated with `unsafe` error: aborting due to 1 previous error diff --git a/tests/ui/parser/default-on-wrong-item-kind.rs b/tests/ui/parser/default-on-wrong-item-kind.rs index db4e41a28ac2..9de856405656 100644 --- a/tests/ui/parser/default-on-wrong-item-kind.rs +++ b/tests/ui/parser/default-on-wrong-item-kind.rs @@ -19,7 +19,7 @@ mod free_items { default union foo {} //~ ERROR a union cannot be `default` default trait foo {} //~ ERROR a trait cannot be `default` default trait foo = Ord; //~ ERROR a trait alias cannot be `default` - default impl foo {} //~ ERROR inherent impls cannot be `default` + default impl foo {} //~ ERROR inherent impls cannot be default default!(); default::foo::bar!(); default default!(); //~ ERROR an item macro invocation cannot be `default` @@ -53,7 +53,7 @@ mod free_items { //~^ ERROR trait is not supported in `extern` blocks default trait foo = Ord; //~ ERROR a trait alias cannot be `default` //~^ ERROR trait alias is not supported in `extern` blocks - default impl foo {} //~ ERROR inherent impls cannot be `default` + default impl foo {} //~ ERROR inherent impls cannot be default //~^ ERROR implementation is not supported in `extern` blocks default!(); default::foo::bar!(); @@ -90,7 +90,7 @@ impl S { //~^ ERROR trait is not supported in `trait`s or `impl`s default trait foo = Ord; //~ ERROR a trait alias cannot be `default` //~^ ERROR trait alias is not supported in `trait`s or `impl`s - default impl foo {} //~ ERROR inherent impls cannot be `default` + default impl foo {} //~ ERROR inherent impls cannot be default //~^ ERROR implementation is not supported in `trait`s or `impl`s default!(); default::foo::bar!(); @@ -127,7 +127,7 @@ trait T { //~^ ERROR trait is not supported in `trait`s or `impl`s default trait foo = Ord; //~ ERROR a trait alias cannot be `default` //~^ ERROR trait alias is not supported in `trait`s or `impl`s - default impl foo {} //~ ERROR inherent impls cannot be `default` + default impl foo {} //~ ERROR inherent impls cannot be default //~^ ERROR implementation is not supported in `trait`s or `impl`s default!(); default::foo::bar!(); diff --git a/tests/ui/parser/default-on-wrong-item-kind.stderr b/tests/ui/parser/default-on-wrong-item-kind.stderr index fb15af832caa..0380fcdf775f 100644 --- a/tests/ui/parser/default-on-wrong-item-kind.stderr +++ b/tests/ui/parser/default-on-wrong-item-kind.stderr @@ -78,13 +78,13 @@ LL | default trait foo = Ord; | = note: only associated `fn`, `const`, and `type` items can be `default` -error: inherent impls cannot be `default` +error: inherent impls cannot be default --> $DIR/default-on-wrong-item-kind.rs:22:18 | LL | default impl foo {} | ------- ^^^ inherent impl for this type | | - | `default` because of this + | default because of this | = note: only trait implementations may be annotated with `default` @@ -285,13 +285,13 @@ LL | default trait foo = Ord; | = help: consider moving the trait alias out to a nearby module scope -error: inherent impls cannot be `default` +error: inherent impls cannot be default --> $DIR/default-on-wrong-item-kind.rs:56:18 | LL | default impl foo {} | ------- ^^^ inherent impl for this type | | - | `default` because of this + | default because of this | = note: only trait implementations may be annotated with `default` @@ -509,13 +509,13 @@ LL | default trait foo = Ord; | = help: consider moving the trait alias out to a nearby module scope -error: inherent impls cannot be `default` +error: inherent impls cannot be default --> $DIR/default-on-wrong-item-kind.rs:93:18 | LL | default impl foo {} | ------- ^^^ inherent impl for this type | | - | `default` because of this + | default because of this | = note: only trait implementations may be annotated with `default` @@ -733,13 +733,13 @@ LL | default trait foo = Ord; | = help: consider moving the trait alias out to a nearby module scope -error: inherent impls cannot be `default` +error: inherent impls cannot be default --> $DIR/default-on-wrong-item-kind.rs:130:18 | LL | default impl foo {} | ------- ^^^ inherent impl for this type | | - | `default` because of this + | default because of this | = note: only trait implementations may be annotated with `default` diff --git a/tests/ui/specialization/defaultimpl/validation.rs b/tests/ui/specialization/defaultimpl/validation.rs index 14771be8982d..78f6099c3dd9 100644 --- a/tests/ui/specialization/defaultimpl/validation.rs +++ b/tests/ui/specialization/defaultimpl/validation.rs @@ -4,7 +4,7 @@ struct S; struct Z; -default impl S {} //~ ERROR inherent impls cannot be `default` +default impl S {} //~ ERROR inherent impls cannot be default default unsafe impl Send for S {} //~^ ERROR impls of auto traits cannot be default diff --git a/tests/ui/specialization/defaultimpl/validation.stderr b/tests/ui/specialization/defaultimpl/validation.stderr index 82a33bf9cdc3..a8dfef2dcfb1 100644 --- a/tests/ui/specialization/defaultimpl/validation.stderr +++ b/tests/ui/specialization/defaultimpl/validation.stderr @@ -1,10 +1,10 @@ -error: inherent impls cannot be `default` +error: inherent impls cannot be default --> $DIR/validation.rs:7:14 | LL | default impl S {} | ------- ^ inherent impl for this type | | - | `default` because of this + | default because of this | = note: only trait implementations may be annotated with `default` diff --git a/tests/ui/traits/const-traits/inherent-impl.rs b/tests/ui/traits/const-traits/inherent-impl.rs index 07b23adf9e1f..5ca4c5d7863d 100644 --- a/tests/ui/traits/const-traits/inherent-impl.rs +++ b/tests/ui/traits/const-traits/inherent-impl.rs @@ -5,9 +5,9 @@ trait T {} impl const S {} -//~^ ERROR inherent impls cannot be `const` +//~^ ERROR inherent impls cannot be const impl const dyn T {} -//~^ ERROR inherent impls cannot be `const` +//~^ ERROR inherent impls cannot be const fn main() {} diff --git a/tests/ui/traits/const-traits/inherent-impl.stderr b/tests/ui/traits/const-traits/inherent-impl.stderr index e4ec1e807b0e..d828ecb6349e 100644 --- a/tests/ui/traits/const-traits/inherent-impl.stderr +++ b/tests/ui/traits/const-traits/inherent-impl.stderr @@ -1,20 +1,20 @@ -error: inherent impls cannot be `const` +error: inherent impls cannot be const --> $DIR/inherent-impl.rs:7:12 | LL | impl const S {} | ----- ^ inherent impl for this type | | - | `const` because of this + | const because of this | = note: only trait implementations may be annotated with `const` -error: inherent impls cannot be `const` +error: inherent impls cannot be const --> $DIR/inherent-impl.rs:10:12 | LL | impl const dyn T {} | ----- ^^^^^ inherent impl for this type | | - | `const` because of this + | const because of this | = note: only trait implementations may be annotated with `const` diff --git a/tests/ui/traits/const-traits/macro-const-trait-bound-theoretical-regression.stderr b/tests/ui/traits/const-traits/macro-const-trait-bound-theoretical-regression.stderr index 58d38a62c679..9fad260f0bea 100644 --- a/tests/ui/traits/const-traits/macro-const-trait-bound-theoretical-regression.stderr +++ b/tests/ui/traits/const-traits/macro-const-trait-bound-theoretical-regression.stderr @@ -1,4 +1,4 @@ -error: inherent impls cannot be `const` +error: inherent impls cannot be const --> $DIR/macro-const-trait-bound-theoretical-regression.rs:10:40 | LL | (impl $c:ident Trait) => { impl $c Trait {} }; @@ -7,7 +7,7 @@ LL | (impl $c:ident Trait) => { impl $c Trait {} }; LL | demo! { impl const Trait } | -------------------------- | | | - | | `const` because of this + | | const because of this | in this macro invocation | = note: only trait implementations may be annotated with `const` diff --git a/tests/ui/traits/const-traits/span-bug-issue-121418.rs b/tests/ui/traits/const-traits/span-bug-issue-121418.rs index 50a7e12f2a78..593180ac0946 100644 --- a/tests/ui/traits/const-traits/span-bug-issue-121418.rs +++ b/tests/ui/traits/const-traits/span-bug-issue-121418.rs @@ -4,7 +4,7 @@ trait T {} impl const dyn T { - //~^ ERROR inherent impls cannot be `const` + //~^ ERROR inherent impls cannot be const pub const fn new() -> std::sync::Mutex {} //~^ ERROR mismatched types //~| ERROR cannot be known at compilation time diff --git a/tests/ui/traits/const-traits/span-bug-issue-121418.stderr b/tests/ui/traits/const-traits/span-bug-issue-121418.stderr index f31129d9cb7c..0c8ca918a3e1 100644 --- a/tests/ui/traits/const-traits/span-bug-issue-121418.stderr +++ b/tests/ui/traits/const-traits/span-bug-issue-121418.stderr @@ -1,10 +1,10 @@ -error: inherent impls cannot be `const` +error: inherent impls cannot be const --> $DIR/span-bug-issue-121418.rs:6:12 | LL | impl const dyn T { | ----- ^^^^^ inherent impl for this type | | - | `const` because of this + | const because of this | = note: only trait implementations may be annotated with `const` diff --git a/tests/ui/traits/safety-inherent-impl.stderr b/tests/ui/traits/safety-inherent-impl.stderr index f4443857dc92..45cdbe2b523b 100644 --- a/tests/ui/traits/safety-inherent-impl.stderr +++ b/tests/ui/traits/safety-inherent-impl.stderr @@ -6,7 +6,7 @@ LL | unsafe impl SomeStruct { | | | unsafe because of this | - = note: only trait implementations may be annotated with unsafe + = note: only trait implementations may be annotated with `unsafe` error: aborting due to 1 previous error diff --git a/tests/ui/traits/syntax-trait-polarity.stderr b/tests/ui/traits/syntax-trait-polarity.stderr index a623cdbef3ac..8ffcdc7d8b55 100644 --- a/tests/ui/traits/syntax-trait-polarity.stderr +++ b/tests/ui/traits/syntax-trait-polarity.stderr @@ -5,6 +5,8 @@ LL | impl !TestType {} | -^^^^^^^^ inherent impl for this type | | | negative because of this + | + = note: only trait implementations may be annotated with `!` error: inherent impls cannot be negative --> $DIR/syntax-trait-polarity.rs:18:10 @@ -13,6 +15,8 @@ LL | impl !TestType2 {} | -^^^^^^^^^^^^ inherent impl for this type | | | negative because of this + | + = note: only trait implementations may be annotated with `!` error[E0198]: negative impls cannot be unsafe --> $DIR/syntax-trait-polarity.rs:12:13