Skip to content

Commit 569fb43

Browse files
Uplift RegionVid
1 parent 75cb5c5 commit 569fb43

File tree

12 files changed

+75
-64
lines changed

12 files changed

+75
-64
lines changed

compiler/rustc_borrowck/src/facts.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,31 @@ use std::path::Path;
1515
#[derive(Copy, Clone, Debug)]
1616
pub struct RustcFacts;
1717

18+
rustc_index::newtype_index! {
19+
/// A (kinda) newtype of `RegionVid` so we can implement `Atom` on it.
20+
#[orderable]
21+
#[debug_format = "'?{}"]
22+
pub struct PoloniusRegionVid {}
23+
}
24+
25+
impl polonius_engine::Atom for PoloniusRegionVid {
26+
fn index(self) -> usize {
27+
self.as_usize()
28+
}
29+
}
30+
impl From<RegionVid> for PoloniusRegionVid {
31+
fn from(value: RegionVid) -> Self {
32+
Self::from_usize(value.as_usize())
33+
}
34+
}
35+
impl From<PoloniusRegionVid> for RegionVid {
36+
fn from(value: PoloniusRegionVid) -> Self {
37+
Self::from_usize(value.as_usize())
38+
}
39+
}
40+
1841
impl polonius_engine::FactTypes for RustcFacts {
19-
type Origin = RegionVid;
42+
type Origin = PoloniusRegionVid;
2043
type Loan = BorrowIndex;
2144
type Point = LocationIndex;
2245
type Variable = Local;
@@ -119,7 +142,7 @@ trait FactRow {
119142
) -> Result<(), Box<dyn Error>>;
120143
}
121144

