Skip to content

Commit 7024dc5

Browse files
committed
Overhaul RegionKind and Region.
Specifically, change `Region` from this: ``` pub type Region<'tcx> = &'tcx RegionKind; ``` to this: ``` pub struct Region<'tcx>(&'tcx Interned<RegionKind>); ``` This now matches `Ty` and `Predicate` more closely. Things to note - Regions have always been interned, but we haven't been using pointer-based `Eq` and `Hash`. This is now happening. - I chose to impl `Deref` for `Region` because it makes pattern matching a lot nicer, and `Region` can be viewed as just a smart wrapper for `RegionKind`. - Various methods are moved from `RegionKind` to `Region`. - There is a lot of tedious sigil changes. - A couple of types like `HighlightBuilder`, `RegionHighlightMode` now have a `'tcx` lifetime because they hold a `Ty<'tcx>`, so they can call `mk_region`. - A couple of test outputs change slightly, I'm not sure why, but the new outputs are a little better.
1 parent 925ec0d commit 7024dc5

File tree

80 files changed

+443
-346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+443
-346
lines changed

compiler/rustc_borrowck/src/constraint_generation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> {
6060

6161
/// We sometimes have `region` within an rvalue, or within a
6262
/// call. Make them live at the location where they appear.
63-
fn visit_region(&mut self, region: &ty::Region<'tcx>, location: Location) {
64-
self.add_regular_live_constraint(*region, location);
63+
fn visit_region(&mut self, region: ty::Region<'tcx>, location: Location) {
64+
self.add_regular_live_constraint(region, location);
6565
self.super_region(region);
6666
}
6767

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ fn try_extract_error_from_fulfill_cx<'tcx>(
356356
})?;
357357

