diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs index 15b2a5ef2e213..254d28d243ff2 100644 --- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs @@ -52,8 +52,8 @@ impl<'tcx> UniverseInfo<'tcx> { pub(crate) fn report_erroneous_element( &self, mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>, - placeholder: ty::PlaceholderRegion, - error_element: RegionElement, + placeholder: ty::PlaceholderRegion<'tcx>, + error_element: RegionElement<'tcx>, cause: ObligationCause<'tcx>, ) { match *self { @@ -152,8 +152,8 @@ pub(crate) trait TypeOpInfo<'tcx> { fn report_erroneous_element( &self, mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>, - placeholder: ty::PlaceholderRegion, - error_element: RegionElement, + placeholder: ty::PlaceholderRegion<'tcx>, + error_element: RegionElement<'tcx>, cause: ObligationCause<'tcx>, ) { let tcx = mbcx.infcx.tcx; @@ -169,23 +169,22 @@ pub(crate) trait TypeOpInfo<'tcx> { let placeholder_region = ty::Region::new_placeholder( tcx, - ty::Placeholder { universe: adjusted_universe.into(), bound: placeholder.bound }, + ty::Placeholder::new(adjusted_universe.into(), placeholder.bound), ); - let error_region = if let RegionElement::PlaceholderRegion(error_placeholder) = - error_element - { - let adjusted_universe = - error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32()); - adjusted_universe.map(|adjusted| { - ty::Region::new_placeholder( - tcx, - ty::Placeholder { universe: adjusted.into(), bound: error_placeholder.bound }, - ) - }) - } else { - None - }; + let error_region = + if let RegionElement::PlaceholderRegion(error_placeholder) = error_element { + let adjusted_universe = + error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32()); + adjusted_universe.map(|adjusted| { + ty::Region::new_placeholder( + tcx, + ty::Placeholder::new(adjusted.into(), error_placeholder.bound), + ) + }) + } else { + None + }; debug!(?placeholder_region); @@ -440,7 +439,7 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>( placeholder_region: ty::Region<'tcx>, error_region: Option>, region_constraints: &RegionConstraintData<'tcx>, - mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin, + mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin<'tcx>, mut universe_of_region: impl FnMut(RegionVid) -> UniverseIndex, ) -> Option> { let placeholder_universe = match placeholder_region.kind() { diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 05826bea66bf8..408e0f7e03f6f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -109,15 +109,15 @@ pub(crate) enum RegionErrorKind<'tcx> { /// The placeholder free region. longer_fr: RegionVid, /// The region element that erroneously must be outlived by `longer_fr`. - error_element: RegionElement, + error_element: RegionElement<'tcx>, /// The placeholder region. - placeholder: ty::PlaceholderRegion, + placeholder: ty::PlaceholderRegion<'tcx>, }, /// Any other lifetime error. RegionError { /// The origin of the region. - fr_origin: NllRegionVariableOrigin, + fr_origin: NllRegionVariableOrigin<'tcx>, /// The region that should outlive `shorter_fr`. longer_fr: RegionVid, /// The region that should be shorter, but we can't prove it. @@ -427,7 +427,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { pub(crate) fn report_region_error( &mut self, fr: RegionVid, - fr_origin: NllRegionVariableOrigin, + fr_origin: NllRegionVariableOrigin<'tcx>, outlived_fr: RegionVid, outlives_suggestion: &mut OutlivesSuggestionBuilder, ) { diff --git a/compiler/rustc_borrowck/src/handle_placeholders.rs b/compiler/rustc_borrowck/src/handle_placeholders.rs index d23ecf6c70796..60be521c29af0 100644 --- a/compiler/rustc_borrowck/src/handle_placeholders.rs +++ b/compiler/rustc_borrowck/src/handle_placeholders.rs @@ -32,7 +32,7 @@ pub(crate) struct LoweredConstraints<'tcx> { pub(crate) type_tests: Vec>, pub(crate) liveness_constraints: LivenessValues, pub(crate) universe_causes: FxIndexMap>, - pub(crate) placeholder_indices: PlaceholderIndices, + pub(crate) placeholder_indices: PlaceholderIndices<'tcx>, } impl<'d, 'tcx, A: scc::Annotation> SccAnnotations<'d, 'tcx, A> { diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 20411fcc16fbb..8d61ffde116c5 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -661,7 +661,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> { pub(crate) fn next_region_var( &self, - origin: RegionVariableOrigin, + origin: RegionVariableOrigin<'tcx>, get_ctxt_fn: F, ) -> ty::Region<'tcx> where @@ -683,7 +683,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> { #[instrument(skip(self, get_ctxt_fn), level = "debug")] pub(crate) fn next_nll_region_var( &self, - origin: NllRegionVariableOrigin, + origin: NllRegionVariableOrigin<'tcx>, get_ctxt_fn: F, ) -> ty::Region<'tcx> where diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index e98c60e633805..847e1ebe56fbf 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -110,7 +110,7 @@ pub struct RegionInferenceContext<'tcx> { /// The final inferred values of the region variables; we compute /// one value per SCC. To get the value for any given *region*, /// you first find which scc it is a part of. - scc_values: RegionValues, + scc_values: RegionValues<'tcx, ConstraintSccIndex>, /// Type constraints that we check after solving. type_tests: Vec>, @@ -125,7 +125,7 @@ pub(crate) struct RegionDefinition<'tcx> { /// What kind of variable is this -- a free region? existential /// variable? etc. (See the `NllRegionVariableOrigin` for more /// info.) - pub(crate) origin: NllRegionVariableOrigin, + pub(crate) origin: NllRegionVariableOrigin<'tcx>, /// Which universe is this region variable defined in? This is /// most often `ty::UniverseIndex::ROOT`, but when we encounter @@ -453,7 +453,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { /// Returns `true` if the region `r` contains the point `p`. /// /// Panics if called before `solve()` executes, - pub(crate) fn region_contains(&self, r: RegionVid, p: impl ToElementIndex) -> bool { + pub(crate) fn region_contains(&self, r: RegionVid, p: impl ToElementIndex<'tcx>) -> bool { let scc = self.constraint_sccs.scc(r); self.scc_values.contains(scc, p) } @@ -481,7 +481,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { pub(crate) fn placeholders_contained_in( &self, r: RegionVid, - ) -> impl Iterator { + ) -> impl Iterator> { let scc = self.constraint_sccs.scc(r); self.scc_values.placeholders_contained_in(scc) } @@ -1311,7 +1311,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { fn check_bound_universal_region( &self, longer_fr: RegionVid, - placeholder: ty::PlaceholderRegion, + placeholder: ty::PlaceholderRegion<'tcx>, errors_buffer: &mut RegionErrors<'tcx>, ) { debug!("check_bound_universal_region(fr={:?}, placeholder={:?})", longer_fr, placeholder,); @@ -1523,7 +1523,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { pub(crate) fn region_from_element( &self, longer_fr: RegionVid, - element: &RegionElement, + element: &RegionElement<'tcx>, ) -> RegionVid { match *element { RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l), @@ -1564,7 +1564,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { pub(crate) fn best_blame_constraint( &self, from_region: RegionVid, - from_region_origin: NllRegionVariableOrigin, + from_region_origin: NllRegionVariableOrigin<'tcx>, to_region: RegionVid, ) -> (BlameConstraint<'tcx>, Vec>) { assert!(from_region != to_region, "Trying to blame a region for itself!"); diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types/region_ctxt.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types/region_ctxt.rs index 90b15cbdd2cc8..ada8908e220ac 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types/region_ctxt.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types/region_ctxt.rs @@ -27,7 +27,7 @@ pub(super) struct RegionCtxt<'a, 'tcx> { pub(super) constraint_sccs: ConstraintSccs, pub(super) scc_annotations: IndexVec, pub(super) rev_scc_graph: ReverseSccGraph, - pub(super) scc_values: RegionValues, + pub(super) scc_values: RegionValues<'tcx, ConstraintSccIndex>, } impl<'a, 'tcx> RegionCtxt<'a, 'tcx> { diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index eb611fa34757f..0063af25d781a 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -10,8 +10,8 @@ use rustc_middle::ty::{self, RegionVid}; use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex}; use tracing::debug; -use crate::BorrowIndex; use crate::polonius::LiveLoans; +use crate::{BorrowIndex, TyCtxt}; rustc_index::newtype_index! { /// A single integer representing a `ty::Placeholder`. @@ -22,7 +22,7 @@ rustc_index::newtype_index! { /// An individual element in a region value -- the value of a /// particular region variable consists of a set of these elements. #[derive(Debug, Clone, PartialEq)] -pub(crate) enum RegionElement { +pub(crate) enum RegionElement<'tcx> { /// A point in the control-flow graph. Location(Location), @@ -32,7 +32,7 @@ pub(crate) enum RegionElement { /// A placeholder (e.g., instantiated from a `for<'a> fn(&'a u32)` /// type). - PlaceholderRegion(ty::PlaceholderRegion), + PlaceholderRegion(ty::PlaceholderRegion<'tcx>), } /// Records the CFG locations where each region is live. When we initially compute liveness, we use @@ -196,25 +196,28 @@ impl LivenessValues { /// NLL. #[derive(Debug, Default)] #[derive(Clone)] // FIXME(#146079) -pub(crate) struct PlaceholderIndices { - indices: FxIndexSet, +pub(crate) struct PlaceholderIndices<'tcx> { + indices: FxIndexSet>, } -impl PlaceholderIndices { +impl<'tcx> PlaceholderIndices<'tcx> { /// Returns the `PlaceholderIndex` for the inserted `PlaceholderRegion` - pub(crate) fn insert(&mut self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex { + pub(crate) fn insert(&mut self, placeholder: ty::PlaceholderRegion<'tcx>) -> PlaceholderIndex { let (index, _) = self.indices.insert_full(placeholder); index.into() } - pub(crate) fn lookup_index(&self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex { + pub(crate) fn lookup_index( + &self, + placeholder: ty::PlaceholderRegion<'tcx>, + ) -> PlaceholderIndex { self.indices.get_index_of(&placeholder).unwrap().into() } pub(crate) fn lookup_placeholder( &self, placeholder: PlaceholderIndex, - ) -> ty::PlaceholderRegion { + ) -> ty::PlaceholderRegion<'tcx> { self.indices[placeholder.index()] } @@ -241,9 +244,9 @@ impl PlaceholderIndices { /// Here, the variable `'0` would contain the free region `'a`, /// because (since it is returned) it must live for at least `'a`. But /// it would also contain various points from within the function. -pub(crate) struct RegionValues { +pub(crate) struct RegionValues<'tcx, N: Idx> { location_map: Rc, - placeholder_indices: PlaceholderIndices, + placeholder_indices: PlaceholderIndices<'tcx>, points: SparseIntervalMatrix, free_regions: SparseBitMatrix, @@ -252,14 +255,14 @@ pub(crate) struct RegionValues { placeholders: SparseBitMatrix, } -impl RegionValues { +impl<'tcx, N: Idx> RegionValues<'tcx, N> { /// Creates a new set of "region values" that tracks causal information. /// Each of the regions in num_region_variables will be initialized with an /// empty set of points and no causal information. pub(crate) fn new( location_map: Rc, num_universal_regions: usize, - placeholder_indices: PlaceholderIndices, + placeholder_indices: PlaceholderIndices<'tcx>, ) -> Self { let num_points = location_map.num_points(); let num_placeholders = placeholder_indices.len(); @@ -274,7 +277,7 @@ impl RegionValues { /// Adds the given element to the value for the given region. Returns whether /// the element is newly added (i.e., was not already present). - pub(crate) fn add_element(&mut self, r: N, elem: impl ToElementIndex) -> bool { + pub(crate) fn add_element(&mut self, r: N, elem: impl ToElementIndex<'tcx>) -> bool { debug!("add(r={:?}, elem={:?})", r, elem); elem.add_to_row(self, r) } @@ -293,7 +296,7 @@ impl RegionValues { } /// Returns `true` if the region `r` contains the given element. - pub(crate) fn contains(&self, r: N, elem: impl ToElementIndex) -> bool { + pub(crate) fn contains(&self, r: N, elem: impl ToElementIndex<'tcx>) -> bool { elem.contained_in_row(self, r) } @@ -359,7 +362,7 @@ impl RegionValues { pub(crate) fn placeholders_contained_in( &self, r: N, - ) -> impl Iterator { + ) -> impl Iterator> { self.placeholders .row(r) .into_iter() @@ -368,7 +371,7 @@ impl RegionValues { } /// Returns all the elements contained in a given region's value. - pub(crate) fn elements_contained_in(&self, r: N) -> impl Iterator { + pub(crate) fn elements_contained_in(&self, r: N) -> impl Iterator> { let points_iter = self.locations_outlived_by(r).map(RegionElement::Location); let free_regions_iter = @@ -386,42 +389,50 @@ impl RegionValues { } } -pub(crate) trait ToElementIndex: Debug + Copy { - fn add_to_row(self, values: &mut RegionValues, row: N) -> bool; +pub(crate) trait ToElementIndex<'tcx>: Debug + Copy { + fn add_to_row(self, values: &mut RegionValues<'tcx, N>, row: N) -> bool; - fn contained_in_row(self, values: &RegionValues, row: N) -> bool; + fn contained_in_row(self, values: &RegionValues<'tcx, N>, row: N) -> bool; } -impl ToElementIndex for Location { - fn add_to_row(self, values: &mut RegionValues, row: N) -> bool { +impl ToElementIndex<'_> for Location { + fn add_to_row(self, values: &mut RegionValues<'_, N>, row: N) -> bool { let index = values.location_map.point_from_location(self); values.points.insert(row, index) } - fn contained_in_row(self, values: &RegionValues, row: N) -> bool { + fn contained_in_row(self, values: &RegionValues<'_, N>, row: N) -> bool { let index = values.location_map.point_from_location(self); values.points.contains(row, index) } } -impl ToElementIndex for RegionVid { - fn add_to_row(self, values: &mut RegionValues, row: N) -> bool { +impl ToElementIndex<'_> for RegionVid { + fn add_to_row(self, values: &mut RegionValues<'_, N>, row: N) -> bool { values.free_regions.insert(row, self) } - fn contained_in_row(self, values: &RegionValues, row: N) -> bool { + fn contained_in_row(self, values: &RegionValues<'_, N>, row: N) -> bool { values.free_regions.contains(row, self) } } -impl ToElementIndex for ty::PlaceholderRegion { - fn add_to_row(self, values: &mut RegionValues, row: N) -> bool { - let index = values.placeholder_indices.lookup_index(self); +impl<'tcx> ToElementIndex<'tcx> for ty::PlaceholderRegion<'tcx> { + fn add_to_row(self, values: &mut RegionValues<'tcx, N>, row: N) -> bool + where + Self: Into, ty::BoundRegion>>, + { + let placeholder: ty::Placeholder, ty::BoundRegion> = self.into(); + let index = values.placeholder_indices.lookup_index(placeholder); values.placeholders.insert(row, index) } - fn contained_in_row(self, values: &RegionValues, row: N) -> bool { - let index = values.placeholder_indices.lookup_index(self); + fn contained_in_row(self, values: &RegionValues<'tcx, N>, row: N) -> bool + where + Self: Into, ty::BoundRegion>>, + { + let placeholder: ty::Placeholder, ty::BoundRegion> = self.into(); + let index = values.placeholder_indices.lookup_index(placeholder); values.placeholders.contains(row, index) } } @@ -441,7 +452,9 @@ pub(crate) fn pretty_print_points( } /// For debugging purposes, returns a pretty-printed string of the given region elements. -fn pretty_print_region_elements(elements: impl IntoIterator) -> String { +fn pretty_print_region_elements<'tcx>( + elements: impl IntoIterator>, +) -> String { let mut result = String::new(); result.push('{'); diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 5f86e8646c03f..b0b2f0d1cc9cd 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -258,7 +258,7 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> { /// /// To keep everything in sync, do not insert this set /// directly. Instead, use the `placeholder_region` helper. - pub(crate) placeholder_indices: PlaceholderIndices, + pub(crate) placeholder_indices: PlaceholderIndices<'tcx>, /// Each time we add a placeholder to `placeholder_indices`, we /// also create a corresponding "representative" region vid for @@ -289,7 +289,7 @@ impl<'tcx> MirTypeckRegionConstraints<'tcx> { pub(crate) fn placeholder_region( &mut self, infcx: &InferCtxt<'tcx>, - placeholder: ty::PlaceholderRegion, + placeholder: ty::PlaceholderRegion<'tcx>, ) -> ty::Region<'tcx> { let placeholder_index = self.placeholder_indices.insert(placeholder); match self.placeholder_index_to_region.get(placeholder_index) { diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 7ac2dff12f756..9c828e670f072 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -182,7 +182,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> { universe }); - let placeholder = ty::PlaceholderRegion { universe, bound: br }; + let placeholder = ty::PlaceholderRegion::new(universe, br); debug!(?placeholder); let placeholder_reg = self.next_placeholder_region(placeholder); debug!(?placeholder_reg); @@ -255,7 +255,10 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> { } #[instrument(skip(self), level = "debug")] - fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> { + fn next_placeholder_region( + &mut self, + placeholder: ty::PlaceholderRegion<'tcx>, + ) -> ty::Region<'tcx> { let reg = self.type_checker.constraints.placeholder_region(self.type_checker.infcx, placeholder); diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index 64a7b4084349d..aee1eb94dc81f 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -454,8 +454,6 @@ struct UniversalRegionsBuilder<'infcx, 'tcx> { mir_def: LocalDefId, } -const FR: NllRegionVariableOrigin = NllRegionVariableOrigin::FreeRegion; - impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { fn build(self) -> UniversalRegions<'tcx> { debug!("build(mir_def={:?})", self.mir_def); @@ -466,8 +464,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { assert_eq!(FIRST_GLOBAL_INDEX, self.infcx.num_region_vars()); // Create the "global" region that is always free in all contexts: 'static. - let fr_static = - self.infcx.next_nll_region_var(FR, || RegionCtxt::Free(kw::Static)).as_var(); + let fr_static = self + .infcx + .next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || { + RegionCtxt::Free(kw::Static) + }) + .as_var(); // We've now added all the global regions. The next ones we // add will be external. @@ -500,7 +502,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { debug!(?r); let region_vid = { let name = r.get_name_or_anon(self.infcx.tcx); - self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name)) + self.infcx.next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || { + RegionCtxt::LateBound(name) + }) }; debug!(?region_vid); @@ -526,7 +530,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { let r = ty::Region::new_late_param(self.infcx.tcx, self.mir_def.to_def_id(), kind); let region_vid = { let name = r.get_name_or_anon(self.infcx.tcx); - self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name)) + self.infcx.next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || { + RegionCtxt::LateBound(name) + }) }; debug!(?region_vid); @@ -553,7 +559,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { let reg_vid = self .infcx - .next_nll_region_var(FR, || RegionCtxt::Free(sym::c_dash_variadic)) + .next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || { + RegionCtxt::Free(sym::c_dash_variadic) + }) .as_var(); let region = ty::Region::new_var(self.infcx.tcx, reg_vid); @@ -569,8 +577,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { } } - let fr_fn_body = - self.infcx.next_nll_region_var(FR, || RegionCtxt::Free(sym::fn_body)).as_var(); + let fr_fn_body = self + .infcx + .next_nll_region_var(NllRegionVariableOrigin::FreeRegion, || { + RegionCtxt::Free(sym::fn_body) + }) + .as_var(); let num_universals = self.infcx.num_region_vars(); @@ -613,8 +625,10 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { debug!("defining_ty (pre-replacement): {:?}", defining_ty); - let defining_ty = - self.infcx.replace_free_regions_with_nll_infer_vars(FR, defining_ty); + let defining_ty = self.infcx.replace_free_regions_with_nll_infer_vars( + NllRegionVariableOrigin::FreeRegion, + defining_ty, + ); match *defining_ty.kind() { ty::Closure(def_id, args) => DefiningTy::Closure(def_id, args), @@ -638,8 +652,10 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { // Do not ICE when checking default_field_values consts with lifetimes (#135649) && DefKind::Field != tcx.def_kind(tcx.parent(typeck_root_def_id)) { - let args = - self.infcx.replace_free_regions_with_nll_infer_vars(FR, identity_args); + let args = self.infcx.replace_free_regions_with_nll_infer_vars( + NllRegionVariableOrigin::FreeRegion, + identity_args, + ); DefiningTy::Const(self.mir_def.to_def_id(), args) } else { // FIXME this line creates a dependency between borrowck and typeck. @@ -659,7 +675,10 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { InlineConstArgsParts { parent_args: identity_args, ty }, ) .args; - let args = self.infcx.replace_free_regions_with_nll_infer_vars(FR, args); + let args = self.infcx.replace_free_regions_with_nll_infer_vars( + NllRegionVariableOrigin::FreeRegion, + args, + ); DefiningTy::InlineConst(self.mir_def.to_def_id(), args) } } @@ -856,7 +875,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> { #[instrument(skip(self), level = "debug")] fn replace_free_regions_with_nll_infer_vars( &self, - origin: NllRegionVariableOrigin, + origin: NllRegionVariableOrigin<'tcx>, value: T, ) -> T where diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index fa497cc21e40d..c18faa785b169 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -590,10 +590,10 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( ty, Ty::new_placeholder( tcx, - ty::Placeholder { + ty::Placeholder::new( universe, - bound: ty::BoundTy { var: idx, kind: ty::BoundTyKind::Anon }, - }, + ty::BoundTy { var: idx, kind: ty::BoundTyKind::Anon }, + ), ), ) }) diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index e445def4faa73..c07b41b56caaa 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -684,22 +684,22 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { CanonicalVarKind::Region(u) => CanonicalVarKind::Region(reverse_universe_map[&u]), CanonicalVarKind::Const(u) => CanonicalVarKind::Const(reverse_universe_map[&u]), CanonicalVarKind::PlaceholderTy(placeholder) => { - CanonicalVarKind::PlaceholderTy(ty::Placeholder { - universe: reverse_universe_map[&placeholder.universe], - ..placeholder - }) + CanonicalVarKind::PlaceholderTy(ty::Placeholder::new( + reverse_universe_map[&placeholder.universe], + placeholder.bound, + )) } CanonicalVarKind::PlaceholderRegion(placeholder) => { - CanonicalVarKind::PlaceholderRegion(ty::Placeholder { - universe: reverse_universe_map[&placeholder.universe], - ..placeholder - }) + CanonicalVarKind::PlaceholderRegion(ty::Placeholder::new( + reverse_universe_map[&placeholder.universe], + placeholder.bound, + )) } CanonicalVarKind::PlaceholderConst(placeholder) => { - CanonicalVarKind::PlaceholderConst(ty::Placeholder { - universe: reverse_universe_map[&placeholder.universe], - ..placeholder - }) + CanonicalVarKind::PlaceholderConst(ty::Placeholder::new( + reverse_universe_map[&placeholder.universe], + placeholder.bound, + )) } }) .collect() diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index f99f228e19d8e..9af0e17be4e2c 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -109,9 +109,9 @@ impl<'tcx> InferCtxt<'tcx> { CanonicalVarKind::Float => self.next_float_var().into(), - CanonicalVarKind::PlaceholderTy(ty::PlaceholderType { universe, bound }) => { + CanonicalVarKind::PlaceholderTy(ty::PlaceholderType { universe, bound, .. }) => { let universe_mapped = universe_map(universe); - let placeholder_mapped = ty::PlaceholderType { universe: universe_mapped, bound }; + let placeholder_mapped = ty::PlaceholderType::new(universe_mapped, bound); Ty::new_placeholder(self.tcx, placeholder_mapped).into() } @@ -119,18 +119,22 @@ impl<'tcx> InferCtxt<'tcx> { .next_region_var_in_universe(RegionVariableOrigin::Misc(span), universe_map(ui)) .into(), - CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion { universe, bound }) => { + CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion { + universe, bound, .. + }) => { let universe_mapped = universe_map(universe); - let placeholder_mapped = ty::PlaceholderRegion { universe: universe_mapped, bound }; + let placeholder_mapped = ty::PlaceholderRegion::new(universe_mapped, bound); ty::Region::new_placeholder(self.tcx, placeholder_mapped).into() } CanonicalVarKind::Const(ui) => { self.next_const_var_in_universe(span, universe_map(ui)).into() } - CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, bound }) => { + CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { + universe, bound, .. + }) => { let universe_mapped = universe_map(universe); - let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, bound }; + let placeholder_mapped = ty::PlaceholderConst::new(universe_mapped, bound); ty::Const::new_placeholder(self.tcx, placeholder_mapped).into() } } diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs index 3adcfb4272788..5134b7b7ca8f1 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs @@ -31,7 +31,7 @@ use crate::infer::{RegionRelations, RegionVariableOrigin, SubregionOrigin}; #[instrument(level = "debug", skip(region_rels, var_infos, data))] pub(crate) fn resolve<'tcx>( region_rels: &RegionRelations<'_, 'tcx>, - var_infos: VarInfos, + var_infos: VarInfos<'tcx>, data: RegionConstraintData<'tcx>, ) -> (LexicalRegionResolutions<'tcx>, Vec>) { let mut errors = vec![]; @@ -80,7 +80,7 @@ pub enum RegionResolutionError<'tcx> { /// `sub_r <= sup_r` does not hold. SubSupConflict( RegionVid, - RegionVariableOrigin, + RegionVariableOrigin<'tcx>, SubregionOrigin<'tcx>, Region<'tcx>, SubregionOrigin<'tcx>, @@ -92,7 +92,7 @@ pub enum RegionResolutionError<'tcx> { /// cannot name the placeholder `'b`. UpperBoundUniverseConflict( RegionVid, - RegionVariableOrigin, + RegionVariableOrigin<'tcx>, ty::UniverseIndex, // the universe index of the region variable SubregionOrigin<'tcx>, // cause of the constraint Region<'tcx>, // the placeholder `'b` @@ -122,7 +122,7 @@ type RegionGraph<'tcx> = LinkedGraph<(), Constraint<'tcx>>; struct LexicalResolver<'cx, 'tcx> { region_rels: &'cx RegionRelations<'cx, 'tcx>, - var_infos: VarInfos, + var_infos: VarInfos<'tcx>, data: RegionConstraintData<'tcx>, } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index dc4ee0d88a06c..53567c6071e3c 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -421,7 +421,7 @@ pub enum BoundRegionConversionTime { /// /// See `error_reporting` module for more details. #[derive(Copy, Clone, Debug)] -pub enum RegionVariableOrigin { +pub enum RegionVariableOrigin<'tcx> { /// Region variables created for ill-categorized reasons. /// /// They mostly indicate places in need of refactoring. @@ -453,11 +453,11 @@ pub enum RegionVariableOrigin { /// This origin is used for the inference variables that we create /// during NLL region processing. - Nll(NllRegionVariableOrigin), + Nll(NllRegionVariableOrigin<'tcx>), } #[derive(Copy, Clone, Debug)] -pub enum NllRegionVariableOrigin { +pub enum NllRegionVariableOrigin<'tcx> { /// During NLL region processing, we create variables for free /// regions that we encounter in the function signature and /// elsewhere. This origin indices we've got one of those. @@ -465,7 +465,7 @@ pub enum NllRegionVariableOrigin { /// "Universal" instantiation of a higher-ranked region (e.g., /// from a `for<'a> T` binder). Meant to represent "any region". - Placeholder(ty::PlaceholderRegion), + Placeholder(ty::PlaceholderRegion<'tcx>), Existential { name: Option, @@ -838,7 +838,7 @@ impl<'tcx> InferCtxt<'tcx> { /// Creates a fresh region variable with the next available index. /// The variable will be created in the maximum universe created /// thus far, allowing it to name any region created thus far. - pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> { + pub fn next_region_var(&self, origin: RegionVariableOrigin<'tcx>) -> ty::Region<'tcx> { self.next_region_var_in_universe(origin, self.universe()) } @@ -847,7 +847,7 @@ impl<'tcx> InferCtxt<'tcx> { /// `next_region_var` and just use the maximal universe. pub fn next_region_var_in_universe( &self, - origin: RegionVariableOrigin, + origin: RegionVariableOrigin<'tcx>, universe: ty::UniverseIndex, ) -> ty::Region<'tcx> { let region_var = @@ -878,7 +878,7 @@ impl<'tcx> InferCtxt<'tcx> { /// Just a convenient wrapper of `next_region_var` for using during NLL. #[instrument(skip(self), level = "debug")] - pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> { + pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin<'tcx>) -> ty::Region<'tcx> { self.next_region_var(RegionVariableOrigin::Nll(origin)) } @@ -886,7 +886,7 @@ impl<'tcx> InferCtxt<'tcx> { #[instrument(skip(self), level = "debug")] pub fn next_nll_region_var_in_universe( &self, - origin: NllRegionVariableOrigin, + origin: NllRegionVariableOrigin<'tcx>, universe: ty::UniverseIndex, ) -> ty::Region<'tcx> { self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe) @@ -954,7 +954,7 @@ impl<'tcx> InferCtxt<'tcx> { self.tainted_by_errors.set(Some(e)); } - pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin { + pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin<'tcx> { let mut inner = self.inner.borrow_mut(); let inner = &mut *inner; inner.unwrap_region_constraints().var_origin(vid) @@ -962,7 +962,7 @@ impl<'tcx> InferCtxt<'tcx> { /// Clone the list of variable regions. This is used only during NLL processing /// to put the set of region variables into the NLL region context. - pub fn get_region_var_infos(&self) -> VarInfos { + pub fn get_region_var_infos(&self) -> VarInfos<'tcx> { let inner = self.inner.borrow(); assert!(!UndoLogs::>::in_snapshot(&inner.undo_log)); let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved"); @@ -1649,7 +1649,7 @@ impl<'tcx> SubregionOrigin<'tcx> { } } -impl RegionVariableOrigin { +impl<'tcx> RegionVariableOrigin<'tcx> { pub fn span(&self) -> Span { match *self { RegionVariableOrigin::Misc(a) diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index a640dcb1b4e1e..f06f50785eccf 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -394,7 +394,7 @@ where &mut self, origin: infer::SubregionOrigin<'tcx>, region: ty::Region<'tcx>, - placeholder_ty: ty::PlaceholderType, + placeholder_ty: ty::PlaceholderType<'tcx>, ) { let verify_bound = self .verify_bound diff --git a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs index 4d76bc2e17a17..4ef1ea5a1c4d9 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs @@ -105,7 +105,7 @@ struct LeakCheck<'a, 'tcx> { // is repurposed to store some placeholder `P` such that the weaker // condition `S: P` must hold. (This is true if `S: S1` transitively and `S1 // = P`.) - scc_placeholders: IndexVec>, + scc_placeholders: IndexVec>>, // For each SCC S, track the minimum universe that flows into it. Note that // this is both the minimum of the universes for every region that is a @@ -258,15 +258,15 @@ impl<'a, 'tcx> LeakCheck<'a, 'tcx> { fn placeholder_error( &self, - placeholder1: ty::PlaceholderRegion, - placeholder2: ty::PlaceholderRegion, + placeholder1: ty::PlaceholderRegion<'tcx>, + placeholder2: ty::PlaceholderRegion<'tcx>, ) -> TypeError<'tcx> { self.error(placeholder1, ty::Region::new_placeholder(self.tcx, placeholder2)) } fn error( &self, - placeholder: ty::PlaceholderRegion, + placeholder: ty::PlaceholderRegion<'tcx>, other_region: ty::Region<'tcx>, ) -> TypeError<'tcx> { debug!("error: placeholder={:?}, other_region={:?}", placeholder, other_region); diff --git a/compiler/rustc_infer/src/infer/region_constraints/mod.rs b/compiler/rustc_infer/src/infer/region_constraints/mod.rs index 85f5e55a8e1e2..81fcd87587b7b 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/mod.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/mod.rs @@ -23,7 +23,7 @@ mod leak_check; #[derive(Clone, Default)] pub struct RegionConstraintStorage<'tcx> { /// For each `RegionVid`, the corresponding `RegionVariableOrigin`. - pub(super) var_infos: IndexVec, + pub(super) var_infos: IndexVec>, pub(super) data: RegionConstraintData<'tcx>, @@ -57,7 +57,7 @@ pub struct RegionConstraintCollector<'a, 'tcx> { undo_log: &'a mut InferCtxtUndoLogs<'tcx>, } -pub type VarInfos = IndexVec; +pub type VarInfos<'tcx> = IndexVec>; /// The full set of region constraints gathered up by the collector. /// Describes constraints between the region variables and other @@ -125,7 +125,7 @@ pub struct Verify<'tcx> { #[derive(Copy, Clone, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)] pub enum GenericKind<'tcx> { Param(ty::ParamTy), - Placeholder(ty::PlaceholderType), + Placeholder(ty::PlaceholderType<'tcx>), Alias(ty::AliasTy<'tcx>), } @@ -269,8 +269,8 @@ pub(crate) enum CombineMapType { type CombineMap<'tcx> = FxHashMap, RegionVid>; #[derive(Debug, Clone, Copy)] -pub struct RegionVariableInfo { - pub origin: RegionVariableOrigin, +pub struct RegionVariableInfo<'tcx> { + pub origin: RegionVariableOrigin<'tcx>, // FIXME: This is only necessary for `fn take_and_reset_data` and // `lexical_region_resolve`. We should rework `lexical_region_resolve` // in the near/medium future anyways and could move the unverse info @@ -374,7 +374,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> { pub(super) fn new_region_var( &mut self, universe: ty::UniverseIndex, - origin: RegionVariableOrigin, + origin: RegionVariableOrigin<'tcx>, ) -> RegionVid { let vid = self.storage.var_infos.push(RegionVariableInfo { origin, universe }); @@ -386,7 +386,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> { } /// Returns the origin for the given variable. - pub(super) fn var_origin(&self, vid: RegionVid) -> RegionVariableOrigin { + pub(super) fn var_origin(&self, vid: RegionVid) -> RegionVariableOrigin<'tcx> { self.storage.var_infos[vid].origin } @@ -624,10 +624,10 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> { } } - pub fn vars_since_snapshot( - &self, + pub fn vars_since_snapshot<'a>( + &'a self, value_count: usize, - ) -> (Range, Vec) { + ) -> (Range, Vec>) { let range = RegionVid::from(value_count)..RegionVid::from(self.storage.unification_table.len()); ( @@ -669,7 +669,7 @@ impl<'tcx> fmt::Display for GenericKind<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { GenericKind::Param(ref p) => write!(f, "{p}"), - GenericKind::Placeholder(ref p) => write!(f, "{p}"), + GenericKind::Placeholder(ref p) => write!(f, "{p:?}"), GenericKind::Alias(ref p) => write!(f, "{p}"), } } diff --git a/compiler/rustc_infer/src/infer/relate/higher_ranked.rs b/compiler/rustc_infer/src/infer/relate/higher_ranked.rs index 16fe591b29bba..7a0f70e979b83 100644 --- a/compiler/rustc_infer/src/infer/relate/higher_ranked.rs +++ b/compiler/rustc_infer/src/infer/relate/higher_ranked.rs @@ -34,21 +34,15 @@ impl<'tcx> InferCtxt<'tcx> { let delegate = FnMutDelegate { regions: &mut |br: ty::BoundRegion| { - ty::Region::new_placeholder( - self.tcx, - ty::PlaceholderRegion { universe: next_universe, bound: br }, - ) + ty::Region::new_placeholder(self.tcx, ty::PlaceholderRegion::new(next_universe, br)) }, types: &mut |bound_ty: ty::BoundTy| { - Ty::new_placeholder( - self.tcx, - ty::PlaceholderType { universe: next_universe, bound: bound_ty }, - ) + Ty::new_placeholder(self.tcx, ty::PlaceholderType::new(next_universe, bound_ty)) }, consts: &mut |bound_const: ty::BoundConst| { ty::Const::new_placeholder( self.tcx, - ty::PlaceholderConst { universe: next_universe, bound: bound_const }, + ty::PlaceholderConst::new(next_universe, bound_const), ) }, }; diff --git a/compiler/rustc_infer/src/infer/snapshot/fudge.rs b/compiler/rustc_infer/src/infer/snapshot/fudge.rs index 3730d215a901e..6709c822dc7b1 100644 --- a/compiler/rustc_infer/src/infer/snapshot/fudge.rs +++ b/compiler/rustc_infer/src/infer/snapshot/fudge.rs @@ -113,7 +113,7 @@ impl<'tcx> InferCtxt<'tcx> { fn fudge_inference>>( &self, - snapshot_vars: SnapshotVarData, + snapshot_vars: SnapshotVarData<'tcx>, value: T, ) -> T { // Micro-optimization: if no variables have been created, then @@ -126,16 +126,16 @@ impl<'tcx> InferCtxt<'tcx> { } } -struct SnapshotVarData { - region_vars: (Range, Vec), +struct SnapshotVarData<'tcx> { + region_vars: (Range, Vec>), type_vars: (Range, Vec), int_vars: Range, float_vars: Range, const_vars: (Range, Vec), } -impl SnapshotVarData { - fn new(infcx: &InferCtxt<'_>, vars_pre_snapshot: VariableLengths) -> SnapshotVarData { +impl<'tcx> SnapshotVarData<'tcx> { + fn new(infcx: &InferCtxt<'tcx>, vars_pre_snapshot: VariableLengths) -> SnapshotVarData<'tcx> { let mut inner = infcx.inner.borrow_mut(); let region_vars = inner .unwrap_region_constraints() @@ -165,7 +165,7 @@ impl SnapshotVarData { struct InferenceFudger<'a, 'tcx> { infcx: &'a InferCtxt<'tcx>, - snapshot_vars: SnapshotVarData, + snapshot_vars: SnapshotVarData<'tcx>, } impl<'a, 'tcx> TypeFolder> for InferenceFudger<'a, 'tcx> { diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 95adb561c704d..787ea5f9363d2 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -107,7 +107,10 @@ impl<'tcx> Const<'tcx> { } #[inline] - pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Const<'tcx> { + pub fn new_placeholder( + tcx: TyCtxt<'tcx>, + placeholder: ty::PlaceholderConst<'tcx>, + ) -> Const<'tcx> { Const::new(tcx, ty::ConstKind::Placeholder(placeholder)) } @@ -192,7 +195,7 @@ impl<'tcx> rustc_type_ir::inherent::Const> for Const<'tcx> { Const::new_canonical_bound(tcx, var) } - fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst) -> Self { + fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderConst<'tcx>) -> Self { Const::new_placeholder(tcx, placeholder) } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 0cd36d5e971de..a5744f5c53cec 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -148,7 +148,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type BoundTy = ty::BoundTy; type Symbol = Symbol; - type PlaceholderTy = ty::PlaceholderType; + type PlaceholderTy = ty::PlaceholderType<'tcx>; type ErrorGuaranteed = ErrorGuaranteed; type BoundExistentialPredicates = &'tcx List>; @@ -158,7 +158,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type Safety = hir::Safety; type Abi = ExternAbi; type Const = ty::Const<'tcx>; - type PlaceholderConst = ty::PlaceholderConst; + type PlaceholderConst = ty::PlaceholderConst<'tcx>; type ParamConst = ty::ParamConst; type BoundConst = ty::BoundConst; @@ -170,7 +170,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type EarlyParamRegion = ty::EarlyParamRegion; type LateParamRegion = ty::LateParamRegion; type BoundRegion = ty::BoundRegion; - type PlaceholderRegion = ty::PlaceholderRegion; + type PlaceholderRegion = ty::PlaceholderRegion<'tcx>; type RegionAssumptions = &'tcx ty::List>; diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 61b3059ab4253..7913178edc304 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -886,20 +886,9 @@ impl<'tcx> DefinitionSiteHiddenType<'tcx> { } } -/// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are -/// identified by both a universe, as well as a name residing within that universe. Distinct bound -/// regions/types/consts within the same universe simply have an unknown relationship to one -/// another. -#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -#[derive(HashStable, TyEncodable, TyDecodable)] -pub struct Placeholder { - pub universe: UniverseIndex, - pub bound: T, -} - -pub type PlaceholderRegion = Placeholder; +pub type PlaceholderRegion<'tcx> = ty::Placeholder, BoundRegion>; -impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderRegion { +impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderRegion<'tcx> { type Bound = BoundRegion; fn universe(self) -> UniverseIndex { @@ -911,21 +900,21 @@ impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for Placeholde } fn with_updated_universe(self, ui: UniverseIndex) -> Self { - Placeholder { universe: ui, ..self } + ty::Placeholder::new(ui, self.bound) } fn new(ui: UniverseIndex, bound: BoundRegion) -> Self { - Placeholder { universe: ui, bound } + ty::Placeholder::new(ui, bound) } fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self { - Placeholder { universe: ui, bound: BoundRegion { var, kind: BoundRegionKind::Anon } } + ty::Placeholder::new(ui, BoundRegion { var, kind: BoundRegionKind::Anon }) } } -pub type PlaceholderType = Placeholder; +pub type PlaceholderType<'tcx> = ty::Placeholder, BoundTy>; -impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderType { +impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderType<'tcx> { type Bound = BoundTy; fn universe(self) -> UniverseIndex { @@ -937,15 +926,15 @@ impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for Placeholde } fn with_updated_universe(self, ui: UniverseIndex) -> Self { - Placeholder { universe: ui, ..self } + ty::Placeholder::new(ui, self.bound) } fn new(ui: UniverseIndex, bound: BoundTy) -> Self { - Placeholder { universe: ui, bound } + ty::Placeholder::new(ui, bound) } fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self { - Placeholder { universe: ui, bound: BoundTy { var, kind: BoundTyKind::Anon } } + ty::Placeholder::new(ui, BoundTy { var, kind: BoundTyKind::Anon }) } } @@ -965,9 +954,9 @@ impl<'tcx> rustc_type_ir::inherent::BoundVarLike> for BoundConst { } } -pub type PlaceholderConst = Placeholder; +pub type PlaceholderConst<'tcx> = ty::Placeholder, BoundConst>; -impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderConst { +impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for PlaceholderConst<'tcx> { type Bound = BoundConst; fn universe(self) -> UniverseIndex { @@ -979,15 +968,15 @@ impl<'tcx> rustc_type_ir::inherent::PlaceholderLike> for Placeholde } fn with_updated_universe(self, ui: UniverseIndex) -> Self { - Placeholder { universe: ui, ..self } + ty::Placeholder::new(ui, self.bound) } fn new(ui: UniverseIndex, bound: BoundConst) -> Self { - Placeholder { universe: ui, bound } + ty::Placeholder::new(ui, bound) } fn new_anon(ui: UniverseIndex, var: BoundVar) -> Self { - Placeholder { universe: ui, bound: BoundConst { var } } + ty::Placeholder::new(ui, BoundConst { var }) } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 06744ae6e2426..42f169c4c1215 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -811,7 +811,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { } ty::Foreign(def_id) => self.print_def_path(def_id, &[])?, ty::Alias(ty::Projection | ty::Inherent | ty::Free, ref data) => data.print(self)?, - ty::Placeholder(placeholder) => placeholder.print(self)?, + ty::Placeholder(placeholder) => write!(self, "{:?}", placeholder)?, ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => { // We use verbose printing in 'NO_QUERIES' mode, to // avoid needing to call `predicates_of`. This should @@ -3274,6 +3274,16 @@ define_print! { p.reset_type_limit(); self.term.print(p)?; } + + ty::PlaceholderType<'tcx> { + match self.bound.kind { + ty::BoundTyKind::Anon => write!(p, "{self:?}")?, + ty::BoundTyKind::Param(def_id) => match p.should_print_verbose() { + true => write!(p, "{self:?}")?, + false => write!(p, "{}", p.tcx().item_name(def_id))?, + }, + } + } } define_print_and_forward_display! { @@ -3338,16 +3348,6 @@ define_print_and_forward_display! { write!(p, "{}", self.name)?; } - ty::PlaceholderType { - match self.bound.kind { - ty::BoundTyKind::Anon => write!(p, "{self:?}")?, - ty::BoundTyKind::Param(def_id) => match p.should_print_verbose() { - true => write!(p, "{self:?}")?, - false => write!(p, "{}", p.tcx().item_name(def_id))?, - }, - } - } - ty::ParamConst { write!(p, "{}", self.name)?; } diff --git a/compiler/rustc_middle/src/ty/region.rs b/compiler/rustc_middle/src/ty/region.rs index f0687f2bc726d..61994d928decd 100644 --- a/compiler/rustc_middle/src/ty/region.rs +++ b/compiler/rustc_middle/src/ty/region.rs @@ -97,7 +97,10 @@ impl<'tcx> Region<'tcx> { } #[inline] - pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion) -> Region<'tcx> { + pub fn new_placeholder( + tcx: TyCtxt<'tcx>, + placeholder: ty::PlaceholderRegion<'tcx>, + ) -> Region<'tcx> { tcx.intern_region(ty::RePlaceholder(placeholder)) } @@ -170,7 +173,7 @@ impl<'tcx> rustc_type_ir::inherent::Region> for Region<'tcx> { Region::new_canonical_bound(tcx, var) } - fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion) -> Self { + fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion<'tcx>) -> Self { Region::new_placeholder(tcx, placeholder) } diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index cff415e9036ad..796b098e5cced 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -184,16 +184,6 @@ impl fmt::Debug for ty::BoundTy { } } -impl fmt::Debug for ty::Placeholder { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.universe == ty::UniverseIndex::ROOT { - write!(f, "!{:?}", self.bound) - } else { - write!(f, "!{}_{:?}", self.universe.index(), self.bound) - } - } -} - impl<'tcx> fmt::Debug for GenericArg<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.kind() { @@ -295,7 +285,7 @@ TrivialTypeTraversalImpls! { TrivialTypeTraversalAndLiftImpls! { // tidy-alphabetical-start crate::ty::ParamTy, - crate::ty::PlaceholderType, + crate::ty::PlaceholderType<'tcx>, crate::ty::instance::ReifyReason, rustc_hir::def_id::DefId, // tidy-alphabetical-end diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index e71bb9b6bb267..72573d96dc54f 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -39,6 +39,7 @@ pub type FnSig<'tcx> = ir::FnSig>; pub type Binder<'tcx, T> = ir::Binder, T>; pub type EarlyBinder<'tcx, T> = ir::EarlyBinder, T>; pub type TypingMode<'tcx> = ir::TypingMode>; +pub type Placeholder<'tcx, T> = ir::Placeholder, T>; pub trait Article { fn article(&self) -> &'static str; @@ -508,7 +509,7 @@ impl<'tcx> Ty<'tcx> { } #[inline] - pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType) -> Ty<'tcx> { + pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType<'tcx>) -> Ty<'tcx> { Ty::new(tcx, Placeholder(placeholder)) } @@ -957,7 +958,7 @@ impl<'tcx> rustc_type_ir::inherent::Ty> for Ty<'tcx> { Ty::new_param(tcx, param.index, param.name) } - fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType) -> Self { + fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType<'tcx>) -> Self { Ty::new_placeholder(tcx, placeholder) } diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs index da888acc47559..fdaf2d619dd56 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs @@ -980,7 +980,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { fn report_sub_sup_conflict( &self, generic_param_scope: LocalDefId, - var_origin: RegionVariableOrigin, + var_origin: RegionVariableOrigin<'tcx>, sub_origin: SubregionOrigin<'tcx>, sub_region: Region<'tcx>, sup_origin: SubregionOrigin<'tcx>, @@ -1051,7 +1051,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { if sub_region.is_error() | sup_region.is_error() { err.delay_as_bug() } else { err.emit() } } - fn report_inference_failure(&self, var_origin: RegionVariableOrigin) -> Diag<'_> { + fn report_inference_failure(&self, var_origin: RegionVariableOrigin<'tcx>) -> Diag<'_> { let br_string = |br: ty::BoundRegionKind| { let mut s = match br { ty::BoundRegionKind::Named(def_id) => self.tcx.item_name(def_id).to_string(), diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 45357be563998..2aae5f2cde1e8 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -566,13 +566,10 @@ fn plug_infer_with_placeholders<'tcx>( ty, Ty::new_placeholder( self.infcx.tcx, - ty::Placeholder { - universe: self.universe, - bound: ty::BoundTy { - var: self.next_var(), - kind: ty::BoundTyKind::Anon, - }, - }, + ty::Placeholder::new( + self.universe, + ty::BoundTy { var: self.next_var(), kind: ty::BoundTyKind::Anon }, + ), ), ) else { @@ -595,10 +592,10 @@ fn plug_infer_with_placeholders<'tcx>( ct, ty::Const::new_placeholder( self.infcx.tcx, - ty::Placeholder { - universe: self.universe, - bound: ty::BoundConst { var: self.next_var() }, - }, + ty::Placeholder::new( + self.universe, + ty::BoundConst { var: self.next_var() }, + ), ), ) else { @@ -626,13 +623,13 @@ fn plug_infer_with_placeholders<'tcx>( r, ty::Region::new_placeholder( self.infcx.tcx, - ty::Placeholder { - universe: self.universe, - bound: ty::BoundRegion { + ty::Placeholder::new( + self.universe, + ty::BoundRegion { var: self.next_var(), kind: ty::BoundRegionKind::Anon, }, - }, + ), ), ) else { diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index c9f7237b123fb..e536ba4bee9a6 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -756,10 +756,10 @@ fn replace_param_and_infer_args_with_placeholder<'tcx>( self.idx += 1; Ty::new_placeholder( self.tcx, - ty::PlaceholderType { - universe: ty::UniverseIndex::ROOT, - bound: ty::BoundTy { var: idx, kind: ty::BoundTyKind::Anon }, - }, + ty::PlaceholderType::new( + ty::UniverseIndex::ROOT, + ty::BoundTy { var: idx, kind: ty::BoundTyKind::Anon }, + ), ) } else { t.super_fold_with(self) @@ -772,10 +772,7 @@ fn replace_param_and_infer_args_with_placeholder<'tcx>( self.idx += 1; ty::Const::new_placeholder( self.tcx, - ty::PlaceholderConst { - universe: ty::UniverseIndex::ROOT, - bound: ty::BoundConst { var: idx }, - }, + ty::PlaceholderConst::new(ty::UniverseIndex::ROOT, ty::BoundConst { var: idx }), ) } else { c.super_fold_with(self) diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index e1d1ad1b3765f..19ccf6a55bf1a 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -220,9 +220,9 @@ pub fn with_replaced_escaping_bound_vars< /// The inverse of [`BoundVarReplacer`]: replaces placeholders with the bound vars from which they came. pub struct PlaceholderReplacer<'a, 'tcx> { infcx: &'a InferCtxt<'tcx>, - mapped_regions: FxIndexMap, - mapped_types: FxIndexMap, - mapped_consts: FxIndexMap, + mapped_regions: FxIndexMap, ty::BoundRegion>, + mapped_types: FxIndexMap, ty::BoundTy>, + mapped_consts: FxIndexMap, ty::BoundConst>, universe_indices: &'a [Option], current_index: ty::DebruijnIndex, } @@ -230,9 +230,9 @@ pub struct PlaceholderReplacer<'a, 'tcx> { impl<'a, 'tcx> PlaceholderReplacer<'a, 'tcx> { pub fn replace_placeholders>>( infcx: &'a InferCtxt<'tcx>, - mapped_regions: FxIndexMap, - mapped_types: FxIndexMap, - mapped_consts: FxIndexMap, + mapped_regions: FxIndexMap, ty::BoundRegion>, + mapped_types: FxIndexMap, ty::BoundTy>, + mapped_consts: FxIndexMap, ty::BoundConst>, universe_indices: &'a [Option], value: T, ) -> T { diff --git a/compiler/rustc_type_ir/src/binder.rs b/compiler/rustc_type_ir/src/binder.rs index 94b950357e1e4..6472d220a26d9 100644 --- a/compiler/rustc_type_ir/src/binder.rs +++ b/compiler/rustc_type_ir/src/binder.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::marker::PhantomData; use std::ops::{ControlFlow, Deref}; @@ -12,7 +13,7 @@ use crate::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldabl use crate::inherent::*; use crate::lift::Lift; use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; -use crate::{self as ty, DebruijnIndex, Interner}; +use crate::{self as ty, DebruijnIndex, Interner, UniverseIndex}; /// `Binder` is a binder for higher-ranked lifetimes or types. It is part of the /// compiler's representation for things like `for<'a> Fn(&'a isize)` @@ -49,6 +50,56 @@ where } } +/// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are +/// identified by both a universe, as well as a name residing within that universe. Distinct bound +/// regions/types/consts within the same universe simply have an unknown relationship to one +/// another. +#[derive_where(Clone, PartialEq, Ord, Hash; I: Interner, T)] +#[derive_where(PartialOrd; I: Interner, T: Ord)] +#[derive_where(Copy; I: Interner, T: Copy)] +#[cfg_attr( + feature = "nightly", + derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext) +)] +pub struct Placeholder { + pub universe: UniverseIndex, + pub bound: T, + _tcx: PhantomData I>, +} + +impl Placeholder { + pub fn new(universe: UniverseIndex, bound: T) -> Self { + Placeholder { universe, bound, _tcx: PhantomData } + } +} + +impl fmt::Debug for ty::Placeholder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.universe == ty::UniverseIndex::ROOT { + write!(f, "!{:?}", self.bound) + } else { + write!(f, "!{}_{:?}", self.universe.index(), self.bound) + } + } +} + +impl Lift for Placeholder +where + T: Lift, +{ + type Lifted = Placeholder; + + fn lift_to_interner(self, cx: U) -> Option { + Some(Placeholder { + universe: self.universe, + bound: self.bound.lift_to_interner(cx)?, + _tcx: PhantomData, + }) + } +} + +impl Eq for Placeholder {} + #[cfg(feature = "nightly")] macro_rules! impl_binder_encode_decode { ($($t:ty),+ $(,)?) => { diff --git a/compiler/rustc_type_ir/src/ir_print.rs b/compiler/rustc_type_ir/src/ir_print.rs index 82bb8791b846b..db7f685b62649 100644 --- a/compiler/rustc_type_ir/src/ir_print.rs +++ b/compiler/rustc_type_ir/src/ir_print.rs @@ -3,7 +3,8 @@ use std::fmt; use crate::{ AliasTerm, AliasTy, Binder, ClosureKind, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig, HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, - PatternKind, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef, UnevaluatedConst, + PatternKind, Placeholder, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef, + UnevaluatedConst, }; pub trait IrPrint { @@ -32,6 +33,15 @@ where } } +impl fmt::Display for Placeholder +where + I: IrPrint>, +{ + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + >>::print(self, fmt) + } +} + macro_rules! define_debug_via_print { ($($ty:ident),+ $(,)?) => { $( diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index c1e3019612676..1c37727771722 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -61,7 +61,7 @@ pub use InferTy::*; pub use RegionKind::*; pub use TyKind::*; pub use Variance::*; -pub use binder::*; +pub use binder::{Placeholder, *}; pub use canonical::*; pub use const_kind::*; pub use flags::*;