Skip to content

Improve bound const handling #144677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
types: &mut |_bound_ty: ty::BoundTy| {
unreachable!("we only replace regions in nll_relate, not types")
},
consts: &mut |_bound_var: ty::BoundVar| {
consts: &mut |_bound_const: ty::BoundConst| {
unreachable!("we only replace regions in nll_relate, not consts")
},
};
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
types: &mut |_bound_ty: ty::BoundTy| {
unreachable!("we only replace regions in nll_relate, not types")
},
consts: &mut |_bound_var: ty::BoundVar| {
consts: &mut |_bound_const: ty::BoundConst| {
unreachable!("we only replace regions in nll_relate, not consts")
},
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2498,7 +2498,7 @@ fn param_env_with_gat_bounds<'tcx>(
ty::Const::new_bound(
tcx,
ty::INNERMOST,
ty::BoundVar::from_usize(bound_vars.len() - 1),
ty::BoundConst { var: ty::BoundVar::from_usize(bound_vars.len() - 1) },
)
.into()
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
}
ty::GenericArgKind::Const(ct) => {
if let ty::ConstKind::Bound(ty::INNERMOST, bv) = ct.kind() {
mapping.insert(bv, tcx.mk_param_from_def(param))
mapping.insert(bv.var, tcx.mk_param_from_def(param))
} else {
return None;
}
Expand Down Expand Up @@ -307,16 +307,16 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
return ct;
}

if let ty::ConstKind::Bound(binder, old_var) = ct.kind()
if let ty::ConstKind::Bound(binder, old_bound) = ct.kind()
&& self.binder == binder
{
let mapped = if let Some(mapped) = self.mapping.get(&old_var) {
let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
mapped.expect_const()
} else {
let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
self.still_bound_vars.push(ty::BoundVariableKind::Const);
let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, var);
self.mapping.insert(old_var, mapped.into());
let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, ty::BoundConst { var });
self.mapping.insert(old_bound.var, mapped.into());
mapped
};

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 't
ty::ConstKind::Param(param) => {
self.params.insert(param.index);
}
ty::ConstKind::Bound(db, ty::BoundVar { .. }) if db >= self.depth => {
ty::ConstKind::Bound(db, _) if db >= self.depth => {
let guar = self.cx.dcx().delayed_bug("unexpected escaping late-bound const var");
return ControlFlow::Break(guar);
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,9 +2107,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let name = tcx.item_name(param_def_id);
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
}
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
}
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => ty::Const::new_bound(
tcx,
debruijn,
ty::BoundConst { var: ty::BoundVar::from_u32(index) },
),
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
}
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,8 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
) -> Ty<'tcx> {
debug_assert!(!self.infcx.is_some_and(|infcx| ty_var != infcx.shallow_resolve(ty_var)));
let var = self.canonical_var(var_kind, ty_var.into());
Ty::new_bound(self.tcx, self.binder_index, var.into())
let bt = ty::BoundTy { var, kind: ty::BoundTyKind::Anon };
Ty::new_bound(self.tcx, self.binder_index, bt)
}

/// Given a type variable `const_var` of the given kind, first check
Expand All @@ -768,6 +769,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
!self.infcx.is_some_and(|infcx| ct_var != infcx.shallow_resolve_const(ct_var))
);
let var = self.canonical_var(var_kind, ct_var.into());
ty::Const::new_bound(self.tcx, self.binder_index, var)
let bc = ty::BoundConst { var };
ty::Const::new_bound(self.tcx, self.binder_index, bc)
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/canonical/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for CanonicalInstantiator<'tcx> {
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Bound(debruijn, bound_const) if debruijn == self.current_index => {
self.var_values[bound_const.as_usize()].expect_const()
self.var_values[bound_const.var.as_usize()].expect_const()
}
_ => ct.super_fold_with(self),
}
Expand Down Expand Up @@ -217,7 +217,7 @@ fn highest_var_in_clauses<'tcx>(c: ty::Clauses<'tcx>) -> usize {
if let ty::ConstKind::Bound(debruijn, bound_const) = ct.kind()
&& debruijn == self.current_index
{
self.max_var = self.max_var.max(bound_const.as_usize());
self.max_var = self.max_var.max(bound_const.var.as_usize());
} else if ct.has_vars_bound_at_or_above(self.current_index) {
ct.super_visit_with(self);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ impl<'tcx> InferCtxt<'tcx> {
}
GenericArgKind::Lifetime(result_value) => {
// e.g., here `result_value` might be `'?1` in the example above...
if let ty::ReBound(debruijn, br) = result_value.kind() {
if let ty::ReBound(debruijn, b) = result_value.kind() {
// ... in which case we would set `canonical_vars[0]` to `Some('static)`.

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

// We only allow a `ty::INNERMOST` index in generic parameters.
assert_eq!(debruijn, ty::INNERMOST);
opt_values[b] = Some(*original_value);
opt_values[b.var] = Some(*original_value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,8 @@ impl<'tcx> InferCtxt<'tcx> {
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
self.args[bt.var.index()].expect_ty()
}
fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
self.args[bv.index()].expect_const()
fn replace_const(&mut self, bc: ty::BoundConst) -> ty::Const<'tcx> {
self.args[bc.var.index()].expect_const()
}
}
let delegate = ToFreshVars { args };
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/relate/higher_ranked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl<'tcx> InferCtxt<'tcx> {
ty::PlaceholderType { universe: next_universe, bound: bound_ty },
)
},
consts: &mut |bound_var: ty::BoundVar| {
consts: &mut |bound_const: ty::BoundConst| {
ty::Const::new_placeholder(
self.tcx,
ty::PlaceholderConst { universe: next_universe, bound: bound_var },
ty::PlaceholderConst { universe: next_universe, bound: bound_const },
)
},
};
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ impl<'tcx> Const<'tcx> {
pub fn new_bound(
tcx: TyCtxt<'tcx>,
debruijn: ty::DebruijnIndex,
var: ty::BoundVar,
bound_const: ty::BoundConst,
) -> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Bound(debruijn, var))
Const::new(tcx, ty::ConstKind::Bound(debruijn, bound_const))
}

#[inline]
Expand Down Expand Up @@ -168,12 +168,16 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
Const::new_var(tcx, vid)
}

fn new_bound(interner: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
Const::new_bound(interner, debruijn, var)
fn new_bound(
interner: TyCtxt<'tcx>,
debruijn: ty::DebruijnIndex,
bound_const: ty::BoundConst,
) -> Self {
Const::new_bound(interner, debruijn, bound_const)
}

fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
Const::new_bound(tcx, debruijn, var)
Const::new_bound(tcx, debruijn, ty::BoundConst { var })
}

fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type PlaceholderConst = ty::PlaceholderConst;

type ParamConst = ty::ParamConst;
type BoundConst = ty::BoundVar;
type BoundConst = ty::BoundConst;
type ValueConst = ty::Value<'tcx>;
type ExprConst = ty::Expr<'tcx>;
type ValTree = ty::ValTree<'tcx>;
Expand Down
24 changes: 15 additions & 9 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_hir::def_id::DefId;
use rustc_type_ir::data_structures::DelayedMap;

use crate::ty::{
self, Binder, BoundTy, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
self, Binder, BoundConst, BoundTy, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt,
};

Expand Down Expand Up @@ -60,7 +60,7 @@ where
pub trait BoundVarReplacerDelegate<'tcx> {
fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx>;
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx>;
fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx>;
fn replace_const(&mut self, bc: ty::BoundConst) -> ty::Const<'tcx>;
}

