Move trait impl modifier errors to parsing

This is a technically a breaking change for what can be parsed in
`#[cfg(false)]`.
This commit is contained in:
Cameron Steffen
2025-07-30 16:33:59 -05:00
parent 39c5d6d1ca
commit fa733909ed
10 changed files with 123 additions and 78 deletions

View File

@@ -175,11 +175,6 @@ ast_passes_generic_default_trailing = generic parameters with a default must be
ast_passes_incompatible_features = `{$f1}` and `{$f2}` are incompatible, using them at the same time is not allowed
.help = remove one of these features
ast_passes_inherent_cannot_be = inherent impls cannot be {$annotation}
.because = {$annotation} because of this
.type = inherent impl for this type
.only_trait = only trait implementations may be annotated with {$annotation}
ast_passes_item_invalid_safety = items outside of `unsafe extern {"{ }"}` cannot be declared with `safe` safety qualifier
.suggestion = remove safe from this item

View File

@@ -26,7 +26,7 @@ use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list}
use rustc_ast::*;
use rustc_ast_pretty::pprust::{self, State};
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{DiagCtxtHandle, E0197};
use rustc_errors::DiagCtxtHandle;
use rustc_feature::Features;
use rustc_parse::validate_attr;
use rustc_session::Session;
@@ -993,49 +993,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
});
}
ItemKind::Impl(box Impl {
safety,
polarity,
defaultness,
constness,
safety: _,
polarity: _,
defaultness: _,
constness: _,
generics,
of_trait: None,
self_ty,
items,
}) => {
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
span: self_ty.span,
annotation_span,
annotation,
self_ty: self_ty.span,
only_trait,
};
self.visit_attrs_vis(&item.attrs, &item.vis);
self.visibility_not_permitted(
&item.vis,
errors::VisibilityNotPermittedNote::IndividualImplItems,
);
if let &Safety::Unsafe(span) = safety {
self.dcx()
.create_err(errors::InherentImplCannot {
span: self_ty.span,
annotation_span: span,
annotation: "unsafe",
self_ty: self_ty.span,
only_trait: true,
})
.with_code(E0197)
.emit();
}
if let &ImplPolarity::Negative(span) = polarity {
self.dcx().emit_err(error(span, "negative", false));
}
if let &Defaultness::Default(def_span) = defaultness {
self.dcx().emit_err(error(def_span, "`default`", true));
}
if let &Const::Yes(span) = constness {
self.dcx().emit_err(error(span, "`const`", true));
}
self.with_tilde_const(Some(TildeConstReason::Impl { span: item.span }), |this| {
this.visit_generics(generics)

View File

@@ -464,20 +464,6 @@ pub(crate) struct UnsafeNegativeImpl {
pub r#unsafe: Span,
}
#[derive(Diagnostic)]
#[diag(ast_passes_inherent_cannot_be)]
pub(crate) struct InherentImplCannot<'a> {
#[primary_span]
pub span: Span,
#[label(ast_passes_because)]
pub annotation_span: Span,
pub annotation: &'a str,
#[label(ast_passes_type)]
pub self_ty: Span,
#[note(ast_passes_only_trait)]
pub only_trait: bool,
}
#[derive(Diagnostic)]
#[diag(ast_passes_unsafe_item)]
pub(crate) struct UnsafeItem {

View File

@@ -869,6 +869,11 @@ 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
.type = inherent impl for this type
.only_trait = only trait implementations may be annotated with {$annotation}
parse_transpose_dyn_or_impl = `for<...>` expected after `{$kw}`, not before
.suggestion = move `{$kw}` before the `for<...>`

View File

@@ -71,6 +71,20 @@ pub(crate) struct BadQPathStage2 {
pub wrap: WrapType,
}
#[derive(Diagnostic)]
#[diag(parse_trait_impl_modifier_in_inherent_impl)]
pub(crate) struct TraitImplModifierInInherentImpl<'a> {
#[primary_span]
pub span: Span,
#[label(parse_because)]
pub annotation_span: Span,
pub annotation: &'a str,
#[label(parse_type)]
pub self_ty: Span,
#[note(parse_only_trait)]
pub only_trait: bool,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
pub(crate) struct WrapType {

View File

@@ -665,7 +665,41 @@ impl<'a> Parser<'a> {
(Some(trait_ref), ty_second)
}
None => (None, ty_first), // impl Type
None => {
let self_ty = ty_first;
let error = |annotation_span, annotation, only_trait| {
errors::TraitImplModifierInInherentImpl {
span: self_ty.span,
annotation_span,
annotation,
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();
}
if let ImplPolarity::Negative(span) = polarity {
self.dcx().emit_err(error(span, "negative", false));
}
if let Defaultness::Default(def_span) = defaultness {
self.dcx().emit_err(error(def_span, "`default`", true));
}
if let Const::Yes(span) = constness {
self.dcx().emit_err(error(span, "`const`", true));
}
(None, self_ty)
}
};
Ok(ItemKind::Impl(Box::new(Impl {
safety,

View File

@@ -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 {}
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 @@ extern "C" {
//~^ 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 {}
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 {}
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 {}
default impl foo {} //~ ERROR inherent impls cannot be `default`
//~^ ERROR implementation is not supported in `trait`s or `impl`s
default!();
default::foo::bar!();

View File

@@ -78,6 +78,16 @@ LL | default trait foo = Ord;
|
= note: only associated `fn`, `const`, and `type` items can 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
|
= note: only trait implementations may be annotated with `default`
error: an item macro invocation cannot be `default`
--> $DIR/default-on-wrong-item-kind.rs:25:5
|
@@ -275,6 +285,16 @@ LL | default trait foo = Ord;
|
= help: consider moving the trait alias out to a nearby module scope
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
|
= note: only trait implementations may be annotated with `default`
error: implementation is not supported in `extern` blocks
--> $DIR/default-on-wrong-item-kind.rs:56:5
|
@@ -489,6 +509,16 @@ LL | default trait foo = Ord;
|
= help: consider moving the trait alias out to a nearby module scope
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
|
= note: only trait implementations may be annotated with `default`
error: implementation is not supported in `trait`s or `impl`s
--> $DIR/default-on-wrong-item-kind.rs:93:5
|
@@ -703,6 +733,16 @@ LL | default trait foo = Ord;
|
= help: consider moving the trait alias out to a nearby module scope
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
|
= note: only trait implementations may be annotated with `default`
error: implementation is not supported in `trait`s or `impl`s
--> $DIR/default-on-wrong-item-kind.rs:130:5
|
@@ -759,5 +799,5 @@ LL | default macro_rules! foo {}
|
= help: consider moving the macro definition out to a nearby module scope
error: aborting due to 95 previous errors
error: aborting due to 99 previous errors

View File

@@ -1,14 +1,3 @@
error: macro expansion ignores keyword `dyn` and any tokens following
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:14:31
|
LL | (dyn $c:ident Trait) => { dyn $c Trait {} };
| ^^^
...
LL | demo! { dyn const Trait }
| ------------------------- caused by the macro expansion here
|
= note: the usage of `demo!` is likely invalid in item context
error: inherent impls cannot be `const`
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:10:40
|
@@ -24,6 +13,17 @@ LL | demo! { impl const Trait }
= note: only trait implementations may be annotated with `const`
= note: this error originates in the macro `demo` (in Nightly builds, run with -Z macro-backtrace for more info)
error: macro expansion ignores keyword `dyn` and any tokens following
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:14:31
|
LL | (dyn $c:ident Trait) => { dyn $c Trait {} };
| ^^^
...
LL | demo! { dyn const Trait }
| ------------------------- caused by the macro expansion here
|
= note: the usage of `demo!` is likely invalid in item context
error[E0658]: const trait impls are experimental
--> $DIR/macro-const-trait-bound-theoretical-regression.rs:17:14
|

View File

@@ -6,6 +6,14 @@ LL | impl !TestType {}
| |
| negative because of this
error: inherent impls cannot be negative
--> $DIR/syntax-trait-polarity.rs:18:10
|
LL | impl<T> !TestType2<T> {}
| -^^^^^^^^^^^^ inherent impl for this type
| |
| negative because of this
error[E0198]: negative impls cannot be unsafe
--> $DIR/syntax-trait-polarity.rs:12:13
|
@@ -15,14 +23,6 @@ LL | unsafe impl !Send for TestType {}
| | negative because of this
| unsafe because of this
error: inherent impls cannot be negative
--> $DIR/syntax-trait-polarity.rs:18:10
|
LL | impl<T> !TestType2<T> {}
| -^^^^^^^^^^^^ inherent impl for this type
| |
| negative because of this
error[E0198]: negative impls cannot be unsafe
--> $DIR/syntax-trait-polarity.rs:21:16
|