Skip to content

Commit c5d1b3e

Browse files
committed
Auto merge of #108020 - nnethercote:opt-mk_region, r=compiler-errors
Optimize `mk_region` PR #107869 avoiding some interning under `mk_ty` by special-casing `Ty` variants with simple (integer) bodies. This PR does something similar for regions. r? `@compiler-errors`
2 parents af3c8b2 + 9a53cee commit c5d1b3e

File tree

40 files changed

+254
-186
lines changed

40 files changed

+254
-186
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,20 @@ trait TypeOpInfo<'tcx> {
180180
return;
181181
};
182182

183-
let placeholder_region = tcx.mk_region(ty::RePlaceholder(ty::Placeholder {
183+
let placeholder_region = tcx.mk_re_placeholder(ty::Placeholder {
184184
name: placeholder.name,
185185
universe: adjusted_universe.into(),
186-
}));
186+
});
187187

188188
let error_region =
189189
if let RegionElement::PlaceholderRegion(error_placeholder) = error_element {
190190
let adjusted_universe =
191191
error_placeholder.universe.as_u32().checked_sub(base_universe.as_u32());
192192
adjusted_universe.map(|adjusted| {
193-
tcx.mk_region(ty::RePlaceholder(ty::Placeholder {
193+
tcx.mk_re_placeholder(ty::Placeholder {
194194
name: error_placeholder.name,
195195
universe: adjusted.into(),
196-
}))
196+
})
197197
})
198198
} else {
199199
None
@@ -390,7 +390,7 @@ fn try_extract_error_from_fulfill_cx<'tcx>(
390390
error_region,
391391
&region_constraints,
392392
|vid| ocx.infcx.region_var_origin(vid),
393-
|vid| ocx.infcx.universe_of_region(ocx.infcx.tcx.mk_region(ty::ReVar(vid))),
393+
|vid| ocx.infcx.universe_of_region(ocx.infcx.tcx.mk_re_var(vid)),
394394
)
395395
}
396396

@@ -411,7 +411,7 @@ fn try_extract_error_from_region_constraints<'tcx>(
411411
}
412412
// FIXME: Should this check the universe of the var?
413413
Constraint::VarSubReg(vid, sup) if sup == placeholder_region => {
414-
Some((infcx.tcx.mk_region(ty::ReVar(vid)), cause.clone()))
414+
Some((infcx.tcx.mk_re_var(vid), cause.clone()))
415415
}
416416
_ => None,
417417
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
12971297
let vid = self.to_region_vid(r);
12981298
let scc = self.constraint_sccs.scc(vid);
12991299
let repr = self.scc_representatives[scc];
1300-
tcx.mk_region(ty::ReVar(repr))
1300+
tcx.mk_re_var(repr)
13011301
})
13021302
}
13031303

@@ -1719,7 +1719,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17191719
}
17201720

