Skip to content

Use BoundVar more #110025

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

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 10 additions & 10 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,20 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
let intrinsic_name = tcx.item_name(intrinsic_id);
let name_str = intrinsic_name.as_str();

let var0 = ty::BoundVar::from_u32(0);
let var1 = ty::BoundVar::from_u32(1);
let bound_vars = tcx.mk_bound_variable_kinds(&[
ty::BoundVariableKind::Region(ty::BrAnon(0, None)),
ty::BoundVariableKind::Region(ty::BrAnon(var0, None)),
ty::BoundVariableKind::Region(ty::BrEnv),
]);
let mk_va_list_ty = |mutbl| {
tcx.lang_items().va_list().map(|did| {
let region = tcx.mk_re_late_bound(
ty::INNERMOST,
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) },
);
let env_region = tcx.mk_re_late_bound(
ty::INNERMOST,
ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrEnv },
ty::BoundRegion { var: var0, kind: ty::BrAnon(var0, None) },
);
let env_region =
tcx.mk_re_late_bound(ty::INNERMOST, ty::BoundRegion { var: var1, kind: ty::BrEnv });
let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
(tcx.mk_ref(env_region, ty::TypeAndMut { ty: va_list_ty, mutbl }), va_list_ty)
})
Expand Down Expand Up @@ -387,8 +387,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
);
let discriminant_def_id = assoc_items[0];

let br =
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) };
let var = ty::BoundVar::from_u32(0);
let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) };
(
1,
vec![tcx.mk_imm_ref(tcx.mk_re_late_bound(ty::INNERMOST, br), param(0))],
Expand Down Expand Up @@ -440,8 +440,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
sym::nontemporal_store => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),

sym::raw_eq => {
let br =
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) };
let var = ty::BoundVar::from_u32(0);
let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) };
let param_ty = tcx.mk_imm_ref(tcx.mk_re_late_bound(ty::INNERMOST, br), param(0));
(1, vec![param_ty; 2], tcx.types.bool)
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_hir_typeck/src/generator_interior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ pub fn resolve_interior<'a, 'tcx>(

let mut counter = 0;
let mut mk_bound_region = |span| {
let kind = ty::BrAnon(counter, span);
let var = ty::BoundVar::from_u32(counter);
let kind = ty::BrAnon(var, span);
counter += 1;
ty::BoundRegion { var, kind }
};
Expand Down Expand Up @@ -282,7 +282,6 @@ pub fn resolve_interior<'a, 'tcx>(
.collect();

let mut bound_vars: SmallVec<[BoundVariableKind; 4]> = smallvec![];
let mut counter = 0;
// Optimization: If there is only one captured type, then we don't actually
// need to fold and reindex (since the first type doesn't change).
let type_causes = if captured_tys.len() > 0 {
Expand All @@ -293,13 +292,12 @@ pub fn resolve_interior<'a, 'tcx>(
type_causes,
FnMutDelegate {
regions: &mut |br| {
let var = ty::BoundVar::from_usize(bound_vars.len());
let kind = match br.kind {
ty::BrAnon(_, span) => ty::BrAnon(counter, span),
ty::BrAnon(_, span) => ty::BrAnon(var, span),
_ => br.kind,
};
let var = ty::BoundVar::from_usize(bound_vars.len());
bound_vars.push(ty::BoundVariableKind::Region(kind));
counter += 1;
fcx.tcx.mk_re_late_bound(ty::INNERMOST, ty::BoundRegion { var, kind })
},
types: &mut |b| bug!("unexpected bound ty in binder: {b:?}"),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/errors/note_and_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ impl<'a> DescriptionCtx<'a> {
};
me.span = Some(sp);
}
ty::BrAnon(idx, span) => {
ty::BrAnon(var, span) => {
me.kind = "anon_num_here";
me.num_arg = idx+1;
me.num_arg = var.as_u32() + 1;
me.span = match span {
Some(_) => span,
None => Some(tcx.def_span(scope)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
r: ty::Region<'tcx>,
) -> ty::Region<'tcx> {
let var = self.canonical_var(info, r.into());
let br = ty::BoundRegion { var, kind: ty::BrAnon(var.as_u32(), None) };
let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) };
self.interner().mk_re_late_bound(self.binder_index, br)
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
};
(text, sp)
}
ty::BrAnon(idx, span) => (
format!("the anonymous lifetime #{} defined here", idx + 1),
ty::BrAnon(var, span) => (
format!("the anonymous lifetime #{} defined here", var.as_u32() + 1),
match span {
Some(span) => span,
None => tcx.def_span(scope)
Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
) -> SubstsRef<'tcx> {
struct ReplaceParamAndInferWithPlaceholder<'tcx> {
tcx: TyCtxt<'tcx>,
idx: u32,
var: ty::BoundVar,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceParamAndInferWithPlaceholder<'tcx> {
Expand All @@ -2133,9 +2133,9 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
self.tcx.mk_placeholder(ty::PlaceholderType {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundTyKind::Anon({
let idx = self.idx;
self.idx += 1;
idx
let var = self.var;
self.var += 1;
var
}),
})
} else {
Expand All @@ -2153,11 +2153,11 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
self.tcx.mk_const(
ty::PlaceholderConst {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundVar::from_u32({
let idx = self.idx;
self.idx += 1;
idx
}),
name: {
let var = self.var;
self.var += 1;
var
},
},
ty,
)
Expand All @@ -2167,5 +2167,6 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
}
}

substs.fold_with(&mut ReplaceParamAndInferWithPlaceholder { tcx, idx: 0 })
substs
.fold_with(&mut ReplaceParamAndInferWithPlaceholder { tcx, var: ty::BoundVar::from_u32(0) })
}
6 changes: 6 additions & 0 deletions compiler/rustc_macros/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ impl Parse for Newtype {
}
}

impl std::ops::AddAssign<usize> for #name {
fn add_assign(&mut self, rhs: usize) {
*self = Self::from_usize(self.as_usize() + rhs)
}
}

