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

+2-2
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

+3-3
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

+17-19
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

+4-4
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

+3-3
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

+3-3
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

+1-1
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

+3-3
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

+1-1
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

+2-2
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
}

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
358358
// `where Type:` is lowered to `where Type: 'empty` so that
359359
// we check `Type` is well formed, but there's no use for
360360
// this bound here.
361-
if let ty::ReEmpty(_) = r1 {
361+
if r1.is_empty() {
362362
return;
363363
}
364364

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2318,7 +2318,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
23182318
}
23192319

23202320
Rvalue::Ref(region, _borrow_kind, borrowed_place) => {
2321-
self.add_reborrow_constraint(&body, location, region, borrowed_place);
2321+
self.add_reborrow_constraint(&body, location, *region, borrowed_place);
23222322
}
23232323

23242324
Rvalue::BinaryOp(

compiler/rustc_borrowck/src/universal_regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'tcx> UniversalRegions<'tcx> {
323323

324324
/// See `UniversalRegionIndices::to_region_vid`.
325325
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
326-
if let ty::ReEmpty(ty::UniverseIndex::ROOT) = r {
326+
if let ty::ReEmpty(ty::UniverseIndex::ROOT) = *r {
327327
self.root_empty
328328
} else {
329329
self.indices.to_region_vid(r)
@@ -805,7 +805,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
805805
/// during initialization. Relies on the `indices` map having been
806806
/// fully initialized.
807807
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
808-
if let ty::ReVar(..) = r {
808+
if let ty::ReVar(..) = *r {
809809
r.to_region_vid()
810810
} else {
811811
*self

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ pub(crate) fn codegen_drop<'tcx>(
544544
let arg_value = drop_place.place_ref(
545545
fx,
546546
fx.layout_of(fx.tcx.mk_ref(
547-
&ty::RegionKind::ReErased,
547+
fx.tcx.lifetimes.re_erased,
548548
TypeAndMut { ty, mutbl: crate::rustc_hir::Mutability::Mut },
549549
)),
550550
);

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,20 @@ impl CanonicalizeMode for CanonicalizeQueryResponse {
179179
canonicalizer: &mut Canonicalizer<'_, 'tcx>,
180180
r: ty::Region<'tcx>,
181181
) -> ty::Region<'tcx> {
182-
match r {
182+
match *r {
183183
ty::ReFree(_)
184184
| ty::ReErased
185185
| ty::ReStatic
186186
| ty::ReEmpty(ty::UniverseIndex::ROOT)
187187
| ty::ReEarlyBound(..) => r,
188188

189189
ty::RePlaceholder(placeholder) => canonicalizer.canonical_var_for_region(
190-
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderRegion(*placeholder) },
190+
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderRegion(placeholder) },
191191
r,
192192
),
193193

194194
ty::ReVar(vid) => {
195-
let universe = canonicalizer.region_var_universe(*vid);
195+
let universe = canonicalizer.region_var_universe(vid);
196196
canonicalizer.canonical_var_for_region(
197197
CanonicalVarInfo { kind: CanonicalVarKind::Region(universe) },
198198
r,
@@ -240,7 +240,7 @@ impl CanonicalizeMode for CanonicalizeUserTypeAnnotation {
240240
canonicalizer: &mut Canonicalizer<'_, 'tcx>,
241241
r: ty::Region<'tcx>,
242242
) -> ty::Region<'tcx> {
243-
match r {
243+
match *r {
244244
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReErased | ty::ReStatic => r,
245245
ty::ReVar(_) => canonicalizer.canonical_var_for_region_in_root_universe(r),
246246
_ => {
@@ -311,11 +311,7 @@ impl CanonicalizeMode for CanonicalizeFreeRegionsOtherThanStatic {
311311
canonicalizer: &mut Canonicalizer<'_, 'tcx>,
312312
r: ty::Region<'tcx>,
313313
) -> ty::Region<'tcx> {
314-
if let ty::ReStatic = r {
315-
r
316-
} else {
317-
canonicalizer.canonical_var_for_region_in_root_universe(r)
318-
}
314+
if r.is_static() { r } else { canonicalizer.canonical_var_for_region_in_root_universe(r) }
319315
}
320316

321317
fn any(&self) -> bool {

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
237237
v.var_values[BoundVar::new(index)]
238238
});
239239
match (original_value.unpack(), result_value.unpack()) {
240-
(
241-
GenericArgKind::Lifetime(ty::ReErased),
242-
GenericArgKind::Lifetime(ty::ReErased),
243-
) => {
240+
(GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
241+
if re1.is_erased() && re2.is_erased() =>
242+
{
244243
// No action needed.
245244
}
246245

@@ -429,7 +428,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
429428
}
430429
GenericArgKind::Lifetime(result_value) => {
431430
// e.g., here `result_value` might be `'?1` in the example above...
432-
if let &ty::RegionKind::ReLateBound(debruijn, br) = result_value {
431+
if let ty::RegionKind::ReLateBound(debruijn, br) = *result_value {
433432
// ... in which case we would set `canonical_vars[0]` to `Some('static)`.
434433

435434
// We only allow a `ty::INNERMOST` index in substitutions.
@@ -558,10 +557,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
558557
obligations
559558
.extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
560559
}
561-
(
562-
GenericArgKind::Lifetime(ty::ReErased),
563-
GenericArgKind::Lifetime(ty::ReErased),
564-
) => {
560+
(GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
561+
if re1.is_erased() && re2.is_erased() =>
562+
{
565563
// no action needed
566564
}
567565
(GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => {

compiler/rustc_infer/src/infer/combine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
915915
debug_assert_eq!(r, _r);
916916
debug!("ConstInferUnifier: r={:?}", r);
917917

918-
match r {
918+
match *r {
919919
// Never make variables for regions bound within the type itself,
920920
// nor for erased regions.
921921
ty::ReLateBound(..) | ty::ReErased => {

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
239239
);
240240

241241
// Explain the region we are capturing.
242-
match hidden_region {
242+
match *hidden_region {
243243
ty::ReEmpty(ty::UniverseIndex::ROOT) => {
244244
// All lifetimes shorter than the function body are `empty` in
245245
// lexical region resolution. The default explanation of "an empty
@@ -1114,7 +1114,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11141114
}
11151115

11161116
fn push_ty_ref<'tcx>(
1117-
region: &ty::Region<'tcx>,
1117+
region: ty::Region<'tcx>,
11181118
ty: Ty<'tcx>,
11191119
mutbl: hir::Mutability,
11201120
s: &mut DiagnosticStyledString,
@@ -1335,14 +1335,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13351335
// When finding T != &T, highlight only the borrow
13361336
(&ty::Ref(r1, ref_ty1, mutbl1), _) if equals(ref_ty1, t2) => {
13371337
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
1338-
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
1338+
push_ty_ref(r1, ref_ty1, mutbl1, &mut values.0);
13391339
values.1.push_normal(t2.to_string());
13401340
values
13411341
}
13421342
(_, &ty::Ref(r2, ref_ty2, mutbl2)) if equals(t1, ref_ty2) => {
13431343
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
13441344
values.0.push_normal(t1.to_string());
1345-
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
1345+
push_ty_ref(r2, ref_ty2, mutbl2, &mut values.1);
13461346
values
13471347
}
13481348

@@ -1351,8 +1351,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13511351
if equals(ref_ty1, ref_ty2) =>
13521352
{
13531353
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
1354-
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
1355-
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
1354+
push_ty_ref(r1, ref_ty1, mutbl1, &mut values.0);
1355+
push_ty_ref(r2, ref_ty2, mutbl2, &mut values.1);
13561356
values
13571357
}
13581358

0 commit comments

Comments
 (0)