Skip to content

Opaque type collection: Guard against endlessly recursing free alias types #143793

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
}
// Skips type aliases, as they are meant to be transparent.
// FIXME(type_alias_impl_trait): can we require mentioning nested type aliases explicitly?
ty::Alias(ty::Free, alias_ty) if alias_ty.def_id.is_local() => {
ty::Alias(ty::Free, alias_ty) if let Some(def_id) = alias_ty.def_id.as_local() => {
if !self.seen.insert(def_id) {
Copy link
Member Author

@fmease fmease Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, this only guards against cyclic alias types, not against "overly deep" ones. It's within the realm of possibility that this still overflows the stack on inputs like type T${n} = T${n+1}; type T${N} = (); where 0 ≤ n < N where N is large (like >10_000 which is the maximum I tested and which took a while to compile but didn't exhibit a stack overflow).

However, rustc struggles on such inputs anyway today whether eager or lazy (I already found a bunch of boring hangs).

Keeping track of depth instead of visited types would address that but I didn't want to make large modifications to this collector. I thought about utilizing tcx.expand_free_alias_tys in appropriate places which can deal with such inputs. However, that might be incompatible with // FIXME(type_alias_impl_trait): can we require mentioning nested type aliases explicitly? which I guess is referring to constructions like:

#![feature(type_alias_impl_trait)]
// #[define_opaque(Inner)] // <-- (?)
type Outer = Inner;
type Inner = impl Sized;
// ...

or sth. like that >.<

return;
}
self.tcx
.type_of(alias_ty.def_id)
.instantiate(self.tcx, alias_ty.args)
Expand Down Expand Up @@ -256,16 +259,16 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
return;
}

let impl_args = alias_ty.args.rebase_onto(
let alias_args = alias_ty.args.rebase_onto(
self.tcx,
impl_trait_ref.def_id,
ty::GenericArgs::identity_for_item(self.tcx, parent),
);

if self.tcx.check_args_compatible(assoc.def_id, impl_args) {
if self.tcx.check_args_compatible(assoc.def_id, alias_args) {
self.tcx
.type_of(assoc.def_id)
.instantiate(self.tcx, impl_args)
.instantiate(self.tcx, alias_args)
.visit_with(self);
return;
} else {
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/lazy-type-alias/opaq-ty-collection-infinite-recur.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The opaque type collector used to expand free alias types (in situ) without guarding against
// endlessly recursing aliases which lead to the compiler overflowing its stack in certain
// situations.
//
// In most situations we wouldn't even reach the collector when there's an overflow because we
// would've already bailed out early during the item's wfcheck due to the normalization failure.
//
// In the case below however, while collecting the opaque types defined by the AnonConst, we
// descend into its nested items (here: type alias `Recur`) to acquire their opaque types --
// meaning we get there before we wfcheck `Recur`.
//
// issue: <https://github.com/rust-lang/rust/issues/131994>
#![feature(lazy_type_alias)]
#![expect(incomplete_features)]

struct Hold([(); { type Recur = Recur; 0 }]); //~ ERROR overflow normalizing the type alias `Recur`

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/lazy-type-alias/opaq-ty-collection-infinite-recur.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0275]: overflow normalizing the type alias `Recur`
--> $DIR/opaq-ty-collection-infinite-recur.rs:16:20
|
LL | struct Hold([(); { type Recur = Recur; 0 }]);
| ^^^^^^^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0275`.
Loading