Skip to content

Commit 8642a23

Browse files
committed
Adjust bound tys indices in canonicalization
1 parent 9049c85 commit 8642a23

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

src/librustc/infer/canonical/canonicalizer.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,32 @@ struct Canonicalizer<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
228228
indices: FxHashMap<Kind<'tcx>, BoundVar>,
229229
canonicalize_region_mode: &'cx dyn CanonicalizeRegionMode,
230230
needs_canonical_flags: TypeFlags,
231+
232+
binder_index: ty::DebruijnIndex,
231233
}
232234

233235
impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx> {
234236
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
235237
self.tcx
236238
}
237239

240+
fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
241+
where T: TypeFoldable<'tcx>
242+
{
243+
self.binder_index.shift_in(1);
244+
let t = t.super_fold_with(self);
245+
self.binder_index.shift_out(1);
246+
t
247+
}
248+
238249
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
239250
match *r {
240-
ty::ReLateBound(..) => {
241-
// leave bound regions alone
242-
r
251+
ty::ReLateBound(index, ..) => {
252+
if index >= self.binder_index {
253+
bug!("escaping late bound region during canonicalization")
254+
} else {
255+
r
256+
}
243257
}
244258

245259
ty::ReVar(vid) => {
@@ -283,8 +297,12 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
283297
bug!("encountered a fresh type during canonicalization")
284298
}
285299

286-
ty::Bound(_) => {
287-
bug!("encountered a bound type during canonicalization")
300+
ty::Bound(bound_ty) => {
301+
if bound_ty.index >= self.binder_index {
302+
bug!("escaping bound type during canonicalization")
303+
} else {
304+
t
305+
}
288306
}
289307

290308
ty::Closure(..)
@@ -367,6 +385,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
367385
variables: SmallVec::new(),
368386
query_state,
369387
indices: FxHashMap::default(),
388+
binder_index: ty::INNERMOST,
370389
};
371390
let out_value = value.fold_with(&mut canonicalizer);
372391

@@ -469,7 +488,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
469488
kind: CanonicalVarKind::Ty(ty_kind),
470489
};
471490
let var = self.canonical_var(info, ty_var.into());
472-
self.tcx().mk_ty(ty::Bound(BoundTy::new(ty::INNERMOST, var)))
491+
self.tcx().mk_ty(ty::Bound(BoundTy::new(self.binder_index, var)))
473492
}
474493
}
475494
}

src/librustc/infer/canonical/query_response.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
418418
UnpackedKind::Type(result_value) => {
419419
// e.g., here `result_value` might be `?0` in the example above...
420420
if let ty::Bound(b) = result_value.sty {
421+
assert_eq!(b.index, ty::INNERMOST);
421422
// in which case we would set `canonical_vars[0]` to `Some(?U)`.
422423
opt_values[b.var] = Some(*original_value);
423424
}

src/librustc/infer/canonical/substitute.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,48 @@ where
6969
} else if !value.has_type_flags(TypeFlags::HAS_CANONICAL_VARS) {
7070
value.clone()
7171
} else {
72-
value.fold_with(&mut CanonicalVarValuesSubst { tcx, var_values })
72+
value.fold_with(&mut CanonicalVarValuesSubst {
73+
tcx,
74+
var_values,
75+
binder_index: ty::INNERMOST,
76+
})
7377
}
7478
}
7579

7680
struct CanonicalVarValuesSubst<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
7781
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
7882
var_values: &'cx CanonicalVarValues<'tcx>,
83+
binder_index: ty::DebruijnIndex,
7984
}
8085

8186
impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for CanonicalVarValuesSubst<'cx, 'gcx, 'tcx> {
8287
fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
8388
self.tcx
8489
}
8590

91+
fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
92+
where T: TypeFoldable<'tcx>
93+
{
94+
self.binder_index.shift_in(1);
95+
let t = t.super_fold_with(self);
96+
self.binder_index.shift_out(1);
97+
t
98+
}
99+
86100
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
87101
match t.sty {
88102
ty::Bound(b) => {
89-
match self.var_values.var_values[b.var].unpack() {
90-
UnpackedKind::Type(ty) => ty,
91-
r => bug!("{:?} is a type but value is {:?}", b, r),
103+
if b.index == self.binder_index {
104+
match self.var_values.var_values[b.var].unpack() {
105+
UnpackedKind::Type(ty) => ty::fold::shift_vars(
106+
self.tcx,
107+
self.binder_index.index() as u32,
108+
&ty
109+
),
110+
r => bug!("{:?} is a type but value is {:?}", b, r),
111+
}
112+
} else {
113+
t
92114
}
93115
}
94116
_ => {

src/librustc/ty/subst.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,10 @@ impl CanonicalUserSubsts<'tcx> {
556556
self.value.substs.iter().zip(BoundVar::new(0)..).all(|(kind, cvar)| {
557557
match kind.unpack() {
558558
UnpackedKind::Type(ty) => match ty.sty {
559-
ty::Bound(ref b) => cvar == b.var,
559+
ty::Bound(ref b) => {
560+
assert_eq!(b.index, ty::INNERMOST);
561+
cvar == b.var
562+
}
560563
_ => false,
561564
},
562565

0 commit comments

Comments
 (0)