17211721
// If not, report an error.
1722-
let member_region = infcx.tcx.mk_region(ty::ReVar(member_region_vid));
1722+
let member_region = infcx.tcx.mk_re_var(member_region_vid);
17231723
errors_buffer.push(RegionErrorKind::UnexpectedHiddenRegion {
17241724
span: m_c.definition_span,
17251725
hidden_ty: m_c.hidden_ty,

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9191
}
9292
None => {
9393
subst_regions.push(vid);
94-
infcx.tcx.re_error_with_message(
94+
infcx.tcx.mk_re_error_with_message(
9595
concrete_type.span,
9696
"opaque type with non-universal region substs",
9797
)

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
137137
upvars: &[Upvar<'tcx>],
138138
use_polonius: bool,
139139
) -> MirTypeckResults<'tcx> {
140-
let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
140+
let implicit_region_bound = infcx.tcx.mk_re_var(universal_regions.fr_fn_body);
141141
let mut constraints = MirTypeckRegionConstraints {
142142
placeholder_indices: PlaceholderIndices::default(),
143143
placeholder_index_to_region: IndexVec::default(),

compiler/rustc_borrowck/src/universal_regions.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
480480
LangItem::VaList,
481481
Some(self.infcx.tcx.def_span(self.mir_def.did)),
482482
);
483-
let region = self
484-
.infcx
485-
.tcx
486-
.mk_region(ty::ReVar(self.infcx.next_nll_region_var(FR).to_region_vid()));
483+
let region =
484+
self.infcx.tcx.mk_re_var(self.infcx.next_nll_region_var(FR).to_region_vid());
487485
let va_list_ty = self
488486
.infcx
489487
.tcx
@@ -636,7 +634,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
636634
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
637635
kind: ty::BrEnv,
638636
};
639-
let env_region = ty::ReLateBound(ty::INNERMOST, br);
637+
let env_region = tcx.mk_re_late_bound(ty::INNERMOST, br);
640638
let closure_ty = tcx.closure_env_ty(def_id, substs, env_region).unwrap();
641639

642640
// The "inputs" of the closure in the
@@ -748,10 +746,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
748746
{
749747
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
750748
debug!(?br);
751-
let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
752-
scope: all_outlive_scope.to_def_id(),
753-
bound_region: br.kind,
754-
}));
749+
let liberated_region = self.tcx.mk_re_free(all_outlive_scope.to_def_id(), br.kind);
755750
let region_vid = self.next_nll_region_var(origin);
756751
indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid());
757752
debug!(?liberated_region, ?region_vid);
@@ -843,7 +838,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
843838
where
844839
T: TypeFoldable<'tcx>,
845840
{
846-
tcx.fold_regions(value, |region, _| tcx.mk_region(ty::ReVar(self.to_region_vid(region))))
841+
tcx.fold_regions(value, |region, _| tcx.mk_re_var(self.to_region_vid(region)))
847842
}
848843
}
849844

@@ -883,8 +878,7 @@ fn for_each_late_bound_region_in_item<'tcx>(
883878

884879
for bound_var in tcx.late_bound_vars(tcx.hir().local_def_id_to_hir_id(mir_def_id)) {
885880
let ty::BoundVariableKind::Region(bound_region) = bound_var else { continue; };
886-
let liberated_region = tcx
887-
.mk_region(ty::ReFree(ty::FreeRegion { scope: mir_def_id.to_def_id(), bound_region }));
881+
let liberated_region = tcx.mk_re_free(mir_def_id.to_def_id(), bound_region);
888882
f(liberated_region);
889883
}
890884
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -234,23 +234,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
234234
var: ty::BoundVar::from_u32(index),
235235
kind: ty::BrNamed(def_id, name),
236236
};
237-
tcx.mk_region(ty::ReLateBound(debruijn, br))
237+
tcx.mk_re_late_bound(debruijn, br)
238238
}
239239

240240
Some(rl::Region::EarlyBound(def_id)) => {
241241
let name = tcx.hir().ty_param_name(def_id.expect_local());
242242
let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local());
243243
let generics = tcx.generics_of(item_def_id);
244244
let index = generics.param_def_id_to_index[&def_id];
245-
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }))
245+
tcx.mk_re_early_bound(ty::EarlyBoundRegion { def_id, index, name })
246246
}
247247

248248
Some(rl::Region::Free(scope, id)) => {
249249
let name = lifetime_name(id.expect_local());
250-
tcx.mk_region(ty::ReFree(ty::FreeRegion {
251-
scope,
252-
bound_region: ty::BrNamed(id, name),
253-
}))
250+
tcx.mk_re_free(scope, ty::BrNamed(id, name))
254251

255252
// (*) -- not late-bound, won't change
256253
}
@@ -263,7 +260,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
263260
// elision. `resolve_lifetime` should have
264261
// reported an error in this case -- but if
265262
// not, let's error out.
266-
tcx.re_error_with_message(lifetime.ident.span, "unelided lifetime in signature")
263+
tcx.mk_re_error_with_message(
264+
lifetime.ident.span,
265+
"unelided lifetime in signature",
266+
)
267267
})
268268
}
269269
}
@@ -477,7 +477,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
477477
debug!(?param, "unelided lifetime in signature");
478478

