Skip to content

Commit 2471431

Browse files
committed
Move is_free and is_free_or_static to Region, change resolve_var to resolve_region, and remove RootEmptyRegion
1 parent 1517f5d commit 2471431

File tree

7 files changed

+43
-50
lines changed

7 files changed

+43
-50
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
495495
}
496496
}
497497

498-
NllRegionVariableOrigin::RootEmptyRegion
499-
| NllRegionVariableOrigin::Existential { .. } => {
498+
NllRegionVariableOrigin::Existential { .. } => {
500499
// For existential, regions, nothing to do.
501500
}
502501
}
@@ -1410,8 +1409,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
14101409
self.check_bound_universal_region(fr, placeholder, errors_buffer);
14111410
}
14121411

1413-
NllRegionVariableOrigin::RootEmptyRegion
1414-
| NllRegionVariableOrigin::Existential { .. } => {
1412+
NllRegionVariableOrigin::Existential { .. } => {
14151413
// nothing to check here
14161414
}
14171415
}
@@ -1513,8 +1511,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
15131511
self.check_bound_universal_region(fr, placeholder, errors_buffer);
15141512
}
15151513

1516-
NllRegionVariableOrigin::RootEmptyRegion
1517-
| NllRegionVariableOrigin::Existential { .. } => {
1514+
NllRegionVariableOrigin::Existential { .. } => {
15181515
// nothing to check here
15191516
}
15201517
}
@@ -1788,9 +1785,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17881785
universe1.cannot_name(placeholder.universe)
17891786
}
17901787

1791-
NllRegionVariableOrigin::RootEmptyRegion
1792-
| NllRegionVariableOrigin::FreeRegion
1793-
| NllRegionVariableOrigin::Existential { .. } => false,
1788+
NllRegionVariableOrigin::FreeRegion | NllRegionVariableOrigin::Existential { .. } => {
1789+
false
1790+
}
17941791
}
17951792
}
17961793

@@ -2152,8 +2149,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21522149
let blame_source = match from_region_origin {
21532150
NllRegionVariableOrigin::FreeRegion
21542151
| NllRegionVariableOrigin::Existential { from_forall: false } => true,
2155-
NllRegionVariableOrigin::RootEmptyRegion
2156-
| NllRegionVariableOrigin::Placeholder(_)
2152+
NllRegionVariableOrigin::Placeholder(_)
21572153
| NllRegionVariableOrigin::Existential { from_forall: true } => false,
21582154
};
21592155

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
@@ -4,7 +4,7 @@
44
//! and use that to decide when one free region outlives another, and so forth.
55
66
use rustc_data_structures::transitive_relation::TransitiveRelation;
7-
use rustc_middle::ty::{self, Lift, Region, TyCtxt};
7+
use rustc_middle::ty::{Lift, Region, TyCtxt};
88

99
/// Combines a `FreeRegionMap` and a `TyCtxt`.
1010
///
@@ -49,7 +49,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
4949
// (with the exception that `'static: 'x` is not notable)
5050
pub fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
5151
debug!("relate_regions(sub={:?}, sup={:?})", sub, sup);
52-
if self.is_free_or_static(sub) && self.is_free(sup) {
52+
if sub.is_free_or_static() && sup.is_free() {
5353
self.relation.add(sub, sup)
5454
}
5555
}
@@ -68,7 +68,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
6868
r_a: Region<'tcx>,
6969
r_b: Region<'tcx>,
7070
) -> bool {
71-
assert!(self.is_free_or_static(r_a) && self.is_free_or_static(r_b));
71+
assert!(r_a.is_free_or_static() && r_b.is_free_or_static());
7272
let re_static = tcx.lifetimes.re_static;
7373
if self.check_relation(re_static, r_b) {
7474
// `'a <= 'static` is always true, and not stored in the
@@ -85,20 +85,6 @@ impl<'tcx> FreeRegionMap<'tcx> {
8585
r_a == r_b || self.relation.contains(r_a, r_b)
8686
}
8787

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

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

+15-13
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)]
@@ -140,7 +139,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
140139
/// empty region. The `expansion` phase will grow this larger.
141140
fn construct_var_data(&self, tcx: TyCtxt<'tcx>) -> LexicalRegionResolutions<'tcx> {
142141
LexicalRegionResolutions {
143-
error_region: tcx.lifetimes.re_static,
144142
values: IndexVec::from_fn_n(
145143
|vid| {
146144
let vid_universe = self.var_infos[vid].universe;
@@ -310,7 +308,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
310308

311309
// Check for the case where we know that `'b: 'static` -- in that case,
312310
// `a <= b` for all `a`.
313-
let b_free_or_static = self.region_rels.free_regions.is_free_or_static(b);
311+
let b_free_or_static = b.is_free_or_static();
314312
if b_free_or_static && sub_free_regions(tcx.lifetimes.re_static, b) {
315313
return true;
316314
}
@@ -320,7 +318,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
320318
// `lub` relationship defined below, since sometimes the "lub"
321319
// is actually the `postdom_upper_bound` (see
322320
// `TransitiveRelation` for more details).
323-
let a_free_or_static = self.region_rels.free_regions.is_free_or_static(a);
321+
let a_free_or_static = a.is_free_or_static();
324322
if a_free_or_static && b_free_or_static {
325323
return sub_free_regions(a, b);
326324
}
@@ -864,10 +862,7 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
864862
where
865863
T: TypeFoldable<'tcx>,
866864
{
867-
tcx.fold_regions(value, |r, _db| match *r {
868-
ty::ReVar(rid) => self.resolve_var(rid),
869-
_ => r,
870-
})
865+
tcx.fold_regions(value, |r, _db| self.resolve_region(tcx, r))
871866
}
872867

873868
fn value(&self, rid: RegionVid) -> &VarValue<'tcx> {
@@ -878,12 +873,19 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
878873
&mut self.values[rid]
879874
}
880875

881-
pub fn resolve_var(&self, rid: RegionVid) -> ty::Region<'tcx> {
882-
let result = match self.values[rid] {
883-
VarValue::Value(r) => r,
884-
VarValue::ErrorValue => self.error_region,
876+
pub(crate) fn resolve_region(
877+
&self,
878+
tcx: TyCtxt<'tcx>,
879+
r: ty::Region<'tcx>,
880+
) -> ty::Region<'tcx> {
881+
let result = match *r {
882+
ty::ReVar(rid) => match self.values[rid] {
883+
VarValue::Value(r) => r,
884+
VarValue::ErrorValue => tcx.lifetimes.re_static,
885+
},
886+
_ => r,
885887
};
886-
debug!("resolve_var({:?}) = {:?}", rid, result);
888+
debug!("resolve_region({:?}) = {:?}", r, result);
887889
result
888890
}
889891
}

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
@@ -206,13 +206,13 @@ impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
206206

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

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)