/// A simple delegate taking 3 mutable functions. The used functions must
Expand All @@ -69,7 +69,7 @@ pub trait BoundVarReplacerDelegate<'tcx> {
pub struct FnMutDelegate<'a, 'tcx> {
pub regions: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
pub types: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
pub consts: &'a mut (dyn FnMut(ty::BoundVar) -> ty::Const<'tcx> + 'a),
pub consts: &'a mut (dyn FnMut(ty::BoundConst) -> ty::Const<'tcx> + 'a),
}

impl<'a, 'tcx> BoundVarReplacerDelegate<'tcx> for FnMutDelegate<'a, 'tcx> {
Expand All @@ -79,8 +79,8 @@ impl<'a, 'tcx> BoundVarReplacerDelegate<'tcx> for FnMutDelegate<'a, 'tcx> {
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
(self.types)(bt)
}
fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
(self.consts)(bv)
fn replace_const(&mut self, bc: ty::BoundConst) -> ty::Const<'tcx> {
(self.consts)(bc)
}
}

Expand Down Expand Up @@ -300,7 +300,13 @@ impl<'tcx> TyCtxt<'tcx> {
ty::BoundTy { var: shift_bv(t.var), kind: t.kind },
)
},
consts: &mut |c| ty::Const::new_bound(self, ty::INNERMOST, shift_bv(c)),
consts: &mut |c| {
ty::Const::new_bound(
self,
ty::INNERMOST,
ty::BoundConst { var: shift_bv(c.var) },
)
},
},
)
}
Expand Down Expand Up @@ -343,12 +349,12 @@ impl<'tcx> TyCtxt<'tcx> {
.expect_ty();
Ty::new_bound(self.tcx, ty::INNERMOST, BoundTy { var, kind })
}
fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
let entry = self.map.entry(bv);
fn replace_const(&mut self, bc: ty::BoundConst) -> ty::Const<'tcx> {
let entry = self.map.entry(bc.var);
let index = entry.index();
let var = ty::BoundVar::from_usize(index);
let () = entry.or_insert_with(|| ty::BoundVariableKind::Const).expect_const();
ty::Const::new_bound(self.tcx, ty::INNERMOST, var)
ty::Const::new_bound(self.tcx, ty::INNERMOST, BoundConst { var })
}
}

Expand Down
23 changes: 16 additions & 7 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,34 +968,43 @@ impl<'tcx> rustc_type_ir::inherent::PlaceholderLike<TyCtxt<'tcx>> for Placeholde

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
#[derive(TyEncodable, TyDecodable)]
pub struct BoundConst<'tcx> {
pub struct BoundConst {
pub var: BoundVar,
pub ty: Ty<'tcx>,
}

pub type PlaceholderConst = Placeholder<BoundVar>;
impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundConst {
fn var(self) -> BoundVar {
self.var
}

fn assert_eq(self, var: ty::BoundVariableKind) {
var.expect_const()
}
}

pub type PlaceholderConst = Placeholder<BoundConst>;

impl<'tcx> rustc_type_ir::inherent::PlaceholderLike<TyCtxt<'tcx>> for PlaceholderConst {
type Bound = BoundVar;
type Bound = BoundConst;

fn universe(self) -> UniverseIndex {
self.universe
}

fn var(self) -> BoundVar {
self.bound
self.bound.var
}

fn with_updated_universe(self, ui: UniverseIndex) -> Self {
Placeholder { universe: ui, ..self }
}

fn new(ui: UniverseIndex, bound: BoundVar) -> Self {
fn new(ui: UniverseIndex, bound: BoundConst) -> Self {
Placeholder { universe: ui, bound }
}

fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self {
Placeholder { universe: ui, bound: var }
Placeholder { universe: ui, bound: BoundConst { var } }
}
}

Expand Down
Loading
Loading