479479
// This indicates an illegal lifetime in a non-assoc-trait position
480-
tcx.re_error_with_message(self.span, "unelided lifetime in signature")
480+
tcx.mk_re_error_with_message(
481+
self.span,
482+
"unelided lifetime in signature",
483+
)
481484
})
482485
.into(),
483486
GenericParamDefKind::Type { has_default, .. } => {
@@ -1622,7 +1625,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16221625
} else {
16231626
err.emit()
16241627
};
1625-
tcx.re_error(e)
1628+
tcx.mk_re_error(e)
16261629
})
16271630
}
16281631
})

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,10 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateBound<'_, 'tcx> {
464464

465465
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
466466
if let ty::ReFree(fr) = *r {
467-
self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
468-
bound_region: self
469-
.mapping
470-
.get(&fr.bound_region)
471-
.copied()
472-
.unwrap_or(fr.bound_region),
473-
..fr
474-
}))
467+
self.tcx.mk_re_free(
468+
fr.scope,
469+
self.mapping.get(&fr.bound_region).copied().unwrap_or(fr.bound_region),
470+
)
475471
} else {
476472
r
477473
}
@@ -777,13 +773,13 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
777773
}
778774
let Some(ty::ReEarlyBound(e)) = map.get(&region.into()).map(|r| r.expect_region().kind())
779775
else {
780-
return tcx.re_error_with_message(return_span, "expected ReFree to map to ReEarlyBound")
776+
return tcx.mk_re_error_with_message(return_span, "expected ReFree to map to ReEarlyBound")
781777
};
782-
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
778+
tcx.mk_re_early_bound(ty::EarlyBoundRegion {
783779
def_id: e.def_id,
784780
name: e.name,
785781
index: (e.index as usize - num_trait_substs + num_impl_substs) as u32,
786-
}))
782+
})
787783
});
788784
debug!(%ty);
789785
collected_tys.insert(def_id, ty);
@@ -1920,10 +1916,10 @@ pub(super) fn check_type_bounds<'tcx>(
19201916
let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name);
19211917
let bound_var = ty::BoundVariableKind::Region(kind);
19221918
bound_vars.push(bound_var);
1923-
tcx.mk_region(ty::ReLateBound(
1919+
tcx.mk_re_late_bound(
19241920
ty::INNERMOST,
19251921
ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
1926-
))
1922+
)
19271923
.into()
19281924
}
19291925
GenericParamDefKind::Const { .. } => {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
149149
);
150150
let mk_va_list_ty = |mutbl| {
151151
tcx.lang_items().va_list().map(|did| {
152-
let region = tcx.mk_region(ty::ReLateBound(
152+
let region = tcx.mk_re_late_bound(
153153
ty::INNERMOST,
154154
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) },
155-
));
156-
let env_region = tcx.mk_region(ty::ReLateBound(
155+
);
156+
let env_region = tcx.mk_re_late_bound(
157157
ty::INNERMOST,
158158
ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrEnv },
159-
));
159+
);
160160
let va_list_ty = tcx.bound_type_of(did).subst(tcx, &[region.into()]);
161161
(tcx.mk_ref(env_region, ty::TypeAndMut { ty: va_list_ty, mutbl }), va_list_ty)
162162
})
@@ -377,9 +377,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
377377
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) };
378378
(
379379
1,
380-
vec![
381-
tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)), param(0)),
382-
],
380+
vec![tcx.mk_imm_ref(tcx.mk_re_late_bound(ty::INNERMOST, br), param(0))],
383381
tcx.mk_projection(discriminant_def_id, tcx.mk_substs([param(0).into()].iter())),
384382
)
385383
}
@@ -430,8 +428,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
430428
sym::raw_eq => {
431429
let br =
432430
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0, None) };
433-
let param_ty =
434-
tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)), param(0));
431+
let param_ty = tcx.mk_imm_ref(tcx.mk_re_late_bound(ty::INNERMOST, br), param(0));
435432
(1, vec![param_ty; 2], tcx.types.bool)
436433
}
437434

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,11 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
607607
// Same for the region. In our example, 'a corresponds
608608
// to the 'me parameter.
609609
let region_param = gat_generics.param_at(*region_a_idx, tcx);
610-
let region_param =
611-
tcx.mk_region(ty::RegionKind::ReEarlyBound(ty::EarlyBoundRegion {
612-
def_id: region_param.def_id,
613-
index: region_param.index,
614-
name: region_param.name,
615-
}));
610+
let region_param = tcx.mk_re_early_bound(ty::EarlyBoundRegion {
611+
def_id: region_param.def_id,
612+
index: region_param.index,
613+
name: region_param.name,
614+
});
616615
// The predicate we expect to see. (In our example,
617616
// `Self: 'me`.)
618617
let clause = ty::PredicateKind::Clause(ty::Clause::TypeOutlives(
@@ -645,20 +644,18 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
645644
debug!("required clause: {region_a} must outlive {region_b}");
646645
// Translate into the generic parameters of the GAT.
647646
let region_a_param = gat_generics.param_at(*region_a_idx, tcx);
648-
let region_a_param =
649-
tcx.mk_region(ty::RegionKind::ReEarlyBound(ty::EarlyBoundRegion {
650-
def_id: region_a_param.def_id,
651-
index: region_a_param.index,
652-
name: region_a_param.name,
653-
}));
647+
let region_a_param = tcx.mk_re_early_bound(ty::EarlyBoundRegion {
648+
def_id: region_a_param.def_id,
649+
index: region_a_param.index,
650+
name: region_a_param.name,
651+
});
654652
// Same for the region.
655653
let region_b_param = gat_generics.param_at(*region_b_idx, tcx);
656-
let region_b_param =
657-
tcx.mk_region(ty::RegionKind::ReEarlyBound(ty::EarlyBoundRegion {
658-
def_id: region_b_param.def_id,
659-
index: region_b_param.index,
660-
name: region_b_param.name,
661-
}));
654+
let region_b_param = tcx.mk_re_early_bound(ty::EarlyBoundRegion {
655+
def_id: region_b_param.def_id,
656+
index: region_b_param.index,
657+
name: region_b_param.name,
658+
});
662659
// The predicate we expect to see.
663660
let clause = ty::PredicateKind::Clause(ty::Clause::RegionOutlives(
664661
ty::OutlivesPredicate(region_a_param, region_b_param),

compiler/rustc_hir_analysis/src/collect.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,11 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
458458
self.tcx.replace_late_bound_regions_uncached(
459459
poly_trait_ref,
460460
|_| {
461-
self.tcx.mk_region(ty::ReEarlyBound(
462-
ty::EarlyBoundRegion {
463-
def_id: item_def_id,
464-
index: 0,
465-
name: Symbol::intern(&lt_name),
466-
},
467-
))
461+
self.tcx.mk_re_early_bound(ty::EarlyBoundRegion {
462+
def_id: item_def_id,
463+
index: 0,
464+
name: Symbol::intern(&lt_name),
465+
})
468466
}
469467
),
470468
),

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
284284

285285
let Some(dup_index) = generics.param_def_id_to_index(tcx, dup_def) else { bug!() };
286286

287-
let dup_region = tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
287+
let dup_region = tcx.mk_re_early_bound(ty::EarlyBoundRegion {
288288
def_id: dup_def,
289289
index: dup_index,
290290
name: duplicate.name.ident().name,
291-
}));
291+
});
292292
predicates.push((
293293
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::RegionOutlives(
294294
ty::OutlivesPredicate(orig_region, dup_region),

compiler/rustc_hir_typeck/src/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12691269
// ```
12701270
let ref_ty = match mutability {
12711271
hir::Mutability::Mut => {
1272-
self.tcx.mk_mut_ref(self.tcx.mk_region(ty::ReStatic), checked_ty)
1272+
self.tcx.mk_mut_ref(self.tcx.lifetimes.re_static, checked_ty)
12731273
}
12741274
hir::Mutability::Not => {
1275-
self.tcx.mk_imm_ref(self.tcx.mk_region(ty::ReStatic), checked_ty)
1275+
self.tcx.mk_imm_ref(self.tcx.lifetimes.re_static, checked_ty)
12761276
}
12771277
};
12781278
if self.can_coerce(ref_ty, expected) {

0 commit comments

Comments
 (0)