impl rustc_index::vec::Idx for #name {
#[inline]
fn new(value: usize) -> Self {
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_middle/src/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ impl<'tcx> CanonicalVarInfo<'tcx> {
}
}

pub fn expect_anon_placeholder(self) -> u32 {
pub fn expect_anon_placeholder(self) -> ty::BoundVar {
match self.kind {
CanonicalVarKind::Ty(_)
| CanonicalVarKind::Region(_)
| CanonicalVarKind::Const(_, _) => bug!("expected placeholder: {self:?}"),

CanonicalVarKind::PlaceholderRegion(placeholder) => placeholder.name.expect_anon(),
CanonicalVarKind::PlaceholderTy(placeholder) => placeholder.name.expect_anon(),
CanonicalVarKind::PlaceholderConst(placeholder, _) => placeholder.name.as_u32(),
CanonicalVarKind::PlaceholderConst(placeholder, _) => placeholder.name,
}
}
}
Expand Down Expand Up @@ -409,10 +409,8 @@ impl<'tcx> CanonicalVarValues<'tcx> {
tcx.mk_bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()).into()
}
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
let br = ty::BoundRegion {
var: ty::BoundVar::from_usize(i),
kind: ty::BrAnon(i as u32, None),
};
let var = ty::BoundVar::from_usize(i);
let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) };
tcx.mk_re_late_bound(ty::INNERMOST, br).into()
}
CanonicalVarKind::Const(_, ty)
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,8 @@ impl<'tcx> ClosureOutlivesSubjectTy<'tcx> {
pub fn bind(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
let inner = tcx.fold_regions(ty, |r, depth| match r.kind() {
ty::ReVar(vid) => {
let br = ty::BoundRegion {
var: ty::BoundVar::new(vid.index()),
kind: ty::BrAnon(vid.as_u32(), None),
};
let var = ty::BoundVar::new(vid.index());
let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) };
tcx.mk_re_late_bound(depth, br)
}
_ => bug!("unexpected region in ClosureOutlivesSubjectTy: {r:?}"),
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,10 @@ impl<'tcx> CommonLifetimes<'tcx> {
.map(|i| {
(0..NUM_PREINTERNED_RE_LATE_BOUNDS_V)
.map(|v| {
let var = ty::BoundVar::from_u32(v);
mk(ty::ReLateBound(
ty::DebruijnIndex::from(i),
ty::BoundRegion {
var: ty::BoundVar::from(v),
kind: ty::BrAnon(v, None),
},
ty::DebruijnIndex::from_u32(i),
ty::BoundRegion { var, kind: ty::BrAnon(var, None) },
))
})
.collect()
Expand Down Expand Up @@ -2076,9 +2074,9 @@ impl<'tcx> TyCtxt<'tcx> {
) -> Region<'tcx> {
// Use a pre-interned one when possible.
if let ty::BoundRegion { var, kind: ty::BrAnon(v, None) } = bound_region
&& var.as_u32() == v
&& var == v
&& let Some(inner) = self.lifetimes.re_late_bounds.get(debruijn.as_usize())
&& let Some(re) = inner.get(v as usize).copied()
&& let Some(re) = inner.get(v.as_usize()).copied()
{
re
} else {
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,7 @@ impl<'tcx> TyCtxt<'tcx> {
let index = entry.index();
let var = ty::BoundVar::from_usize(index);
let kind = entry
.or_insert_with(|| {
ty::BoundVariableKind::Region(ty::BrAnon(index as u32, None))
})
.or_insert_with(|| ty::BoundVariableKind::Region(ty::BrAnon(var, None)))
.expect_region();
let br = ty::BoundRegion { var, kind };
self.tcx.mk_re_late_bound(ty::INNERMOST, br)
Expand All @@ -391,9 +389,7 @@ impl<'tcx> TyCtxt<'tcx> {
let index = entry.index();
let var = ty::BoundVar::from_usize(index);
let kind = entry
.or_insert_with(|| {
ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon(index as u32))
})
.or_insert_with(|| ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon(var)))
.expect_ty();
self.tcx.mk_bound(ty::INNERMOST, BoundTy { var, kind })
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,7 @@ pub trait PrettyPrinter<'tcx>:
ty::Error(_) => p!("[type error]"),
ty::Param(ref param_ty) => p!(print(param_ty)),
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
ty::BoundTyKind::Anon(bv) => {
self.pretty_print_bound_var(debruijn, ty::BoundVar::from_u32(bv))?
}
ty::BoundTyKind::Anon(bv) => self.pretty_print_bound_var(debruijn, bv)?,
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
true if debruijn == ty::INNERMOST => p!(write("^{}", s)),
true => p!(write("^{}_{}", debruijn.index(), s)),
Expand Down
27 changes: 17 additions & 10 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct FreeRegion {
#[derive(HashStable)]
pub enum BoundRegionKind {
/// An anonymous region parameter for a given fn (&T)
BrAnon(u32, Option<Span>),
BrAnon(BoundVar, Option<Span>),

/// Named region parameters for functions (a in &'a T)
///
Expand Down Expand Up @@ -108,12 +108,12 @@ impl BoundRegionKind {
}
}

pub fn expect_anon(&self) -> u32 {
pub fn expect_anon(&self) -> BoundVar {
match *self {
BoundRegionKind::BrNamed(_, _) | BoundRegionKind::BrEnv => {
bug!("expected anon region: {self:?}")
}
BoundRegionKind::BrAnon(idx, _) => idx,
BoundRegionKind::BrAnon(var, _) => var,
}
}
}
Expand All @@ -136,10 +136,6 @@ impl<'tcx> Article for TyKind<'tcx> {
}
}

// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
static_assert_size!(TyKind<'_>, 32);

/// A closure can be modeled as a struct that looks like:
/// ```ignore (illustrative)
/// struct Closure<'l0...'li, T0...Tj, CK, CS, U>(...U);
Expand Down Expand Up @@ -1533,12 +1529,12 @@ pub struct BoundTy {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
#[derive(HashStable)]
pub enum BoundTyKind {
Anon(u32),
Anon(BoundVar),
Param(DefId, Symbol),
}

impl BoundTyKind {
pub fn expect_anon(self) -> u32 {
pub fn expect_anon(self) -> BoundVar {
match self {
BoundTyKind::Anon(i) => i,
_ => bug!(),
Expand All @@ -1548,7 +1544,7 @@ impl BoundTyKind {

impl From<BoundVar> for BoundTy {
fn from(var: BoundVar) -> Self {
BoundTy { var, kind: BoundTyKind::Anon(var.as_u32()) }
BoundTy { var, kind: BoundTyKind::Anon(var) }
}
}

Expand Down Expand Up @@ -2514,3 +2510,14 @@ impl<'tcx> VarianceDiagInfo<'tcx> {
}
}
}

// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
use super::*;
use rustc_data_structures::static_assert_size;
// tidy-alphabetical-start
static_assert_size!(RegionKind<'_>, 32);
static_assert_size!(TyKind<'_>, 32);
// tidy-alphabetical-end
}
6 changes: 3 additions & 3 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'tcx> SymbolMangler<'tcx> {
let lifetimes = regions
.into_iter()
.map(|br| match br {
ty::BrAnon(i, _) => i,
ty::BrAnon(var, _) => var.as_u32(),
_ => bug!("symbol_names: non-anonymized region `{:?}` in `{:?}`", br, value),
})
.max()
Expand Down Expand Up @@ -338,9 +338,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {

// Late-bound lifetimes use indices starting at 1,
// see `BinderLevel` for more details.
ty::ReLateBound(debruijn, ty::BoundRegion { kind: ty::BrAnon(i, _), .. }) => {
ty::ReLateBound(debruijn, ty::BoundRegion { kind: ty::BrAnon(var, _), .. }) => {
let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
let depth = binder.lifetime_depths.start + i;
let depth = binder.lifetime_depths.start + var.as_u32();

1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
}
Expand Down
Loading