From 3dcefa371d82c43c9a98212aa86b68fa214e8fbd Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Mon, 23 Feb 2026 14:12:19 +0900 Subject: [PATCH] Trait aliases cannot be `impl`-restricted --- compiler/rustc_parse/src/errors.rs | 8 + compiler/rustc_parse/src/parser/item.rs | 3 + .../trait-alias-cannot-be-impl-restricted.rs | 28 ++++ ...cannot-be-impl-restricted.with_gate.stderr | 83 ++++++++++ ...not-be-impl-restricted.without_gate.stderr | 145 ++++++++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.rs create mode 100644 tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.with_gate.stderr create mode 100644 tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.without_gate.stderr diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 15e7fe805845..4b648b3464a8 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -2422,6 +2422,14 @@ pub(crate) struct TraitAliasCannotBeUnsafe { pub span: Span, } +#[derive(Diagnostic)] +#[diag("trait aliases cannot be `impl`-restricted")] +pub(crate) struct TraitAliasCannotBeImplRestricted { + #[primary_span] + #[label("trait aliases cannot be `impl`-restricted")] + pub span: Span, +} + #[derive(Diagnostic)] #[diag("associated `static` items are not allowed")] pub(crate) struct AssociatedStaticItemNotAllowed { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index c3b8aac220a8..db5be5feaeb6 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1142,6 +1142,9 @@ fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, Ite if let Safety::Unsafe(_) = safety { self.dcx().emit_err(errors::TraitAliasCannotBeUnsafe { span: whole_span }); } + if let RestrictionKind::Restricted { .. } = impl_restriction.kind { + self.dcx().emit_err(errors::TraitAliasCannotBeImplRestricted { span: whole_span }); + } self.psess.gated_spans.gate(sym::trait_alias, whole_span); diff --git a/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.rs b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.rs new file mode 100644 index 000000000000..62622e1d5586 --- /dev/null +++ b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.rs @@ -0,0 +1,28 @@ +//@ compile-flags: --crate-type=lib +//@ revisions: with_gate without_gate +#![cfg_attr(with_gate, feature(impl_restriction))] +//[with_gate]~^ WARN the feature `impl_restriction` is incomplete and may not be safe to use and/or cause compiler crashes +#![feature(auto_traits, const_trait_impl, trait_alias)] + +impl(crate) trait Alias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted +//[without_gate]~^ ERROR `impl` restrictions are experimental +auto impl(in crate) trait AutoAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted +//~^ ERROR trait aliases cannot be `auto` +//[without_gate]~| ERROR `impl` restrictions are experimental +unsafe impl(self) trait UnsafeAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted +//~^ ERROR trait aliases cannot be `unsafe` +//[without_gate]~| ERROR `impl` restrictions are experimental +const impl(in self) trait ConstAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted +//[without_gate]~^ ERROR `impl` restrictions are experimental + +mod foo { + impl(super) trait InnerAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted + //[without_gate]~^ ERROR `impl` restrictions are experimental + const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted + //~^ ERROR trait aliases cannot be `unsafe` + //[without_gate]~| ERROR `impl` restrictions are experimental + unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; //~ ERROR trait aliases cannot be `impl`-restricted + //~^ ERROR trait aliases cannot be `auto` + //~^^ ERROR trait aliases cannot be `unsafe` + //[without_gate]~| ERROR `impl` restrictions are experimental +} diff --git a/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.with_gate.stderr b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.with_gate.stderr new file mode 100644 index 000000000000..70287aca42aa --- /dev/null +++ b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.with_gate.stderr @@ -0,0 +1,83 @@ +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:7:1 + | +LL | impl(crate) trait Alias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `auto` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:9:1 + | +LL | auto impl(in crate) trait AutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `auto` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:9:1 + | +LL | auto impl(in crate) trait AutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:12:1 + | +LL | unsafe impl(self) trait UnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:12:1 + | +LL | unsafe impl(self) trait UnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:15:1 + | +LL | const impl(in self) trait ConstAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:19:5 + | +LL | impl(super) trait InnerAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:21:5 + | +LL | const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:21:5 + | +LL | const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `auto` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `auto` + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +warning: the feature `impl_restriction` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:3:32 + | +LL | #![cfg_attr(with_gate, feature(impl_restriction))] + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = note: `#[warn(incomplete_features)]` on by default + +error: aborting due to 12 previous errors; 1 warning emitted + diff --git a/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.without_gate.stderr b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.without_gate.stderr new file mode 100644 index 000000000000..7bff90396708 --- /dev/null +++ b/tests/ui/impl-restriction/trait-alias-cannot-be-impl-restricted.without_gate.stderr @@ -0,0 +1,145 @@ +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:7:1 + | +LL | impl(crate) trait Alias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `auto` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:9:1 + | +LL | auto impl(in crate) trait AutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `auto` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:9:1 + | +LL | auto impl(in crate) trait AutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:12:1 + | +LL | unsafe impl(self) trait UnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:12:1 + | +LL | unsafe impl(self) trait UnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:15:1 + | +LL | const impl(in self) trait ConstAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:19:5 + | +LL | impl(super) trait InnerAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:21:5 + | +LL | const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:21:5 + | +LL | const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error: trait aliases cannot be `auto` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `auto` + +error: trait aliases cannot be `unsafe` + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: trait aliases cannot be `impl`-restricted + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:5 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `impl`-restricted + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:7:1 + | +LL | impl(crate) trait Alias = Copy; + | ^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:9:6 + | +LL | auto impl(in crate) trait AutoAlias = Copy; + | ^^^^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:12:8 + | +LL | unsafe impl(self) trait UnsafeAlias = Copy; + | ^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:15:7 + | +LL | const impl(in self) trait ConstAlias = Copy; + | ^^^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:19:5 + | +LL | impl(super) trait InnerAlias = Copy; + | ^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:21:18 + | +LL | const unsafe impl(in crate::foo) trait InnerConstUnsafeAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: `impl` restrictions are experimental + --> $DIR/trait-alias-cannot-be-impl-restricted.rs:24:17 + | +LL | unsafe auto impl(in crate::foo) trait InnerUnsafeAutoAlias = Copy; + | ^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #105077 for more information + = help: add `#![feature(impl_restriction)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 19 previous errors + +For more information about this error, try `rustc --explain E0658`.