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

Merged
merged 1 commit into from
Jul 17, 2025

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 >.<

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, those are fine. I think we can address generated worst-case input code situations as they occur. Most likely we have enough ensure_sufficient_stack in place already.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 16, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jul 16, 2025

📌 Commit 28af500 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 16, 2025
Opaque type collection: Guard against endlessly recursing free alias types

See test description for technical details.

Fixes rust-lang#131994.

r? oli-obk (sry, your queue is large, so no rush & feel free to reassign)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 16, 2025
Opaque type collection: Guard against endlessly recursing free alias types

See test description for technical details.

Fixes rust-lang#131994.

r? oli-obk (sry, your queue is large, so no rush & feel free to reassign)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 16, 2025
Opaque type collection: Guard against endlessly recursing free alias types

See test description for technical details.

Fixes rust-lang#131994.

r? oli-obk (sry, your queue is large, so no rush & feel free to reassign)
bors added a commit that referenced this pull request Jul 16, 2025
Rollup of 11 pull requests

Successful merges:

 - #143595 (add `const_make_global`; err for `const_allocate` ptrs if didn't call)
 - #143678 (Added error for invalid char cast)
 - #143793 (Opaque type collection: Guard against endlessly recursing free alias types)
 - #143820 (Fixed a core crate compilation failure when enabling the `optimize_for_size` feature on some targets)
 - #143829 (Trim `BorrowedCursor` API)
 - #143856 (Linting public reexport of private dependencies)
 - #143891 (Port `#[coverage]` to the new attribute system)
 - #143914 (Reword mismatched-lifetime-syntaxes text based on feedback)
 - #143922 (Improve path segment joining)
 - #143926 (Remove deprecated fields in bootstrap)
 - #143975 (type_id_eq: check that the hash fully matches the type)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Jul 17, 2025
Rollup of 11 pull requests

Successful merges:

 - #143326 (Remove deprecated `Error::description` impl from `c_str::FromBytesWithNulError`)
 - #143431 (Use relative visibility when noting sealed trait to reduce false positive)
 - #143550 (resolve: Use interior mutability for extern module map)
 - #143631 (update to literal-escaper-0.0.5)
 - #143793 (Opaque type collection: Guard against endlessly recursing free alias types)
 - #143880 (tests: Test line debuginfo for linebreaked function parameters)
 - #143914 (Reword mismatched-lifetime-syntaxes text based on feedback)
 - #143926 (Remove deprecated fields in bootstrap)
 - #143955 (Make frame spans appear on a separate trace line)
 - #143975 (type_id_eq: check that the hash fully matches the type)
 - #143984 (Fix ice for feature-gated `cfg` attributes applied to the crate)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f78cc42 into rust-lang:master Jul 17, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Lazy Type Aliases (LTA) Jul 17, 2025
@rustbot rustbot added this to the 1.90.0 milestone Jul 17, 2025
rust-timer added a commit that referenced this pull request Jul 17, 2025
Rollup merge of #143793 - fmease:lta-opaq-inf-recur, r=oli-obk

Opaque type collection: Guard against endlessly recursing free alias types

See test description for technical details.

Fixes #131994.

r? oli-obk (sry, your queue is large, so no rush & feel free to reassign)
@fmease fmease deleted the lta-opaq-inf-recur branch July 17, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

crash: lazy type alias: stack overflow
4 participants