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

Conversation

fmease
Copy link
Member

@fmease fmease commented Jul 11, 2025

See test description for technical details.

Fixes #131994.

r? oli-obk (sry, your queue is large, so no rush & feel free to reassign)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 11, 2025
@fmease fmease moved this to In Progress in Lazy Type Aliases (LTA) Jul 11, 2025
@@ -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 >.<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

crash: lazy type alias: stack overflow
3 participants