Skip to content

Commit 64be8bb

Browse files
committed
Check consts in ValidateBoundVars.
Alongside the existing type and region checking.
1 parent 507dec4 commit 64be8bb

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundConst {
977977
self.var
978978
}
979979

980-
fn assert_eq(self, _var: ty::BoundVariableKind) {
981-
unreachable!()
980+
fn assert_eq(self, var: ty::BoundVariableKind) {
981+
var.expect_const()
982982
}
983983
}
984984

compiler/rustc_type_ir/src/binder.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ impl<I: Interner, T: IntoIterator> Binder<I, T> {
274274
pub struct ValidateBoundVars<I: Interner> {
275275
bound_vars: I::BoundVarKinds,
276276
binder_index: ty::DebruijnIndex,
277-
// We may encounter the same variable at different levels of binding, so
278-
// this can't just be `Ty`
277+
// We only cache types because any complex const will have to step through
278+
// a type at some point anyways. We may encounter the same variable at
279+
// different levels of binding, so this can't just be `Ty`.
279280
visited: SsoHashSet<(ty::DebruijnIndex, I::Ty)>,
280281
}
281282

@@ -319,6 +320,24 @@ impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
319320
t.super_visit_with(self)
320321
}
321322

323+
fn visit_const(&mut self, c: I::Const) -> Self::Result {
324+
if c.outer_exclusive_binder() < self.binder_index {
325+
return ControlFlow::Break(());
326+
}
327+
match c.kind() {
328+
ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.binder_index => {
329+
let idx = bound_const.var().as_usize();
330+
if self.bound_vars.len() <= idx {
331+
panic!("Not enough bound vars: {:?} not found in {:?}", c, self.bound_vars);
332+
}
333+
bound_const.assert_eq(self.bound_vars.get(idx).unwrap());
334+
}
335+
_ => {}
336+
};
337+
338+
c.super_visit_with(self)
339+
}
340+
322341
fn visit_region(&mut self, r: I::Region) -> Self::Result {
323342
match r.kind() {
324343
ty::ReBound(index, br) if index == self.binder_index => {

0 commit comments

Comments
 (0)