Skip to content

Commit 85e0498

Browse files
committed
Move is_free and is_free_or_static to Region, change resolve_var to resolve_region, and remove RootEmptyRegion
1 parent 9a6fa4f commit 85e0498

File tree

7 files changed

+43
-47
lines changed

7 files changed

+43
-47
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
493493
}
494494
}
495495

496-
NllRegionVariableOrigin::RootEmptyRegion
497-
| NllRegionVariableOrigin::Existential { .. } => {
496+
NllRegionVariableOrigin::Existential { .. } => {
498497
// For existential, regions, nothing to do.
499498
}
500499
}
@@ -1408,8 +1407,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
14081407
self.check_bound_universal_region(fr, placeholder, errors_buffer);
14091408
}
14101409

1411-
NllRegionVariableOrigin::RootEmptyRegion
1412-
| NllRegionVariableOrigin::Existential { .. } => {
1410+
NllRegionVariableOrigin::Existential { .. } => {
14131411
// nothing to check here
14141412
}
14151413
}
@@ -1511,8 +1509,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15111509
self.check_bound_universal_region(fr, placeholder, errors_buffer);
15121510
}
15131511

1514-
NllRegionVariableOrigin::RootEmptyRegion
1515-
| NllRegionVariableOrigin::Existential { .. } => {
1512+
NllRegionVariableOrigin::Existential { .. } => {
15161513
// nothing to check here
15171514
}
15181515
}
@@ -1786,9 +1783,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17861783
universe1.cannot_name(placeholder.universe)
17871784
}
17881785

1789-
NllRegionVariableOrigin::RootEmptyRegion
1790-
| NllRegionVariableOrigin::FreeRegion
1791-
| NllRegionVariableOrigin::Existential { .. } => false,
1786+
NllRegionVariableOrigin::FreeRegion | NllRegionVariableOrigin::Existential { .. } => {
1787+
false
1788+
}
17921789
}
17931790
}
17941791

@@ -2150,8 +2147,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21502147
let blame_source = match from_region_origin {
21512148
NllRegionVariableOrigin::FreeRegion
21522149
| NllRegionVariableOrigin::Existential { from_forall: false } => true,
2153-
NllRegionVariableOrigin::RootEmptyRegion
2154-
| NllRegionVariableOrigin::Placeholder(_)
2150+
NllRegionVariableOrigin::Placeholder(_)
21552151
| NllRegionVariableOrigin::Existential { from_forall: true } => false,
21562152
};
21572153

compiler/rustc_borrowck/src/universal_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
503503

504504
let root_empty = self
505505
.infcx
506-
.next_nll_region_var(NllRegionVariableOrigin::RootEmptyRegion)
506+
.next_nll_region_var(NllRegionVariableOrigin::Existential { from_forall: true })
507507
.to_region_vid();
508508