358358
debug!(?sub_region, "cause = {:#?}", cause);
359-
let nice_error = match (error_region, sub_region) {
360-
(Some(error_region), &ty::ReVar(vid)) => NiceRegionError::new(
359+
let nice_error = match (error_region, *sub_region) {
360+
(Some(error_region), ty::ReVar(vid)) => NiceRegionError::new(
361361
infcx,
362362
RegionResolutionError::SubSupConflict(
363363
vid,
@@ -374,7 +374,7 @@ fn try_extract_error_from_fulfill_cx<'tcx>(
374374
RegionResolutionError::ConcreteFailure(cause.clone(), error_region, placeholder_region),
375375
),
376376
// Note universe here is wrong...
377-
(None, &ty::ReVar(vid)) => NiceRegionError::new(
377+
(None, ty::ReVar(vid)) => NiceRegionError::new(
378378
infcx,
379379
RegionResolutionError::UpperBoundUniverseConflict(
380380
vid,

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
497497
// We need to add synthesized lifetimes where appropriate. We do
498498
// this by hooking into the pretty printer and telling it to label the
499499
// lifetimes without names with the value `'0`.
500-
match ty.kind() {
501-
ty::Ref(
502-
ty::RegionKind::ReLateBound(_, ty::BoundRegion { kind: br, .. })
503-
| ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }),
504-
_,
505-
_,
506-
) => printer.region_highlight_mode.highlighting_bound_region(*br, counter),
507-
_ => {}
500+
if let ty::Ref(region, ..) = ty.kind() {
501+
match **region {
502+
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
503+
| ty::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
504+
printer.region_highlight_mode.highlighting_bound_region(br, counter)
505+
}
506+
_ => {}
507+
}
508508
}
509509

510510
let _ = ty.print(printer);
@@ -517,19 +517,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
517517
let mut s = String::new();
518518
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
519519

520-
let region = match ty.kind() {
521-
ty::Ref(region, _, _) => {
522-
match region {
523-
ty::RegionKind::ReLateBound(_, ty::BoundRegion { kind: br, .. })
524-
| ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
525-
printer.region_highlight_mode.highlighting_bound_region(*br, counter)
526-
}
527-
_ => {}
520+
let region = if let ty::Ref(region, ..) = ty.kind() {
521+
match **region {
522+
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
523+
| ty::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
524+
printer.region_highlight_mode.highlighting_bound_region(br, counter)
528525
}
529-
530-
region
526+
_ => {}
531527
}
532-
_ => bug!("ty for annotation of borrow region is not a reference"),
528+
region
529+
} else {
530+
bug!("ty for annotation of borrow region is not a reference");
533531
};
534532

535533
let _ = region.print(printer);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
139139

140140
/// Returns `true` if a closure is inferred to be an `FnMut` closure.
141141
fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
142-
if let Some(ty::ReFree(free_region)) = self.to_error_region(fr) {
142+
if let Some(ty::ReFree(free_region)) = self.to_error_region(fr).as_deref() {
143143
if let ty::BoundRegionKind::BrEnv = free_region.bound_region {
144144
if let DefiningTy::Closure(_, substs) =
145145
self.regioncx.universal_regions().defining_ty
@@ -628,8 +628,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
628628
fr_name: RegionName,
629629
outlived_fr: RegionVid,
630630
) {
631-
if let (Some(f), Some(ty::RegionKind::ReStatic)) =
632-
(self.to_error_region(fr), self.to_error_region(outlived_fr))
631+
if let (Some(f), Some(ty::ReStatic)) =
632+
(self.to_error_region(fr), self.to_error_region(outlived_fr).as_deref())
633633
{
634634
if let Some(&ty::Opaque(did, substs)) = self
635635
.infcx
@@ -652,7 +652,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
652652
bound.kind().skip_binder()
653653
{
654654
let r = r.subst(self.infcx.tcx, substs);
655-
if let ty::RegionKind::ReStatic = r {
655+
if r.is_static() {
656656
found = true;
657657
break;
658658
} else {

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
264264
let tcx = self.infcx.tcx;
265265

266266
debug!("give_region_a_name: error_region = {:?}", error_region);
267-
match error_region {
267+
match *error_region {
268268
ty::ReEarlyBound(ebr) => {
269269
if ebr.has_name() {
270270
let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
@@ -433,7 +433,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
433433
span: Span,
434434
counter: usize,
435435
) -> RegionNameHighlight {
436-
let mut highlight = RegionHighlightMode::default();
436+
let mut highlight = RegionHighlightMode::new(self.infcx.tcx);
437437
highlight.highlighting_region_vid(needle_fr, counter);
438438
let type_name =
439439
self.infcx.extract_inference_diagnostics_data(ty.into(), Some(highlight)).name;
@@ -818,7 +818,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
818818
return None;
819819
}
820820

821-
let mut highlight = RegionHighlightMode::default();
821+
let mut highlight = RegionHighlightMode::new(tcx);
822822
highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
823823
let type_name =
824824
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;

compiler/rustc_borrowck/src/nll.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::mir::{
88
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
99
Promoted,
1010
};
11-
use rustc_middle::ty::{self, OpaqueTypeKey, RegionKind, RegionVid, Ty};
11+
use rustc_middle::ty::{self, OpaqueTypeKey, Region, RegionVid, Ty};
1212
use rustc_span::symbol::sym;
1313
use std::env;
1414
use std::fmt::Debug;
@@ -443,9 +443,9 @@ pub trait ToRegionVid {
443443
fn to_region_vid(self) -> RegionVid;
444444
}
445445

446-
impl<'tcx> ToRegionVid for &'tcx RegionKind {
446+
impl<'tcx> ToRegionVid for Region<'tcx> {
447447
fn to_region_vid(self) -> RegionVid {
448-
if let ty::ReVar(vid) = self { *vid } else { bug!("region is not an ReVar: {:?}", self) }
448+
if let ty::ReVar(vid) = *self { vid } else { bug!("region is not an ReVar: {:?}", self) }
449449
}
450450
}
451451

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11781178
}
11791179

11801180
VerifyBound::OutlivedBy(r) => {
1181-
let r_vid = self.to_region_vid(r);
1181+
let r_vid = self.to_region_vid(*r);
11821182
self.eval_outlives(r_vid, lower_bound)
11831183
}
11841184

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
133133
for vid in self.rev_scc_graph.as_ref().unwrap().upper_bounds(scc) {
134134
match self.definitions[vid].external_name {
135135
None => {}
136-
Some(&ty::ReStatic) => {}
136+
Some(region) if region.is_static() => {}
137137
Some(region) => return region,
138138
}
139139
}
@@ -183,7 +183,7 @@ fn check_opaque_type_parameter_valid(
183183
for (i, arg) in opaque_type_key.substs.iter().enumerate() {
184184
let arg_is_param = match arg.unpack() {
185185
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
186-
GenericArgKind::Lifetime(ty::ReStatic) => {
186+
GenericArgKind::Lifetime(lt) if lt.is_static() => {
187187
tcx.sess
188188
.struct_span_err(span, "non-defining opaque type use in defining scope")
189189
.span_label(
@@ -196,7 +196,7 @@ fn check_opaque_type_parameter_valid(
196196
return false;
197197
}
198198
GenericArgKind::Lifetime(lt) => {
199-
matches!(lt, ty::ReEarlyBound(_) | ty::ReFree(_))
199+
matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_))
200200
}
201201
GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)),
202202
};

compiler/rustc_borrowck/src/renumber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
7272
#[instrument(skip(self), level = "debug")]
7373
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
7474
let old_region = *region;
75-
*region = self.renumber_regions(&old_region);
75+
*region = self.renumber_regions(old_region);
7676

7777
debug!(?region);
7878
}

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
142142
}
143143

144144
fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> ty::RegionVid {
145-
if let ty::RePlaceholder(placeholder) = r {
146-
self.constraints.placeholder_region(self.infcx, *placeholder).to_region_vid()
145+
if let ty::RePlaceholder(placeholder) = *r {
146+
self.constraints.placeholder_region(self.infcx, placeholder).to_region_vid()
147147
} else {
148148
self.universal_regions.to_region_vid(r)
149149
}

0 commit comments

Comments
 (0)