Skip to content

Commit e56ebf2

Browse files
committed
Make const bound handling more like types/regions.
Currently there is `Ty` and `BoundTy`, and `Region` and `BoundRegion`, and `Const` and... `BoundVar`. An annoying inconsistency. This commit repurposes the existing `BoundConst`, which was barely used, so it's the partner to `Const`. Unlike `BoundTy`/`BoundRegion` it lacks a `kind` field but it's still nice to have because it makes the const code more similar to the ty/region code everywhere. The commit also removes `impl From<BoundVar> for BoundTy`, which has a single use and doesn't seem worth it. These changes fix the "FIXME: We really should have a separate `BoundConst` for consts".
1 parent 81433fd commit e56ebf2

File tree

21 files changed

+82
-69
lines changed

21 files changed

+82
-69
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
187187
types: &mut |_bound_ty: ty::BoundTy| {
188188
unreachable!("we only replace regions in nll_relate, not types")
189189
},
190-
consts: &mut |_bound_var: ty::BoundVar| {
190+
consts: &mut |_bound_const: ty::BoundConst| {
191191
unreachable!("we only replace regions in nll_relate, not consts")
192192
},
193193
};
@@ -226,7 +226,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
226226
types: &mut |_bound_ty: ty::BoundTy| {
227227
unreachable!("we only replace regions in nll_relate, not types")
228228
},
229-
consts: &mut |_bound_var: ty::BoundVar| {
229+
consts: &mut |_bound_const: ty::BoundConst| {
230230
unreachable!("we only replace regions in nll_relate, not consts")
231231
},
232232
};

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,7 +2498,7 @@ fn param_env_with_gat_bounds<'tcx>(
24982498
ty::Const::new_bound(
24992499
tcx,
25002500
ty::INNERMOST,
2501-
ty::BoundVar::from_usize(bound_vars.len() - 1),
2501+
ty::BoundConst { var: ty::BoundVar::from_usize(bound_vars.len() - 1) },
25022502
)
25032503
.into()
25042504
}

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
189189
}
190190
ty::GenericArgKind::Const(ct) => {
191191
if let ty::ConstKind::Bound(ty::INNERMOST, bv) = ct.kind() {
192-
mapping.insert(bv, tcx.mk_param_from_def(param))
192+
mapping.insert(bv.var, tcx.mk_param_from_def(param))
193193
} else {
194194
return None;
195195
}
@@ -307,16 +307,16 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
307307
return ct;
308308
}
309309

310-
if let ty::ConstKind::Bound(binder, old_var) = ct.kind()
310+
if let ty::ConstKind::Bound(binder, old_bound) = ct.kind()
311311
&& self.binder == binder
312312
{
313-
let mapped = if let Some(mapped) = self.mapping.get(&old_var) {
313+
let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
314314
mapped.expect_const()
315315
} else {
316316
let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
317317
self.still_bound_vars.push(ty::BoundVariableKind::Const);
318-
let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, var);
319-
self.mapping.insert(old_var, mapped.into());
318+
let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, ty::BoundConst { var });
319+
self.mapping.insert(old_bound.var, mapped.into());
320320
mapped
321321
};
322322

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 't
10811081
ty::ConstKind::Param(param) => {
10821082
self.params.insert(param.index);
10831083
}
1084-
ty::ConstKind::Bound(db, ty::BoundVar { .. }) if db >= self.depth => {
1084+
ty::ConstKind::Bound(db, _) if db >= self.depth => {
10851085
let guar = self.cx.dcx().delayed_bug("unexpected escaping late-bound const var");
10861086
return ControlFlow::Break(guar);
10871087
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21072107
let name = tcx.item_name(param_def_id);
21082108
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
21092109
}
2110-
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
2111-
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
2112-
}
2110+
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => ty::Const::new_bound(
2111+
tcx,
2112+
debruijn,
2113+
ty::BoundConst { var: ty::BoundVar::from_u32(index) },
2114+
),
21132115
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
21142116
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
21152117
}

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
752752
) -> Ty<'tcx> {
753753
debug_assert!(!self.infcx.is_some_and(|infcx| ty_var != infcx.shallow_resolve(ty_var)));
754754
let var = self.canonical_var(var_kind, ty_var.into());
755-
Ty::new_bound(self.tcx, self.binder_index, var.into())
755+
let bt = ty::BoundTy { var, kind: ty::BoundTyKind::Anon };
756+
Ty::new_bound(self.tcx, self.binder_index, bt)
756757
}
757758

