diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index 1b7475486dcea..46c8efaecdb92 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -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) }) @@ -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))], @@ -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) } diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index 9ecc870a70dbd..6b0b41d61db8e 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -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 } }; @@ -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 { @@ -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:?}"), diff --git a/compiler/rustc_infer/src/errors/note_and_explain.rs b/compiler/rustc_infer/src/errors/note_and_explain.rs index ef543b1fb9353..ac89d8ac141f9 100644 --- a/compiler/rustc_infer/src/errors/note_and_explain.rs +++ b/compiler/rustc_infer/src/errors/note_and_explain.rs @@ -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)), diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index 3bbd01f827363..4fab89ec5869d 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -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) } diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index c9956b60a56db..7d68abe135a44 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -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) diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 9903ffa90bae1..ba9198faaca72 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -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> for ReplaceParamAndInferWithPlaceholder<'tcx> { @@ -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 { @@ -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, ) @@ -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) }) } diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 89ea89cf502e5..edc042444d258 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -254,6 +254,12 @@ impl Parse for Newtype { } } + impl std::ops::AddAssign 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 { diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index 2f42849f390b6..a731608d18294 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -149,7 +149,7 @@ 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(_) @@ -157,7 +157,7 @@ impl<'tcx> CanonicalVarInfo<'tcx> { 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, } } } @@ -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) diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 68561cf6dd775..03ac8740b6aa3 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -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:?}"), diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 8d0aa622244cd..3c280c9a18148 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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() @@ -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 { diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 6205e2bf24dd1..29a05151e18a7 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -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) @@ -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 }) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index de4c703107e14..8656c42d9cba2 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -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)), diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 5ea77833af255..e483321d3548e 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -60,7 +60,7 @@ pub struct FreeRegion { #[derive(HashStable)] pub enum BoundRegionKind { /// An anonymous region parameter for a given fn (&T) - BrAnon(u32, Option), + BrAnon(BoundVar, Option), /// Named region parameters for functions (a in &'a T) /// @@ -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, } } } @@ -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); @@ -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!(), @@ -1548,7 +1544,7 @@ impl BoundTyKind { impl From for BoundTy { fn from(var: BoundVar) -> Self { - BoundTy { var, kind: BoundTyKind::Anon(var.as_u32()) } + BoundTy { var, kind: BoundTyKind::Anon(var) } } } @@ -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 +} diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index cac7ff72267db..369a412ad2c7e 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -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() @@ -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) } diff --git a/compiler/rustc_trait_selection/src/solve/canonicalize.rs b/compiler/rustc_trait_selection/src/solve/canonicalize.rs index 2e5a8b7debc51..adfafe581ff20 100644 --- a/compiler/rustc_trait_selection/src/solve/canonicalize.rs +++ b/compiler/rustc_trait_selection/src/solve/canonicalize.rs @@ -257,7 +257,7 @@ impl<'tcx> TypeFolder> for Canonicalizer<'_, 'tcx> { self.primitive_var_infos.push(CanonicalVarInfo { kind }); var }); - let br = ty::BoundRegion { var, kind: BrAnon(var.as_u32(), None) }; + let br = ty::BoundRegion { var, kind: BrAnon(var, None) }; self.interner().mk_re_late_bound(self.binder_index, br) } @@ -300,14 +300,14 @@ impl<'tcx> TypeFolder> for Canonicalizer<'_, 'tcx> { ty::Placeholder(placeholder) => match self.canonicalize_mode { CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(ty::Placeholder { universe: placeholder.universe, - name: BoundTyKind::Anon(self.variables.len() as u32), + name: BoundTyKind::Anon(ty::BoundVar::from_usize(self.variables.len())), }), CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder), }, ty::Param(_) => match self.canonicalize_mode { CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(ty::Placeholder { universe: ty::UniverseIndex::ROOT, - name: ty::BoundTyKind::Anon(self.variables.len() as u32), + name: ty::BoundTyKind::Anon(ty::BoundVar::from(self.variables.len())), }), CanonicalizeMode::Response { .. } => bug!("param ty in response: {t:?}"), }, @@ -345,7 +345,7 @@ impl<'tcx> TypeFolder> for Canonicalizer<'_, 'tcx> { var }), ); - let bt = ty::BoundTy { var, kind: BoundTyKind::Anon(var.index() as u32) }; + let bt = ty::BoundTy { var, kind: BoundTyKind::Anon(var) }; self.interner().mk_bound(self.binder_index, bt) } diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs index ee90488730a64..cf883011adea2 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs @@ -188,7 +188,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { } else { // For placeholders which were already part of the input, we simply map this // universal bound variable back the placeholder of the input. - original_values[info.expect_anon_placeholder() as usize] + original_values[info.expect_anon_placeholder().as_usize()] } }, )); diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs b/compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs index 4a3b7e42bac15..3ac1a72cd02f1 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs @@ -95,17 +95,16 @@ fn replace_erased_lifetimes_with_bound_vars<'tcx>( let mut counter = 0; let ty = tcx.fold_regions(ty, |mut r, current_depth| { if let ty::ReErased = r.kind() { - let br = ty::BoundRegion { - var: ty::BoundVar::from_u32(counter), - kind: ty::BrAnon(counter, None), - }; + let var = ty::BoundVar::from_u32(counter); + let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) }; counter += 1; r = tcx.mk_re_late_bound(current_depth, br); } r }); let bound_vars = tcx.mk_bound_variable_kinds_from_iter( - (0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i, None))), + (0..counter) + .map(|i| ty::BoundVariableKind::Region(ty::BrAnon(ty::BoundVar::from_u32(i), None))), ); ty::Binder::bind_with_vars(ty, bound_vars) } diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index b58e62536d604..11079cb445ffd 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -3001,7 +3001,7 @@ fn bind_generator_hidden_types_above<'tcx>( let considering_regions = infcx.considering_regions; - let num_bound_variables = bound_vars.len() as u32; + let num_bound_variables = bound_vars.len(); let mut counter = num_bound_variables; let hidden_types: Vec<_> = tcx @@ -3015,10 +3015,8 @@ fn bind_generator_hidden_types_above<'tcx>( if considering_regions { ty = tcx.fold_regions(ty, |mut r, current_depth| { if let ty::ReErased = r.kind() { - let br = ty::BoundRegion { - var: ty::BoundVar::from_u32(counter), - kind: ty::BrAnon(counter, None), - }; + let var = ty::BoundVar::from_usize(counter); + let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) }; counter += 1; r = tcx.mk_re_late_bound(current_depth, br); } @@ -3032,8 +3030,11 @@ fn bind_generator_hidden_types_above<'tcx>( if considering_regions { debug_assert!(!hidden_types.has_erased_regions()); } - let bound_vars = tcx.mk_bound_variable_kinds_from_iter(bound_vars.iter().chain( - (num_bound_variables..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i, None))), - )); + let bound_vars = + tcx.mk_bound_variable_kinds_from_iter(bound_vars.iter().chain( + (num_bound_variables..counter).map(|i| { + ty::BoundVariableKind::Region(ty::BrAnon(ty::BoundVar::from_usize(i), None)) + }), + )); ty::Binder::bind_with_vars(hidden_types, bound_vars) } diff --git a/compiler/rustc_traits/src/chalk/db.rs b/compiler/rustc_traits/src/chalk/db.rs index f8c8f744e6d53..4467fff82f0c2 100644 --- a/compiler/rustc_traits/src/chalk/db.rs +++ b/compiler/rustc_traits/src/chalk/db.rs @@ -730,7 +730,7 @@ fn bound_vars_for_item(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> { ty::GenericParamDefKind::Lifetime => { let br = ty::BoundRegion { var: ty::BoundVar::from_usize(substs.len()), - kind: ty::BrAnon(substs.len() as u32, None), + kind: ty::BrAnon(ty::BoundVar::from_usize(substs.len()), None), }; tcx.mk_re_late_bound(ty::INNERMOST, br).into() } diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index 527f6013a1516..865274fa91fb2 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -376,7 +376,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty>> for Ty<'tcx> { ty::Placeholder(_placeholder) => { chalk_ir::TyKind::Placeholder(chalk_ir::PlaceholderIndex { ui: chalk_ir::UniverseIndex { counter: _placeholder.universe.as_usize() }, - idx: _placeholder.name.expect_anon() as usize, + idx: _placeholder.name.expect_anon().as_usize(), }) } ty::Infer(_infer) => unimplemented!(), @@ -475,16 +475,16 @@ impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty> { ), }, TyKind::Function(_quantified_ty) => unimplemented!(), - TyKind::BoundVar(bound) => ty::Bound( - ty::DebruijnIndex::from_usize(bound.debruijn.depth() as usize), - ty::BoundTy { - var: ty::BoundVar::from_usize(bound.index), - kind: ty::BoundTyKind::Anon(bound.index as u32), - }, - ), + TyKind::BoundVar(bound) => { + let var = ty::BoundVar::from_usize(bound.index); + ty::Bound( + ty::DebruijnIndex::from_usize(bound.debruijn.depth() as usize), + ty::BoundTy { var, kind: ty::BoundTyKind::Anon(var) }, + ) + } TyKind::Placeholder(placeholder) => ty::Placeholder(ty::Placeholder { universe: ty::UniverseIndex::from_usize(placeholder.ui.counter), - name: ty::BoundTyKind::Anon(placeholder.idx as u32), + name: ty::BoundTyKind::Anon(ty::BoundVar::from_usize(placeholder.idx)), }), TyKind::InferenceVar(_, _) => unimplemented!(), TyKind::Dyn(_) => unimplemented!(), @@ -526,17 +526,17 @@ impl<'tcx> LowerInto<'tcx, Region<'tcx>> for &chalk_ir::Lifetime) -> Region<'tcx> { let tcx = interner.tcx; match self.data(interner) { - chalk_ir::LifetimeData::BoundVar(var) => tcx.mk_re_late_bound( - ty::DebruijnIndex::from_u32(var.debruijn.depth()), - ty::BoundRegion { - var: ty::BoundVar::from_usize(var.index), - kind: ty::BrAnon(var.index as u32, None), - }, - ), + chalk_ir::LifetimeData::BoundVar(var) => { + let v = ty::BoundVar::from_usize(var.index); + tcx.mk_re_late_bound( + ty::DebruijnIndex::from_u32(var.debruijn.depth()), + ty::BoundRegion { var: v, kind: ty::BrAnon(v, None) }, + ) + } chalk_ir::LifetimeData::InferenceVar(_var) => unimplemented!(), chalk_ir::LifetimeData::Placeholder(p) => tcx.mk_re_placeholder(ty::Placeholder { universe: ty::UniverseIndex::from_usize(p.ui.counter), - name: ty::BoundRegionKind::BrAnon(p.idx as u32, None), + name: ty::BrAnon(ty::BoundVar::from_usize(p.idx), None), }), chalk_ir::LifetimeData::Static => tcx.lifetimes.re_static, chalk_ir::LifetimeData::Erased => tcx.lifetimes.re_erased, @@ -682,10 +682,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Binders>>( interner: RustInterner<'tcx>, tcx: TyCtxt<'tcx>, ty: Binder<'tcx, T>, -) -> (T, chalk_ir::VariableKinds>, BTreeMap) { +) -> (T, chalk_ir::VariableKinds>, BTreeMap) { let mut bound_vars_collector = BoundVarsCollector::new(); ty.as_ref().skip_binder().visit_with(&mut bound_vars_collector); let mut parameters = bound_vars_collector.parameters; - let named_parameters: BTreeMap = bound_vars_collector + let named_parameters: BTreeMap = bound_vars_collector .named_parameters .into_iter() .enumerate() - .map(|(i, def_id)| (def_id, (i + parameters.len()) as u32)) + .map(|(i, def_id)| (def_id, ty::BoundVar::from_usize(i + parameters.len()))) .collect(); let mut bound_var_substitutor = NamedBoundVarSubstitutor::new(tcx, &named_parameters); @@ -905,7 +906,7 @@ pub(crate) fn collect_bound_vars<'tcx, T: TypeFoldable>>( (0..parameters.len()).for_each(|i| { parameters - .get(&(i as u32)) + .get(&ty::BoundVar::from_usize(i)) .or_else(|| bug!("Skipped bound var index: parameters={:?}", parameters)); }); @@ -916,7 +917,7 @@ pub(crate) fn collect_bound_vars<'tcx, T: TypeFoldable>>( pub(crate) struct BoundVarsCollector<'tcx> { binder_index: ty::DebruijnIndex, - pub(crate) parameters: BTreeMap>>, + pub(crate) parameters: BTreeMap>>, pub(crate) named_parameters: Vec, } @@ -944,7 +945,7 @@ impl<'tcx> TypeVisitor> for BoundVarsCollector<'tcx> { fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow { match *t.kind() { ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => { - match self.parameters.entry(bound_ty.var.as_u32()) { + match self.parameters.entry(bound_ty.var) { Entry::Vacant(entry) => { entry.insert(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General)); } @@ -1002,11 +1003,11 @@ impl<'tcx> TypeVisitor> for BoundVarsCollector<'tcx> { struct NamedBoundVarSubstitutor<'a, 'tcx> { tcx: TyCtxt<'tcx>, binder_index: ty::DebruijnIndex, - named_parameters: &'a BTreeMap, + named_parameters: &'a BTreeMap, } impl<'a, 'tcx> NamedBoundVarSubstitutor<'a, 'tcx> { - fn new(tcx: TyCtxt<'tcx>, named_parameters: &'a BTreeMap) -> Self { + fn new(tcx: TyCtxt<'tcx>, named_parameters: &'a BTreeMap) -> Self { NamedBoundVarSubstitutor { tcx, binder_index: ty::INNERMOST, named_parameters } } } @@ -1030,8 +1031,10 @@ impl<'a, 'tcx> TypeFolder> for NamedBoundVarSubstitutor<'a, 'tcx> { match *r { ty::ReLateBound(index, br) if index == self.binder_index => match br.kind { ty::BrNamed(def_id, _name) => match self.named_parameters.get(&def_id) { - Some(idx) => { - let new_br = ty::BoundRegion { var: br.var, kind: ty::BrAnon(*idx, None) }; + Some(&var) => { + // njn: can the two vars differ here? Seemingly not + assert_eq!(br.var, var); + let new_br = ty::BoundRegion { var: br.var, kind: ty::BrAnon(var, None) }; return self.tcx.mk_re_late_bound(index, new_br); } None => panic!("Missing `BrNamed`."), @@ -1053,8 +1056,9 @@ pub(crate) struct ParamsSubstitutor<'tcx> { binder_index: ty::DebruijnIndex, list: Vec, next_ty_placeholder: usize, - pub(crate) params: rustc_data_structures::fx::FxHashMap, - pub(crate) named_regions: BTreeMap, + pub(crate) params: + rustc_data_structures::fx::FxHashMap, + pub(crate) named_regions: BTreeMap, } impl<'tcx> ParamsSubstitutor<'tcx> { @@ -1090,15 +1094,16 @@ impl<'tcx> TypeFolder> for ParamsSubstitutor<'tcx> { ty::Param(param) => match self.list.iter().position(|r| r == ¶m) { Some(idx) => self.tcx.mk_placeholder(ty::PlaceholderType { universe: ty::UniverseIndex::from_usize(0), - name: ty::BoundTyKind::Anon(idx as u32), + name: ty::BoundTyKind::Anon(ty::BoundVar::from_usize(idx)), }), None => { self.list.push(param); let idx = self.list.len() - 1 + self.next_ty_placeholder; - self.params.insert(idx as u32, param); + let var = ty::BoundVar::from_usize(idx); + self.params.insert(var, param); self.tcx.mk_placeholder(ty::PlaceholderType { universe: ty::UniverseIndex::from_usize(0), - name: ty::BoundTyKind::Anon(idx as u32), + name: ty::BoundTyKind::Anon(var), }) } }, @@ -1112,20 +1117,14 @@ impl<'tcx> TypeFolder> for ParamsSubstitutor<'tcx> { // since canonicalization will already change these to canonical // variables (ty::ReLateBound). ty::ReEarlyBound(_re) => match self.named_regions.get(&_re.def_id) { - Some(idx) => { - let br = ty::BoundRegion { - var: ty::BoundVar::from_u32(*idx), - kind: ty::BrAnon(*idx, None), - }; + Some(&var) => { + let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) }; self.tcx.mk_re_late_bound(self.binder_index, br) } None => { - let idx = self.named_regions.len() as u32; - let br = ty::BoundRegion { - var: ty::BoundVar::from_u32(idx), - kind: ty::BrAnon(idx, None), - }; - self.named_regions.insert(_re.def_id, idx); + let var = ty::BoundVar::from_usize(self.named_regions.len()); + let br = ty::BoundRegion { var, kind: ty::BrAnon(var, None) }; + self.named_regions.insert(_re.def_id, var); self.tcx.mk_re_late_bound(self.binder_index, br) } }, @@ -1137,13 +1136,13 @@ impl<'tcx> TypeFolder> for ParamsSubstitutor<'tcx> { pub(crate) struct ReverseParamsSubstitutor<'tcx> { tcx: TyCtxt<'tcx>, - params: rustc_data_structures::fx::FxHashMap, + params: rustc_data_structures::fx::FxHashMap, } impl<'tcx> ReverseParamsSubstitutor<'tcx> { pub(crate) fn new( tcx: TyCtxt<'tcx>, - params: rustc_data_structures::fx::FxHashMap, + params: rustc_data_structures::fx::FxHashMap, ) -> Self { Self { tcx, params } } @@ -1190,7 +1189,7 @@ impl<'tcx> TypeVisitor> for PlaceholdersCollector { match t.kind() { ty::Placeholder(p) if p.universe == self.universe_index => { self.next_ty_placeholder = - self.next_ty_placeholder.max(p.name.expect_anon() as usize + 1); + self.next_ty_placeholder.max(p.name.expect_anon().as_usize() + 1); } _ => (), @@ -1203,7 +1202,8 @@ impl<'tcx> TypeVisitor> for PlaceholdersCollector { match *r { ty::RePlaceholder(p) if p.universe == self.universe_index => { if let ty::BoundRegionKind::BrAnon(anon, _) = p.name { - self.next_anon_region_placeholder = self.next_anon_region_placeholder.max(anon); + self.next_anon_region_placeholder = + self.next_anon_region_placeholder.max(anon.as_u32()); } // FIXME: This doesn't seem to handle BrNamed at all? }