Skip to content

Commit aa465a5

Browse files
committed
Bail on any found recursion when expanding opaque types
Fixes rust-lang#87450. More of a bandaid because it does not fix the exponential complexity of the type folding used for opaque type expansion.
1 parent eba3228 commit aa465a5

File tree

1 file changed

+5
-1
lines changed
  • compiler/rustc_middle/src/ty

1 file changed

+5
-1
lines changed

compiler/rustc_middle/src/ty/util.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ impl<'tcx> TyCtxt<'tcx> {
540540
expanded_cache: FxHashMap::default(),
541541
primary_def_id: Some(def_id),
542542
found_recursion: false,
543+
found_any_recursion: false,
543544
check_recursion: true,
544545
tcx: self,
545546
};
@@ -560,6 +561,7 @@ struct OpaqueTypeExpander<'tcx> {
560561
expanded_cache: FxHashMap<(DefId, SubstsRef<'tcx>), Ty<'tcx>>,
561562
primary_def_id: Option<DefId>,
562563
found_recursion: bool,
564+
found_any_recursion: bool,
563565
/// Whether or not to check for recursive opaque types.
564566
/// This is `true` when we're explicitly checking for opaque type
565567
/// recursion, and 'false' otherwise to avoid unnecessary work.
@@ -569,7 +571,7 @@ struct OpaqueTypeExpander<'tcx> {
569571

570572
impl<'tcx> OpaqueTypeExpander<'tcx> {
571573
fn expand_opaque_ty(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> {
572-
if self.found_recursion {
574+
if self.found_any_recursion {
573575
return None;
574576
}
575577
let substs = substs.fold_with(self);
@@ -591,6 +593,7 @@ impl<'tcx> OpaqueTypeExpander<'tcx> {
591593
} else {
592594
// If another opaque type that we contain is recursive, then it
593595
// will report the error, so we don't have to.
596+
self.found_any_recursion = true;
594597
self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
595598
None
596599
}
@@ -1078,6 +1081,7 @@ pub fn normalize_opaque_types(
10781081
expanded_cache: FxHashMap::default(),
10791082
primary_def_id: None,
10801083
found_recursion: false,
1084+
found_any_recursion: false,
10811085
check_recursion: false,
10821086
tcx,
10831087
};

0 commit comments

Comments
 (0)