Tweak trait modifier errors

This commit is contained in:
Cameron Steffen
2025-07-30 18:00:53 -05:00
parent fa733909ed
commit 3aa0ac0a8a
15 changed files with 50 additions and 55 deletions
+3 -3
View File
@@ -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<...>`
+5 -5
View File
@@ -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)]
+10 -19
View File
@@ -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)
}
+1 -1
View File
@@ -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
@@ -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!();
@@ -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`
@@ -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
@@ -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`
@@ -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() {}
@@ -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`
@@ -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`
@@ -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<dyn T> {}
//~^ ERROR mismatched types
//~| ERROR cannot be known at compilation time
@@ -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`
+1 -1
View File
@@ -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
@@ -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<T> !TestType2<T> {}
| -^^^^^^^^^^^^ 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