-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
fmease
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
fmease:lta-opaq-inf-recur
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/ui/lazy-type-alias/opaq-ty-collection-infinite-recur.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
tests/ui/lazy-type-alias/opaq-ty-collection-infinite-recur.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
whereN
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:or sth. like that >.<