Skip to content

Commit 0a606b9

Browse files
committed
Check consts in ValidateBoundVars.
Alongside the existing type and region checking.
1 parent ade381e commit 0a606b9

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
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: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,17 @@ 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`
279-
visited: SsoHashSet<(ty::DebruijnIndex, I::Ty)>,
277+
// A cache for types. We may encounter the same variable at different levels of binding, so
278+
// this can't just be `Ty`.
279+
visited_tys: SsoHashSet<(ty::DebruijnIndex, I::Ty)>,
280280
}
281281

282282
impl<I: Interner> ValidateBoundVars<I> {
283283
pub fn new(bound_vars: I::BoundVarKinds) -> Self {
284284
ValidateBoundVars {
285285
bound_vars,
286286
binder_index: ty::INNERMOST,
287-
visited: SsoHashSet::default(),
287+
visited_tys: SsoHashSet::default(),
288288
}
289289
}
290290
}
@@ -301,7 +301,7 @@ impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
301301

302302
fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
303303
if t.outer_exclusive_binder() < self.binder_index
304-
|| !self.visited.insert((self.binder_index, t))
304+
|| !self.visited_tys.insert((self.binder_index, t))
305305
{
306306
return ControlFlow::Break(());
307307
}
@@ -319,6 +319,24 @@ impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
319319
t.super_visit_with(self)
320320
}
321321

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

0 commit comments

Comments
 (0)