Skip to content

Commit

Permalink
Revert "Remove region_bound_pairs"
Browse files Browse the repository at this point in the history
This reverts commit 485c6f4.
  • Loading branch information
compiler-errors committed Jan 25, 2025
1 parent 485c6f4 commit 415429f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 47 deletions.
14 changes: 10 additions & 4 deletions compiler/rustc_borrowck/src/type_check/constraint_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_hir::def_id::DefId;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
Expand Down Expand Up @@ -34,9 +35,10 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
/// `process_registered_region_obligations` has some special-cased
/// logic expecting to see (e.g.) `ReStatic`, and if we supplied
/// our special inference variable there, we would mess that up.
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
param_env: ty::ParamEnv<'tcx>,
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
locations: Locations,
span: Span,
category: ConstraintCategory<'tcx>,
Expand All @@ -48,9 +50,10 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
pub(crate) fn new(
infcx: &'a InferCtxt<'tcx>,
universal_regions: &'a UniversalRegions<'tcx>,
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
param_env: ty::ParamEnv<'tcx>,
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
locations: Locations,
span: Span,
category: ConstraintCategory<'tcx>,
Expand All @@ -60,9 +63,10 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
infcx,
tcx: infcx.tcx,
universal_regions,
region_bound_pairs,
implicit_region_bound,
param_env,
known_type_outlives,
known_type_outlives_obligations,
locations,
span,
category,
Expand Down Expand Up @@ -131,8 +135,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
let ConstraintConversion {
tcx,
infcx,
region_bound_pairs,
implicit_region_bound,
known_type_outlives: known_type_outlives_obligations,
known_type_outlives_obligations,
..
} = *self;

Expand Down Expand Up @@ -174,6 +179,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
TypeOutlives::new(
&mut *self,
tcx,
region_bound_pairs,
Some(implicit_region_bound),
known_type_outlives_obligations,
)
Expand Down
44 changes: 26 additions & 18 deletions compiler/rustc_borrowck/src/type_check/free_region_relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
use rustc_hir::def::DefKind;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::region_constraints::GenericKind;
use rustc_infer::infer::{InferCtxt, outlives};
use rustc_infer::traits::ScrubbedTraitError;
use rustc_middle::mir::ConstraintCategory;
Expand Down Expand Up @@ -42,7 +44,8 @@ type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;

pub(crate) struct CreateResult<'tcx> {
pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
pub(crate) known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
pub(crate) region_bound_pairs: RegionBoundPairs<'tcx>,
pub(crate) known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
pub(crate) normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
}

Expand All @@ -59,7 +62,7 @@ pub(crate) fn create<'tcx>(
implicit_region_bound,
constraints,
universal_regions,
known_type_outlives: vec![],
region_bound_pairs: Default::default(),
outlives: Default::default(),
inverse_outlives: Default::default(),
}
Expand Down Expand Up @@ -186,8 +189,8 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {

// outputs:
outlives: TransitiveRelationBuilder<RegionVid>,
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
inverse_outlives: TransitiveRelationBuilder<RegionVid>,
region_bound_pairs: RegionBoundPairs<'tcx>,
}

impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
Expand Down Expand Up @@ -225,9 +228,15 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {

// Normalize the assumptions we use to borrowck the program.
let mut constraints = vec![];
let mut known_type_outlives_obligations = vec![];
for bound in param_env.caller_bounds() {
if let Some(outlives) = bound.as_type_outlives_clause() {
self.normalize_and_push_type_outlives_obligation(outlives, span, &mut constraints);
self.normalize_and_push_type_outlives_obligation(
outlives,
span,
&mut known_type_outlives_obligations,
&mut constraints,
);
};
}

Expand All @@ -242,8 +251,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
// - Normalize the type. This will create some region
// constraints, which we buffer up because we are
// not ready to process them yet.
// - Then compute the implied bounds, updating the
// known outlives types and free region regions.
// - Then compute the implied bounds. This will adjust
// the `region_bound_pairs` and so forth.
// - After this is done, we'll process the constraints, once
// the `relations` is built.
let mut normalized_inputs_and_output =
Expand Down Expand Up @@ -314,9 +323,10 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
constraint_conversion::ConstraintConversion::new(
self.infcx,
&self.universal_regions,
&self.region_bound_pairs,
self.implicit_region_bound,
param_env,
&self.known_type_outlives,
&known_type_outlives_obligations,
Locations::All(span),
span,
ConstraintCategory::Internal,
Expand All @@ -331,15 +341,17 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
outlives: self.outlives.freeze(),
inverse_outlives: self.inverse_outlives.freeze(),
}),
known_type_outlives: self.known_type_outlives,
known_type_outlives_obligations,
region_bound_pairs: self.region_bound_pairs,
normalized_inputs_and_output,
}
}

fn normalize_and_push_type_outlives_obligation(
&mut self,
&self,
mut outlives: ty::PolyTypeOutlivesPredicate<'tcx>,
span: Span,
known_type_outlives_obligations: &mut Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
constraints: &mut Vec<&QueryRegionConstraints<'tcx>>,
) {
// In the new solver, normalize the type-outlives obligation assumptions.
Expand Down Expand Up @@ -370,7 +382,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
}
}

self.known_type_outlives.push(outlives);
known_type_outlives_obligations.push(outlives);
}

/// Update the type of a single local, which should represent
Expand Down Expand Up @@ -416,17 +428,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
}

OutlivesBound::RegionSubParam(r_a, param_b) => {
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
Ty::new_param(self.infcx.tcx, param_b.index, param_b.name),
r_a,
)));
self.region_bound_pairs
.insert(ty::OutlivesPredicate(GenericKind::Param(param_b), r_a));
}

