Skip to content

Commit be6056c

Browse files
authored
Unrolled build for #143793
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)
2 parents 8c12d76 + 28af500 commit be6056c

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
223223
}
224224
// Skips type aliases, as they are meant to be transparent.
225225
// FIXME(type_alias_impl_trait): can we require mentioning nested type aliases explicitly?
226-
ty::Alias(ty::Free, alias_ty) if alias_ty.def_id.is_local() => {
226+
ty::Alias(ty::Free, alias_ty) if let Some(def_id) = alias_ty.def_id.as_local() => {
227+
if !self.seen.insert(def_id) {
228+
return;
229+
}
227230
self.tcx
228231
.type_of(alias_ty.def_id)
229232
.instantiate(self.tcx, alias_ty.args)
@@ -256,16 +259,16 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
256259
return;
257260
}
258261

259-
let impl_args = alias_ty.args.rebase_onto(
262+
let alias_args = alias_ty.args.rebase_onto(
260263
self.tcx,
261264
impl_trait_ref.def_id,
262265
ty::GenericArgs::identity_for_item(self.tcx, parent),
263266
);
264267

265-
if self.tcx.check_args_compatible(assoc.def_id, impl_args) {
268+
if self.tcx.check_args_compatible(assoc.def_id, alias_args) {
266269
self.tcx
267270
.type_of(assoc.def_id)
268-
.instantiate(self.tcx, impl_args)
271+
.instantiate(self.tcx, alias_args)
269272
.visit_with(self);
270273
return;
271274
} else {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// The opaque type collector used to expand free alias types (in situ) without guarding against
2+
// endlessly recursing aliases which lead to the compiler overflowing its stack in certain
3+
// situations.
4+
//
5+
// In most situations we wouldn't even reach the collector when there's an overflow because we
6+
// would've already bailed out early during the item's wfcheck due to the normalization failure.
7+
//
8+
// In the case below however, while collecting the opaque types defined by the AnonConst, we
9+
// descend into its nested items (here: type alias `Recur`) to acquire their opaque types --
10+
// meaning we get there before we wfcheck `Recur`.
11+
//
12+
// issue: <https://github.com/rust-lang/rust/issues/131994>
13+
#![feature(lazy_type_alias)]
14+
#![expect(incomplete_features)]
15+
16+
struct Hold([(); { type Recur = Recur; 0 }]); //~ ERROR overflow normalizing the type alias `Recur`
17+
18+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0275]: overflow normalizing the type alias `Recur`
2+
--> $DIR/opaq-ty-collection-infinite-recur.rs:16:20
3+
|
4+
LL | struct Hold([(); { type Recur = Recur; 0 }]);
5+
| ^^^^^^^^^^
6+
|
7+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)