Skip to content

Commit 5569757

Browse files
committed
Auto merge of #93148 - nnethercote:Uniq, r=fee1-dead
Overhaul interning. A number of types are interned and `eq` and `hash` are implemented on the pointer rather than the contents. But this is not well enforced within the type system like you might expect. This PR introduces a new type `Interned` which encapsulates this concept more rigorously, and uses it to convert a couple of the less common interned types. r? `@fee1-dead`
2 parents 6421a49 + 80632de commit 5569757

File tree

252 files changed

+1913
-1685
lines changed

Some content is hidden

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

252 files changed

+1913
-1685
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/conflict_errors.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
23242324
// This is also case 2 from above but for functions, return type is still an
23252325
// anonymous reference so we select the first argument.
23262326
let argument_span = fn_decl.inputs.first()?.span;
2327-
let argument_ty = sig.inputs().skip_binder().first()?;
2327+
let argument_ty = *sig.inputs().skip_binder().first()?;
23282328

23292329
let return_span = fn_decl.output.span();
23302330
let return_ty = sig.output().skip_binder();
@@ -2379,27 +2379,27 @@ impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
23792379
diag: &mut DiagnosticBuilder<'_>,
23802380
) -> String {
23812381
match self {
2382-
AnnotatedBorrowFnSignature::Closure { argument_ty, argument_span } => {
2382+
&AnnotatedBorrowFnSignature::Closure { argument_ty, argument_span } => {
23832383
diag.span_label(
2384-
*argument_span,
2384+
argument_span,
23852385
format!("has type `{}`", cx.get_name_for_ty(argument_ty, 0)),
23862386
);
23872387

23882388
cx.get_region_name_for_ty(argument_ty, 0)
23892389
}
2390-
AnnotatedBorrowFnSignature::AnonymousFunction {
2390+
&AnnotatedBorrowFnSignature::AnonymousFunction {
23912391
argument_ty,
23922392
argument_span,
23932393
return_ty,
23942394
return_span,
23952395
} => {
23962396
let argument_ty_name = cx.get_name_for_ty(argument_ty, 0);
2397-
diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
2397+
diag.span_label(argument_span, format!("has type `{}`", argument_ty_name));
23982398

23992399
let return_ty_name = cx.get_name_for_ty(return_ty, 0);
24002400
let types_equal = return_ty_name == argument_ty_name;
24012401
diag.span_label(
2402-
*return_span,
2402+
return_span,
24032403
format!(
24042404
"{}has type `{}`",
24052405
if types_equal { "also " } else { "" },
@@ -2419,7 +2419,7 @@ impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
24192419
}
24202420
AnnotatedBorrowFnSignature::NamedFunction { arguments, return_ty, return_span } => {
24212421
// Region of return type and arguments checked to be the same earlier.
2422-
let region_name = cx.get_region_name_for_ty(return_ty, 0);
2422+
let region_name = cx.get_region_name_for_ty(*return_ty, 0);
24232423
for (_, argument_span) in arguments {
24242424
diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
24252425
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
331331
match place {
332332
PlaceRef { local, projection: [] } => {
333333
let local = &self.body.local_decls[local];
334-
self.describe_field_from_ty(&local.ty, field, None)
334+
self.describe_field_from_ty(local.ty, field, None)
335335
}
336336
PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
337337
ProjectionElem::Deref => {
338338
self.describe_field(PlaceRef { local, projection: proj_base }, field)
339339
}
340340
ProjectionElem::Downcast(_, variant_index) => {
341341
let base_ty = place.ty(self.body, self.infcx.tcx).ty;
342-
self.describe_field_from_ty(&base_ty, field, Some(*variant_index))
342+
self.describe_field_from_ty(base_ty, field, Some(*variant_index))
343343
}
344344
ProjectionElem::Field(_, field_type) => {
345-
self.describe_field_from_ty(&field_type, field, None)
345+
self.describe_field_from_ty(*field_type, field, None)
346346
}
347347
ProjectionElem::Index(..)
348348
| ProjectionElem::ConstantIndex { .. }
@@ -362,7 +362,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
362362
) -> String {
363363
if ty.is_box() {
364364
// If the type is a box, the field is described from the boxed type
365-
self.describe_field_from_ty(&ty.boxed_ty(), field, variant_index)
365+
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index)
366366
} else {
367367
match *ty.kind() {
368368
ty::Adt(def, _) => {
@@ -376,10 +376,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
376376
}
377377
ty::Tuple(_) => field.index().to_string(),
378378
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
379-
self.describe_field_from_ty(&ty, field, variant_index)
379+
self.describe_field_from_ty(ty, field, variant_index)
380380
}
381381
ty::Array(ty, _) | ty::Slice(ty) => {
382-
self.describe_field_from_ty(&ty, field, variant_index)
382+
self.describe_field_from_ty(ty, field, variant_index)
383383
}
384384
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
385385
// We won't be borrowck'ing here if the closure came from another crate,
@@ -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/move_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
246246
);
247247
(
248248
match kind {
249-
IllegalMoveOriginKind::BorrowedContent { target_place } => self
249+
&IllegalMoveOriginKind::BorrowedContent { target_place } => self
250250
.report_cannot_move_from_borrowed_content(
251251
original_path,
252-
*target_place,
252+
target_place,
253253
span,
254254
use_spans,
255255
),
256-
IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
256+
&IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
257257
self.cannot_move_out_of_interior_of_drop(span, ty)
258258
}
259-
IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => {
260-
self.cannot_move_out_of_interior_noncopy(span, ty, Some(*is_index))
259+
&IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => {
260+
self.cannot_move_out_of_interior_noncopy(span, ty, Some(is_index))
261261
}
262262
},
263263
span,

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

+5-5
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;
@@ -500,7 +500,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
500500
}
501501

502502
// Otherwise, let's descend into the referent types.
503-
search_stack.push((referent_ty, &referent_hir_ty.ty));
503+
search_stack.push((*referent_ty, &referent_hir_ty.ty));
504504
}
505505

506506
// Match up something like `Foo<'1>`
@@ -539,7 +539,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
539539

540540
(ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
541541
| (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
542-
search_stack.push((elem_ty, elem_hir_ty));
542+
search_stack.push((*elem_ty, elem_hir_ty));
543543
}
544544

545545
(ty::RawPtr(mut_ty), hir::TyKind::Ptr(mut_hir_ty)) => {
@@ -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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11691169

11701170
match verify_bound {
11711171
VerifyBound::IfEq(test_ty, verify_bound1) => {
1172-
self.eval_if_eq(tcx, body, generic_ty, lower_bound, test_ty, verify_bound1)
1172+
self.eval_if_eq(tcx, body, generic_ty, lower_bound, *test_ty, verify_bound1)
11731173
}
11741174

11751175
VerifyBound::IsEmpty => {
@@ -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

+4-4
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,9 +196,9 @@ 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
}
201-
GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)),
201+
GenericArgKind::Const(ct) => matches!(ct.val(), ty::ConstKind::Param(_)),
202202
};
203203

204204
if arg_is_param {

compiler/rustc_borrowck/src/renumber.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
5757

5858
#[instrument(skip(self), level = "debug")]
5959
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
60-
*ty = self.renumber_regions(ty);
60+
*ty = self.renumber_regions(*ty);
6161

6262
debug!(?ty);
6363
}
@@ -72,12 +72,12 @@ 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
}
7979

80-
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
81-
*constant = self.renumber_regions(&*constant);
80+
fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
81+
*constant = self.renumber_regions(*constant);
8282
}
8383
}

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
105105
// create new region variables, which can't be done later when
106106
// verifying these bounds.
107107
if t1.has_placeholders() {
108-
t1 = tcx.fold_regions(&t1, &mut false, |r, _| match *r {
109-
ty::RegionKind::RePlaceholder(placeholder) => {
108+
t1 = tcx.fold_regions(t1, &mut false, |r, _| match *r {
109+
ty::RePlaceholder(placeholder) => {
110110
self.constraints.placeholder_region(self.infcx, placeholder)
111111
}
112112
_ => r,
@@ -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)