758759
/// Given a type variable `const_var` of the given kind, first check
@@ -768,6 +769,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
768769
!self.infcx.is_some_and(|infcx| ct_var != infcx.shallow_resolve_const(ct_var))
769770
);
770771
let var = self.canonical_var(var_kind, ct_var.into());
771-
ty::Const::new_bound(self.tcx, self.binder_index, var)
772+
let bc = ty::BoundConst { var };
773+
ty::Const::new_bound(self.tcx, self.binder_index, bc)
772774
}
773775
}

compiler/rustc_infer/src/infer/canonical/instantiate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for CanonicalInstantiator<'tcx> {
133133
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
134134
match ct.kind() {
135135
ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
136-
self.var_values[bound_const.as_usize()].expect_const()
136+
self.var_values[bound_const.var.as_usize()].expect_const()
137137
}
138138
_ => ct.super_fold_with(self),
139139
}
@@ -217,7 +217,7 @@ fn highest_var_in_clauses<'tcx>(c: ty::Clauses<'tcx>) -> usize {
217217
if let ty::ConstKind::Bound(debruijn, bound_const) = ct.kind()
218218
&& debruijn == self.current_index
219219
{
220-
self.max_var = self.max_var.max(bound_const.as_usize());
220+
self.max_var = self.max_var.max(bound_const.var.as_usize());
221221
} else if ct.has_vars_bound_at_or_above(self.current_index) {
222222
ct.super_visit_with(self);
223223
}

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ impl<'tcx> InferCtxt<'tcx> {
433433
}
434434
GenericArgKind::Lifetime(result_value) => {
435435
// e.g., here `result_value` might be `'?1` in the example above...
436-
if let ty::ReBound(debruijn, br) = result_value.kind() {
436+
if let ty::ReBound(debruijn, b) = result_value.kind() {
437437
// ... in which case we would set `canonical_vars[0]` to `Some('static)`.
438438

439439
// We only allow a `ty::INNERMOST` index in generic parameters.
440440
assert_eq!(debruijn, ty::INNERMOST);
441-
opt_values[br.var] = Some(*original_value);
441+
opt_values[b.var] = Some(*original_value);
442442
}
443443
}
444444
GenericArgKind::Const(result_value) => {
@@ -447,7 +447,7 @@ impl<'tcx> InferCtxt<'tcx> {
447447

448448
// We only allow a `ty::INNERMOST` index in generic parameters.
449449
assert_eq!(debruijn, ty::INNERMOST);
450-
opt_values[b] = Some(*original_value);
450+
opt_values[b.var] = Some(*original_value);
451451
}
452452
}
453453
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,8 @@ impl<'tcx> InferCtxt<'tcx> {
12081208
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
12091209
self.args[bt.var.index()].expect_ty()
12101210
}
1211-
fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
1212-
self.args[bv.index()].expect_const()
1211+
fn replace_const(&mut self, bc: ty::BoundConst) -> ty::Const<'tcx> {
1212+
self.args[bc.var.index()].expect_const()
12131213
}
12141214
}
12151215
let delegate = ToFreshVars { args };

compiler/rustc_infer/src/infer/relate/higher_ranked.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ impl<'tcx> InferCtxt<'tcx> {
4545
ty::PlaceholderType { universe: next_universe, bound: bound_ty },
4646
)
4747
},
48-
consts: &mut |bound_var: ty::BoundVar| {
48+
consts: &mut |bound_const: ty::BoundConst| {
4949
ty::Const::new_placeholder(
5050
self.tcx,
51-
ty::PlaceholderConst { universe: next_universe, bound: bound_var },
51+
ty::PlaceholderConst { universe: next_universe, bound: bound_const },
5252
)
5353
},
5454
};

0 commit comments

Comments
 (0)