Skip to content

Commit 066a973

Browse files
committed
Overhaul Constraint.
This commit changes it to store a `Region` instead of a `RegionVid` for the `Var` cases: - We avoid having to call `Region::new_var` to re-create `Region`s from `RegionVid`s in a few places, avoiding the interning process, giving a small perf win. (At the cost of the type allowing some invalid combinations of values.) - All the cases now store two `Region`s, so the commit also separates the `ConstraintKind` (a new type) from the `sub` and `sup` arguments in `Constraint`.
1 parent 75a1f47 commit 066a973

File tree

11 files changed

+227
-241
lines changed

11 files changed

+227
-241
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::rc::Rc;
33

44
use rustc_errors::Diag;
55
use rustc_hir::def_id::LocalDefId;
6-
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
6+
use rustc_infer::infer::region_constraints::{Constraint, ConstraintKind, RegionConstraintData};
77
use rustc_infer::infer::{
88
InferCtxt, RegionResolutionError, RegionVariableOrigin, SubregionOrigin, TyCtxtInferExt as _,
99
};
@@ -454,25 +454,24 @@ fn try_extract_error_from_region_constraints<'a, 'tcx>(
454454
(RePlaceholder(a_p), RePlaceholder(b_p)) => a_p.bound == b_p.bound,
455455
_ => a_region == b_region,
456456
};
457-
let mut check =
458-
|constraint: &Constraint<'tcx>, cause: &SubregionOrigin<'tcx>, exact| match *constraint {
459-
Constraint::RegSubReg(sub, sup)
460-
if ((exact && sup == placeholder_region)
461-
|| (!exact && regions_the_same(sup, placeholder_region)))
462-
&& sup != sub =>
463-
{
464-
Some((sub, cause.clone()))
465-
}
466-
Constraint::VarSubReg(vid, sup)
467-
if (exact
468-
&& sup == placeholder_region
469-
&& !universe_of_region(vid).can_name(placeholder_universe))
470-
|| (!exact && regions_the_same(sup, placeholder_region)) =>
471-
{
472-
Some((ty::Region::new_var(infcx.tcx, vid), cause.clone()))
473-
}
474-
_ => None,
475-
};
457+
let mut check = |c: &Constraint<'tcx>, cause: &SubregionOrigin<'tcx>, exact| match c.kind {
458+
ConstraintKind::RegSubReg
459+
if ((exact && c.sup == placeholder_region)
460+
|| (!exact && regions_the_same(c.sup, placeholder_region)))
461+
&& c.sup != c.sub =>
462+
{
463+
Some((c.sub, cause.clone()))
464+
}
465+
ConstraintKind::VarSubReg
466+
if (exact
467+
&& c.sup == placeholder_region
468+
&& !universe_of_region(c.sub.as_var()).can_name(placeholder_universe))
469+
|| (!exact && regions_the_same(c.sup, placeholder_region)) =>
470+
{
471+
Some((c.sub, cause.clone()))
472+
}
473+
_ => None,
474+
};
476475

477476
let mut find_culprit = |exact_match: bool| {
478477
region_constraints

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::infer::canonical::{
2121
Canonical, CanonicalQueryResponse, CanonicalVarValues, Certainty, OriginalQueryValues,
2222
QueryRegionConstraints, QueryResponse,
2323
};
24-
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
24+
use crate::infer::region_constraints::RegionConstraintData;
2525
use crate::infer::{
2626
DefineOpaqueTypes, InferCtxt, InferOk, InferResult, SubregionOrigin, TypeOutlivesConstraint,
2727
};
@@ -105,8 +105,6 @@ impl<'tcx> InferCtxt<'tcx> {
105105
where
106106
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
107107
{
108-
let tcx = self.tcx;
109-
110108
// Select everything, returning errors.
111109
let errors = fulfill_cx.select_all_or_error(self);
112110

@@ -120,7 +118,6 @@ impl<'tcx> InferCtxt<'tcx> {
120118
debug!(?region_obligations);
121119
let region_constraints = self.with_region_constraints(|region_constraints| {
122120
make_query_region_constraints(
123-
tcx,
124121
region_obligations,
125122
region_constraints,
126123
region_assumptions,
@@ -587,7 +584,6 @@ impl<'tcx> InferCtxt<'tcx> {
587584
/// Given the region obligations and constraints scraped from the infcx,
588585
/// creates query region constraints.
589586
pub fn make_query_region_constraints<'tcx>(
590-
tcx: TyCtxt<'tcx>,
591587
outlives_obligations: Vec<TypeOutlivesConstraint<'tcx>>,
592588
region_constraints: &RegionConstraintData<'tcx>,
593589
assumptions: Vec<ty::ArgOutlivesPredicate<'tcx>>,
@@ -600,22 +596,9 @@ pub fn make_query_region_constraints<'tcx>(
600596

601597
let outlives: Vec<_> = constraints
602598
.iter()
603-
.map(|(k, origin)| {
604-
let constraint = match *k {
605-
// Swap regions because we are going from sub (<=) to outlives
606-
// (>=).
607-
Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
608-
ty::Region::new_var(tcx, v2).into(),
609-
ty::Region::new_var(tcx, v1),
610-
),
611-
Constraint::VarSubReg(v1, r2) => {
612-
ty::OutlivesPredicate(r2.into(), ty::Region::new_var(tcx, v1))
613-
}
614-
Constraint::RegSubVar(r1, v2) => {
615-
ty::OutlivesPredicate(ty::Region::new_var(tcx, v2).into(), r1)
616-
}
617-
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
618-
};
599+
.map(|(c, origin)| {
600+
// Swap regions because we are going from sub (<=) to outlives (>=).
601+
let constraint = ty::OutlivesPredicate(c.sup.into(), c.sub);
619602
(constraint, origin.to_constraint_category())
620603
})
621604
.chain(outlives_obligations.into_iter().map(|obl| {

0 commit comments

Comments
 (0)