Skip to content

Fix TAIT & ATPIT feature gating in the presence of anon consts #139063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ impl<'a> PostExpansionVisitor<'a> {
}
visit::walk_ty(self, ty);
}

fn visit_anon_const(&mut self, _: &ast::AnonConst) -> Self::Result {
// We don't walk the anon const because it crosses a conceptual boundary: We're no
// longer "inside" the original type.
// Brittle: We assume that the callers of `check_impl_trait` will later recurse into
// the items found in the AnonConst to look for nested TyAliases.
}
}
ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ impl Mop {
//~| ERROR: unconstrained opaque type
}

fn funky(_: [(); {
impl Foo for fn() {
type Bar = impl Sized;
//~^ ERROR: `impl Trait` in associated types is unstable
//~| ERROR: unconstrained opaque type
}
0
}]) {}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ LL | type Bop = impl std::fmt::Debug;
= help: add `#![feature(impl_trait_in_assoc_type)]` 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 Trait` in associated types is unstable
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:22:20
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(impl_trait_in_assoc_type)]` 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]: inherent associated types are unstable
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:14:5
|
Expand All @@ -44,6 +54,14 @@ LL | type Bop = impl std::fmt::Debug;
|
= note: `Bop` must be used in combination with a concrete type within the same impl

error: aborting due to 5 previous errors
error: unconstrained opaque type
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:22:20
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
= note: `Bar` must be used in combination with a concrete type within the same impl

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0658`.
26 changes: 15 additions & 11 deletions tests/ui/feature-gates/feature-gate-type_alias_impl_trait.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
//@ check-pass
#![feature(type_alias_impl_trait)]
use std::fmt::Debug;

type Foo = impl Debug;
type Foo = impl Debug; //~ ERROR `impl Trait` in type aliases is unstable

struct Bar(Foo);
#[define_opaque(Foo)]
#[define_opaque(Foo)] //~ ERROR use of unstable library feature `type_alias_impl_trait`
fn define() -> Bar {
Bar(42)
}

type Foo2 = impl Debug;
type Foo2 = impl Debug; //~ ERROR `impl Trait` in type aliases is unstable

#[define_opaque(Foo2)]
#[define_opaque(Foo2)] //~ ERROR use of unstable library feature `type_alias_impl_trait`
fn define2() {
let x = || -> Foo2 { 42 };
}

type Foo3 = impl Debug;
type Foo3 = impl Debug; //~ ERROR `impl Trait` in type aliases is unstable

#[define_opaque(Foo3)]
#[define_opaque(Foo3)] //~ ERROR use of unstable library feature `type_alias_impl_trait`
fn define3(x: Foo3) {
let y: i32 = x;
}
#[define_opaque(Foo3)]
#[define_opaque(Foo3)] //~ ERROR use of unstable library feature `type_alias_impl_trait`
fn define3_1() {
define3(42)
}

type Foo4 = impl Debug;
type Foo4 = impl Debug; //~ ERROR `impl Trait` in type aliases is unstable

#[define_opaque(Foo4)]
#[define_opaque(Foo4)] //~ ERROR use of unstable library feature `type_alias_impl_trait`
fn define4(_: Foo4) {
let y: Foo4 = 42;
}

type Foo5 = [(); {
type Foo = impl Debug; //~ ERROR `impl Trait` in type aliases is unstable
//~^ ERROR unconstrained opaque type
0
}];

fn main() {}
111 changes: 111 additions & 0 deletions tests/ui/feature-gates/feature-gate-type_alias_impl_trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
error[E0658]: use of unstable library feature `type_alias_impl_trait`: `type_alias_impl_trait` has open design concerns
--> $DIR/feature-gate-type_alias_impl_trait.rs:6:3
|
LL | #[define_opaque(Foo)]
| ^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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]: use of unstable library feature `type_alias_impl_trait`: `type_alias_impl_trait` has open design concerns
--> $DIR/feature-gate-type_alias_impl_trait.rs:13:3
|
LL | #[define_opaque(Foo2)]
| ^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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]: use of unstable library feature `type_alias_impl_trait`: `type_alias_impl_trait` has open design concerns
--> $DIR/feature-gate-type_alias_impl_trait.rs:20:3
|
LL | #[define_opaque(Foo3)]
| ^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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]: use of unstable library feature `type_alias_impl_trait`: `type_alias_impl_trait` has open design concerns
--> $DIR/feature-gate-type_alias_impl_trait.rs:24:3
|
LL | #[define_opaque(Foo3)]
| ^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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]: use of unstable library feature `type_alias_impl_trait`: `type_alias_impl_trait` has open design concerns
--> $DIR/feature-gate-type_alias_impl_trait.rs:31:3
|
LL | #[define_opaque(Foo4)]
| ^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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 Trait` in type aliases is unstable
--> $DIR/feature-gate-type_alias_impl_trait.rs:3:12
|
LL | type Foo = impl Debug;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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 Trait` in type aliases is unstable
--> $DIR/feature-gate-type_alias_impl_trait.rs:11:13
|
LL | type Foo2 = impl Debug;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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 Trait` in type aliases is unstable
--> $DIR/feature-gate-type_alias_impl_trait.rs:18:13
|
LL | type Foo3 = impl Debug;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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 Trait` in type aliases is unstable
--> $DIR/feature-gate-type_alias_impl_trait.rs:29:13
|
LL | type Foo4 = impl Debug;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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 Trait` in type aliases is unstable
--> $DIR/feature-gate-type_alias_impl_trait.rs:37:16
|
LL | type Foo = impl Debug;
| ^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` 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: unconstrained opaque type
--> $DIR/feature-gate-type_alias_impl_trait.rs:37:16
|
LL | type Foo = impl Debug;
| ^^^^^^^^^^
|
= note: `Foo` must be used in combination with a concrete type within the same crate

error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0658`.
5 changes: 5 additions & 0 deletions tests/ui/impl-trait/impl_trait_projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ fn projection_from_impl_trait_inside_dyn_trait_is_disallowed()
panic!()
}

fn parametrized_value_in_anon_const_is_disallowed() -> [(); None::<impl Sized>] {
//~^ ERROR `impl Trait` is not allowed in paths
loop {}
}

fn main() {}
10 changes: 9 additions & 1 deletion tests/ui/impl-trait/impl_trait_projections.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ LL | -> <dyn Iterator<Item = impl Debug> as Iterator>::Item
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 4 previous errors
error[E0562]: `impl Trait` is not allowed in paths
--> $DIR/impl_trait_projections.rs:38:68
|
LL | fn parametrized_value_in_anon_const_is_disallowed() -> [(); None::<impl Sized>] {
| ^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0562`.
25 changes: 25 additions & 0 deletions tests/ui/impl-trait/inside-item-nested-in-anon-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Ensure we don't misclassify `impl Trait` as TAIT/ATPIT if located inside an anon const in a
// type alias/assoc type.
// issue: <https://github.com/rust-lang/rust/issues/139055>
//@ check-pass
#![forbid(unstable_features)]

struct Girder<const N: usize>;

type Alias = Girder<{
fn pass(input: impl Sized) -> impl Sized { input }
0
}>;

trait Trait {
type Assoc;
}

impl Trait for () {
type Assoc = [(); {
fn pass(input: impl Sized) -> impl Sized { input }
0
}];
}

fn main() {}
Loading