122-
impl FactRow for RegionVid {
145+
impl FactRow for PoloniusRegionVid {
123146
fn write(
124147
&self,
125148
out: &mut dyn Write,

compiler/rustc_borrowck/src/polonius/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::TyCtxt;
88
use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
99

1010
use crate::borrow_set::BorrowSet;
11-
use crate::facts::AllFacts;
11+
use crate::facts::{AllFacts, PoloniusRegionVid};
1212
use crate::location::LocationTable;
1313
use crate::type_check::free_region_relations::UniversalRegionRelations;
1414
use crate::universal_regions::UniversalRegions;
@@ -137,7 +137,9 @@ fn emit_universal_region_facts(
137137
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
138138
// added to the existing number of loans, as if they succeeded them in the set.
139139
//
140-
all_facts.universal_region.extend(universal_regions.universal_regions());
140+
all_facts
141+
.universal_region
142+
.extend(universal_regions.universal_regions().map(PoloniusRegionVid::from));
141143
let borrow_count = borrow_set.len();
142144
debug!(
143145
"emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
@@ -148,7 +150,7 @@ fn emit_universal_region_facts(
148150
for universal_region in universal_regions.universal_regions() {
149151
let universal_region_idx = universal_region.index();
150152
let placeholder_loan_idx = borrow_count + universal_region_idx;
151-
all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
153+
all_facts.placeholder.push((universal_region.into(), placeholder_loan_idx.into()));
152154
}
153155

154156
// 2: the universal region relations `outlives` constraints are emitted as
@@ -160,7 +162,7 @@ fn emit_universal_region_facts(
160162
fr1={:?}, fr2={:?}",
161163
fr1, fr2
162164
);
163-
all_facts.known_placeholder_subset.push((fr1, fr2));
165+
all_facts.known_placeholder_subset.push((fr1.into(), fr2.into()));
164166
}
165167
}
166168
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1506,22 +1506,22 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15061506
subset_errors.sort();
15071507
subset_errors.dedup();
15081508

1509-
for (longer_fr, shorter_fr) in subset_errors.into_iter() {
1509+
for &(longer_fr, shorter_fr) in subset_errors.into_iter() {
15101510
debug!(
15111511
"check_polonius_subset_errors: subset_error longer_fr={:?},\
15121512
shorter_fr={:?}",
15131513
longer_fr, shorter_fr
15141514
);
15151515

15161516
let propagated = self.try_propagate_universal_region_error(
1517-
*longer_fr,
1518-
*shorter_fr,
1517+
longer_fr.into(),
1518+
shorter_fr.into(),
15191519
&mut propagated_outlives_requirements,
15201520
);
15211521
if propagated == RegionRelationCheckResult::Error {
15221522
errors_buffer.push(RegionErrorKind::RegionError {
1523-
longer_fr: *longer_fr,
1524-
shorter_fr: *shorter_fr,
1523+
longer_fr: longer_fr.into(),
1524+
shorter_fr: shorter_fr.into(),
15251525
fr_origin: NllRegionVariableOrigin::FreeRegion,
15261526
is_reported: true,
15271527
});

compiler/rustc_borrowck/src/type_check/liveness/polonius.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(super) fn populate_access_facts<'a, 'tcx>(
117117
let universal_regions = &typeck.borrowck_context.universal_regions;
118118
typeck.infcx.tcx.for_each_free_region(&local_decl.ty, |region| {
119119
let region_vid = universal_regions.to_region_vid(region);
120-
facts.use_of_var_derefs_origin.push((local, region_vid));
120+
facts.use_of_var_derefs_origin.push((local, region_vid.into()));
121121
});
122122
}
123123
}
@@ -136,7 +136,7 @@ pub(super) fn add_drop_of_var_derefs_origin<'tcx>(
136136
let universal_regions = &typeck.borrowck_context.universal_regions;
137137
typeck.infcx.tcx.for_each_free_region(kind, |drop_live_region| {
138138
let region_vid = universal_regions.to_region_vid(drop_live_region);
139-
facts.drop_of_var_derefs_origin.push((local, region_vid));
139+
facts.drop_of_var_derefs_origin.push((local, region_vid.into()));
140140
});
141141
}
142142
}

compiler/rustc_borrowck/src/type_check/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,14 @@ fn translate_outlives_facts(typeck: &mut TypeChecker<'_, '_>) {
260260
|constraint: &OutlivesConstraint<'_>| {
261261
if let Some(from_location) = constraint.locations.from_location() {
262262
Either::Left(iter::once((
263-
constraint.sup,
264-
constraint.sub,
263+
constraint.sup.into(),
264+
constraint.sub.into(),
265265
location_table.mid_index(from_location),
266266
)))
267267
} else {
268-
Either::Right(
269-
location_table
270-
.all_points()
271-
.map(move |location| (constraint.sup, constraint.sub, location)),
272-
)
268+
Either::Right(location_table.all_points().map(move |location| {
269+
(constraint.sup.into(), constraint.sub.into(), location)
270+
}))
273271
}
274272
},
275273
));
@@ -2547,7 +2545,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25472545
if let Some(borrow_index) = borrow_set.get_index_of(&location) {
25482546
let region_vid = borrow_region.as_var();
25492547
all_facts.loan_issued_at.push((
2550-
region_vid,
2548+
region_vid.into(),
25512549
borrow_index,
25522550
location_table.mid_index(location),
25532551
));

compiler/rustc_middle/src/ty/context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
134134
type EarlyParamRegion = ty::EarlyParamRegion;
135135
type LateParamRegion = ty::LateParamRegion;
136136
type BoundRegion = ty::BoundRegion;
137-
type InferRegion = ty::RegionVid;
138137
type PlaceholderRegion = ty::PlaceholderRegion;
139138

140139
type ParamEnv = ty::ParamEnv<'tcx>;

compiler/rustc_middle/src/ty/region.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use polonius_engine::Atom;
21
use rustc_data_structures::intern::Interned;
32
use rustc_errors::MultiSpan;
43
use rustc_hir::def_id::DefId;
5-
use rustc_index::Idx;
64
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
75
use rustc_span::symbol::sym;
86
use rustc_span::symbol::{kw, Symbol};
97
use rustc_span::{ErrorGuaranteed, DUMMY_SP};
108
use rustc_type_ir::RegionKind as IrRegionKind;
9+
pub use rustc_type_ir::RegionVid;
1110
use std::ops::Deref;
1211

1312
use crate::ty::{self, BoundVar, TyCtxt, TypeFlags};
@@ -348,21 +347,6 @@ impl std::fmt::Debug for EarlyParamRegion {
348347
}
349348
}
350349

351-
rustc_index::newtype_index! {
352-
/// A **region** (lifetime) **v**ariable **ID**.
353-
#[derive(HashStable)]
354-
#[encodable]
355-
#[orderable]
356-
#[debug_format = "'?{}"]
357-
pub struct RegionVid {}
358-
}
359-
360-
impl Atom for RegionVid {
361-
fn index(self) -> usize {
362-
Idx::index(self)
363-
}
364-
}
365-
366350
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
367351
#[derive(HashStable)]
368352
/// The parameter representation of late-bound function parameters, "some region

compiler/rustc_middle/src/ty/structural_impls.rs

-12
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,6 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for Region<'tcx> {
259259
}
260260
}
261261

262-
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::RegionVid {
263-
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
264-
this: WithInfcx<'_, Infcx, &Self>,
265-
f: &mut core::fmt::Formatter<'_>,
266-
) -> core::fmt::Result {
267-
match this.infcx.universe_of_lt(*this.data) {
268-
Some(universe) => write!(f, "'?{}_{}", this.data.index(), universe.index()),
269-
None => write!(f, "{:?}", this.data),
270-
}
271-
}
272-
}
273-
274262
impl<'tcx, T: DebugWithInfcx<TyCtxt<'tcx>>> DebugWithInfcx<TyCtxt<'tcx>> for ty::Binder<'tcx, T> {
275263
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
276264
this: WithInfcx<'_, Infcx, &Self>,

compiler/rustc_type_ir/src/debug.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ConstVid, InferCtxtLike, Interner, TyVid, UniverseIndex};
1+
use crate::{ConstVid, InferCtxtLike, Interner, RegionVid, TyVid, UniverseIndex};
22