509509
UniversalRegions {

compiler/rustc_infer/src/infer/free_regions.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use rustc_data_structures::transitive_relation::TransitiveRelation;
77
use rustc_hir::def_id::DefId;
8-
use rustc_middle::ty::{self, Lift, Region, TyCtxt};
8+
use rustc_middle::ty::{Lift, Region, TyCtxt};
99

1010
/// Combines a `FreeRegionMap` and a `TyCtxt`.
1111
///
@@ -53,7 +53,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
5353
// (with the exception that `'static: 'x` is not notable)
5454
pub fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
5555
debug!("relate_regions(sub={:?}, sup={:?})", sub, sup);
56-
if self.is_free_or_static(sub) && self.is_free(sup) {
56+
if sub.is_free_or_static() && sup.is_free() {
5757
self.relation.add(sub, sup)
5858
}
5959
}
@@ -72,7 +72,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
7272
r_a: Region<'tcx>,
7373
r_b: Region<'tcx>,
7474
) -> bool {
75-
assert!(self.is_free_or_static(r_a) && self.is_free_or_static(r_b));
75+
assert!(r_a.is_free_or_static() && r_b.is_free_or_static());
7676
let re_static = tcx.lifetimes.re_static;
7777
if self.check_relation(re_static, r_b) {
7878
// `'a <= 'static` is always true, and not stored in the
@@ -89,20 +89,6 @@ impl<'tcx> FreeRegionMap<'tcx> {
8989
r_a == r_b || self.relation.contains(r_a, r_b)
9090
}
9191

92-
/// True for free regions other than `'static`.
93-
pub fn is_free(&self, r: Region<'_>) -> bool {
94-
matches!(*r, ty::ReEarlyBound(_) | ty::ReFree(_))
95-
}
96-
97-
/// True if `r` is a free region or static of the sort that this
98-
/// free region map can be used with.
99-
pub fn is_free_or_static(&self, r: Region<'_>) -> bool {
100-
match *r {
101-
ty::ReStatic => true,
102-
_ => self.is_free(r),
103-
}
104-
}
105-
10692
/// Computes the least-upper-bound of two free regions. In some
10793
/// cases, this is more conservative than necessary, in order to
10894
/// avoid making arbitrary choices. See
@@ -114,8 +100,8 @@ impl<'tcx> FreeRegionMap<'tcx> {
114100
r_b: Region<'tcx>,
115101
) -> Region<'tcx> {
116102
debug!("lub_free_regions(r_a={:?}, r_b={:?})", r_a, r_b);
117-
assert!(self.is_free(r_a));
118-
assert!(self.is_free(r_b));
103+
assert!(r_a.is_free());
104+
assert!(r_b.is_free());
119105
let result = if r_a == r_b {
120106
r_a
121107
} else {

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub(crate) fn resolve<'tcx>(
4747
#[derive(Clone)]
4848
pub struct LexicalRegionResolutions<'tcx> {
4949
pub(crate) values: IndexVec<RegionVid, VarValue<'tcx>>,
50-
pub(crate) error_region: ty::Region<'tcx>,
5150
}
5251

5352
#[derive(Copy, Clone, Debug)]
@@ -144,7 +143,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
144143
/// empty region. The `expansion` phase will grow this larger.
145144
fn construct_var_data(&self, tcx: TyCtxt<'tcx>) -> LexicalRegionResolutions<'tcx> {
146145
LexicalRegionResolutions {
147-
error_region: tcx.lifetimes.re_static,
148146
values: IndexVec::from_fn_n(
149147
|vid| {
150148
let vid_universe = self.var_infos[vid].universe;
@@ -314,7 +312,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
314312

315313
// Check for the case where we know that `'b: 'static` -- in that case,
316314
// `a <= b` for all `a`.
317-
let b_free_or_static = self.region_rels.free_regions.is_free_or_static(b);
315+
let b_free_or_static = b.is_free_or_static();
318316
if b_free_or_static && sub_free_regions(tcx.lifetimes.re_static, b) {
319317
return true;
320318
}
@@ -324,7 +322,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
324322
// `lub` relationship defined below, since sometimes the "lub"
325323
// is actually the `postdom_upper_bound` (see
326324
// `TransitiveRelation` for more details).
327-
let a_free_or_static = self.region_rels.free_regions.is_free_or_static(a);
325+
let a_free_or_static = a.is_free_or_static();
328326
if a_free_or_static && b_free_or_static {
329327
return sub_free_regions(a, b);
330328
}
@@ -869,7 +867,7 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
869867
T: TypeFoldable<'tcx>,
870868
{
871869
tcx.fold_regions(value, |r, _db| match *r {
872-
ty::ReVar(rid) => self.resolve_var(rid),
870+
ty::ReVar(_) => self.resolve_region(tcx, r),
873871
_ => r,
874872
})
875873
}
@@ -882,12 +880,19 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
882880
&mut self.values[rid]
883881
}
884882

885-
pub fn resolve_var(&self, rid: RegionVid) -> ty::Region<'tcx> {
886-
let result = match self.values[rid] {
887-
VarValue::Value(r) => r,
888-
VarValue::ErrorValue => self.error_region,
883+
pub(crate) fn resolve_region(
884+
&self,
885+
tcx: TyCtxt<'tcx>,
886+
r: ty::Region<'tcx>,
887+
) -> ty::Region<'tcx> {
888+
let result = match *r {
889+
ty::ReVar(rid) => match self.values[rid] {
890+
VarValue::Value(r) => r,
891+
VarValue::ErrorValue => tcx.lifetimes.re_static,
892+
},
893+
_ => r,
889894
};
890-
debug!("resolve_var({:?}) = {:?}", rid, result);
895+
debug!("resolve_region({:?}) = {:?}", r, result);
891896
result
892897
}
893898
}

compiler/rustc_infer/src/infer/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,6 @@ pub enum NllRegionVariableOrigin {
466466
/// from a `for<'a> T` binder). Meant to represent "any region".
467467
Placeholder(ty::PlaceholderRegion),
468468

469-
/// The variable we create to represent `'empty(U0)`.
470-
RootEmptyRegion,
471-
472469
Existential {
473470
/// If this is true, then this variable was created to represent a lifetime
474471
/// bound in a `for` binder. For example, it might have been created to
@@ -1250,7 +1247,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12501247
};
12511248

12521249
let lexical_region_resolutions = LexicalRegionResolutions {
1253-
error_region: self.tcx.lifetimes.re_static,
12541250
values: rustc_index::vec::IndexVec::from_elem_n(
12551251
crate::infer::lexical_region_resolve::VarValue::Value(self.tcx.lifetimes.re_erased),
12561252
var_infos.len(),

compiler/rustc_infer/src/infer/resolve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
205205

206206
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
207207
match *r {
208-
ty::ReVar(rid) => Ok(self
208+
ty::ReVar(_) => Ok(self
209209
.infcx
210210
.lexical_region_resolutions
211211
.borrow()
212212
.as_ref()
213213
.expect("region resolution not performed")
214-
.resolve_var(rid)),
214+
.resolve_region(self.infcx.tcx, r)),
215215
_ => Ok(r),
216216
}
217217
}

compiler/rustc_middle/src/ty/sty.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,19 @@ impl<'tcx> Region<'tcx> {
15701570
_ => bug!("free_region_binding_scope invoked on inappropriate region: {:?}", self),
15711571
}
15721572
}
1573+
1574+
/// True for free regions other than `'static`.
1575+
pub fn is_free(self) -> bool {
1576+
matches!(*self, ty::ReEarlyBound(_) | ty::ReFree(_))
1577+
}
1578+
1579+
/// True if `self` is a free region or static.
1580+
pub fn is_free_or_static(self) -> bool {
1581+
match *self {
1582+
ty::ReStatic => true,
1583+
_ => self.is_free(),
1584+
}
1585+
}
15731586
}
15741587

15751588
/// Type utilities

0 commit comments

Comments
 (0)