OutlivesBound::RegionSubAlias(r_a, alias_b) => {
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
Ty::new_alias(self.infcx.tcx, alias_b.kind(self.infcx.tcx), alias_b),
r_a,
)));
self.region_bound_pairs
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_hir::def_id::LocalDefId;
use rustc_hir::lang_items::LangItem;
use rustc_index::{IndexSlice, IndexVec};
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::region_constraints::RegionConstraintData;
use rustc_infer::infer::{
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
Expand Down Expand Up @@ -128,8 +129,9 @@ pub(crate) fn type_check<'a, 'tcx>(

let CreateResult {
universal_region_relations,
region_bound_pairs,
normalized_inputs_and_output,
known_type_outlives,
known_type_outlives_obligations,
} = free_region_relations::create(
infcx,
infcx.param_env,
Expand Down Expand Up @@ -157,7 +159,8 @@ pub(crate) fn type_check<'a, 'tcx>(
last_span: body.span,
body,
user_type_annotations: &body.user_type_annotations,
known_type_outlives,
region_bound_pairs,
known_type_outlives_obligations,
implicit_region_bound,
reported_errors: Default::default(),
universal_regions: &universal_region_relations.universal_regions,
Expand Down Expand Up @@ -552,7 +555,8 @@ struct TypeChecker<'a, 'tcx> {
/// User type annotations are shared between the main MIR and the MIR of
/// all of the promoted items.
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
region_bound_pairs: RegionBoundPairs<'tcx>,
known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
implicit_region_bound: ty::Region<'tcx>,
reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
universal_regions: &'a UniversalRegions<'tcx>,
Expand Down Expand Up @@ -744,9 +748,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
constraint_conversion::ConstraintConversion::new(
self.infcx,
self.universal_regions,
&self.region_bound_pairs,
self.implicit_region_bound,
self.infcx.param_env,
&self.known_type_outlives,
&self.known_type_outlives_obligations,
locations,
locations.span(self.body),
category,
Expand Down Expand Up @@ -2526,9 +2531,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
constraint_conversion::ConstraintConversion::new(
self.infcx,
self.universal_regions,
&self.region_bound_pairs,
self.implicit_region_bound,
self.infcx.param_env,
&self.known_type_outlives,
&self.known_type_outlives_obligations,
locations,
self.body.span, // irrelevant; will be overridden.
ConstraintCategory::Boring, // same as above.
Expand Down
14 changes: 11 additions & 3 deletions compiler/rustc_infer/src/infer/outlives/env.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::transitive_relation::TransitiveRelationBuilder;
use rustc_middle::{bug, ty};

use crate::infer::GenericKind;
use crate::infer::free_regions::FreeRegionMap;

/// The `OutlivesEnvironment` collects information about what outlives
/// what in a given type-checking setting. For example, if we have a
/// where-clause like `where T: 'a` in scope, then the
/// `OutlivesEnvironment` would record that. Similarly, it contains
/// methods for processing and adding implied bounds into the outlives
/// `OutlivesEnvironment` would record that (in its
/// `region_bound_pairs` field). Similarly, it contains methods for
/// processing and adding implied bounds into the outlives
/// environment.
///
/// Other code at present does not typically take a
Expand All @@ -28,6 +31,11 @@ pub struct OutlivesEnvironment<'tcx> {
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
}

/// "Region-bound pairs" tracks outlives relations that are known to
/// be true, either because of explicit where-clauses like `T: 'a` or
/// because of implied bounds.
pub type RegionBoundPairs<'tcx> = FxIndexSet<ty::OutlivesPredicate<'tcx, GenericKind<'tcx>>>;

impl<'tcx> OutlivesEnvironment<'tcx> {
/// Create a new `OutlivesEnvironment` from normalized outlives bounds.
pub fn from_normalized_bounds(
Expand Down Expand Up @@ -85,7 +93,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
&self.free_region_map
}

/// Borrows current `known_type_outlives`.
/// Borrows current `region_bound_pairs`.
pub fn known_type_outlives(&self) -> &[ty::PolyTypeOutlivesPredicate<'tcx>] {
&self.known_type_outlives
}
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_infer/src/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use smallvec::smallvec;
use tracing::{debug, instrument};

use super::env::OutlivesEnvironment;
use crate::infer::outlives::env::RegionBoundPairs;
use crate::infer::outlives::verify::VerifyBoundCx;
use crate::infer::resolve::OpportunisticRegionResolver;
use crate::infer::snapshot::undo_log::UndoLog;
Expand Down Expand Up @@ -168,9 +169,12 @@ impl<'tcx> InferCtxt<'tcx> {

debug!(?sup_type, ?sub_region, ?origin);

let region_bound_pairs = &Default::default();
let outlives = &mut TypeOutlives::new(
self,
self.tcx,
// TODO:

Check failure on line 176 in compiler/rustc_infer/src/infer/outlives/obligations.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
region_bound_pairs,
None,
outlives_env.known_type_outlives(),
);
Expand Down Expand Up @@ -225,13 +229,19 @@ where
pub fn new(
delegate: D,
tcx: TyCtxt<'tcx>,
region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
implicit_region_bound: Option<ty::Region<'tcx>>,
known_type_outlives: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
) -> Self {
Self {
delegate,
tcx,
verify_bound: VerifyBoundCx::new(tcx, implicit_region_bound, known_type_outlives),
verify_bound: VerifyBoundCx::new(
tcx,
region_bound_pairs,
implicit_region_bound,
caller_bounds,
),
}
}

Expand Down
Loading

0 comments on commit 415429f

Please sign in to comment.