33
use core::fmt;
44
use std::marker::PhantomData;
@@ -16,7 +16,7 @@ impl<I: Interner> InferCtxtLike for NoInfcx<I> {
1616
None
1717
}
1818

19-
fn universe_of_lt(&self, _lt: I::InferRegion) -> Option<UniverseIndex> {
19+
fn universe_of_lt(&self, _lt: RegionVid) -> Option<UniverseIndex> {
2020
None
2121
}
2222

@@ -32,7 +32,7 @@ impl<I: Interner> InferCtxtLike for NoInfcx<I> {
3232
None
3333
}
3434

35-
fn opportunistic_resolve_lt_var(&self, _vid: I::InferRegion) -> Option<I::Region> {
35+
fn opportunistic_resolve_lt_var(&self, _vid: RegionVid) -> Option<I::Region> {
3636
None
3737
}
3838

compiler/rustc_type_ir/src/infcx.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ConstVid, Interner, TyVid, UniverseIndex};
1+
use crate::{ConstVid, Interner, RegionVid, TyVid, UniverseIndex};
22

33
pub trait InferCtxtLike {
44
type Interner: Interner;
@@ -13,10 +13,7 @@ pub trait InferCtxtLike {
1313
/// Resolve `TyVid` to its inferred type, if it has been equated with a non-infer type.
1414
fn probe_ty_var(&self, vid: TyVid) -> Option<<Self::Interner as Interner>::Ty>;
1515

16-
fn universe_of_lt(
17-
&self,
18-
lt: <Self::Interner as Interner>::InferRegion,
19-
) -> Option<UniverseIndex>;
16+
fn universe_of_lt(&self, lt: RegionVid) -> Option<UniverseIndex>;
2017

2118
/// Resolve `InferRegion` to its inferred region, if it has been equated with
2219
/// a non-infer region.
@@ -27,7 +24,7 @@ pub trait InferCtxtLike {
2724
/// var we're trying to resolve. That's why it's called *opportunistic*.
2825
fn opportunistic_resolve_lt_var(
2926
&self,
30-
vid: <Self::Interner as Interner>::InferRegion,
27+
vid: RegionVid,
3128
) -> Option<<Self::Interner as Interner>::Region>;
3229

3330
fn universe_of_ct(&self, ct: ConstVid) -> Option<UniverseIndex>;

compiler/rustc_type_ir/src/interner.rs

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ pub trait Interner:
7979
type EarlyParamRegion: Copy + Debug + Hash + Eq;
8080
type LateParamRegion: Copy + Debug + Hash + Eq;
8181
type BoundRegion: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
82-
type InferRegion: Copy + DebugWithInfcx<Self> + Hash + Eq;
8382
type PlaceholderRegion: PlaceholderLike;
8483

8584
// Predicates

compiler/rustc_type_ir/src/region_kind.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
#[cfg(feature = "nightly")]
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
#[cfg(feature = "nightly")]
4-
use rustc_macros::{TyDecodable, TyEncodable};
4+
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
55
use std::fmt;
66

77
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
88

99
use self::RegionKind::*;
1010

11+
rustc_index::newtype_index! {
12+
/// A **region** **v**ariable **ID**.
13+
#[encodable]
14+
#[orderable]
15+
#[debug_format = "'?{}"]
16+
#[gate_rustc_only]
17+
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
18+
pub struct RegionVid {}
19+
}
20+
21+
impl<I: Interner> DebugWithInfcx<I> for RegionVid {
22+
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
23+
this: WithInfcx<'_, Infcx, &Self>,
24+
f: &mut core::fmt::Formatter<'_>,
25+
) -> core::fmt::Result {
26+
match this.infcx.universe_of_lt(*this.data) {
27+
Some(universe) => write!(f, "'?{}_{}", this.data.index(), universe.index()),
28+
None => write!(f, "{:?}", this.data),
29+
}
30+
}
31+
}
32+
1133
/// Representation of regions. Note that the NLL checker uses a distinct
1234
/// representation of regions. For this reason, it internally replaces all the
1335
/// regions with inference variables -- the index of the variable is then used
@@ -152,7 +174,7 @@ pub enum RegionKind<I: Interner> {
152174
ReStatic,
153175

154176
/// A region variable. Should not exist outside of type inference.
155-
ReVar(I::InferRegion),
177+
ReVar(RegionVid),
156178

157179
/// A placeholder region -- the higher-ranked version of `ReLateParam`.
158180
/// Should not exist outside of type inference.
@@ -251,7 +273,6 @@ where
251273
I::EarlyParamRegion: HashStable<CTX>,
252274
I::BoundRegion: HashStable<CTX>,
253275
I::LateParamRegion: HashStable<CTX>,
254-
I::InferRegion: HashStable<CTX>,
255276
I::PlaceholderRegion: HashStable<CTX>,
256277
{
257278
#[inline]

0 commit comments

Comments
 (0)