Skip to content

Commit b1964e6

Browse files
committed
Auto merge of #80163 - jackh726:binder-refactor-part-3, r=lcnr
Make BoundRegion have a kind of BoungRegionKind Split from #76814 Also includes making `replace_escaping_bound_vars` only return `T` Going to r? `@lcnr` Feel free to reassign
2 parents 29e3212 + 328fcee commit b1964e6

Some content is hidden

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

47 files changed

+183
-181
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx
6464
ty::Generator(_, substs, _) => {
6565
let sig = substs.as_generator().poly_sig();
6666

67-
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
67+
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { kind: ty::BrEnv });
6868
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
6969

7070
let pin_did = tcx.require_lang_item(rustc_hir::LangItem::Pin, None);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
625625
r: ty::Region<'tcx>,
626626
) -> ty::Region<'tcx> {
627627
let var = self.canonical_var(info, r.into());
628-
let region = ty::ReLateBound(self.binder_index, ty::BoundRegion::BrAnon(var.as_u32()));
628+
let br = ty::BoundRegion { kind: ty::BrAnon(var.as_u32()) };
629+
let region = ty::ReLateBound(self.binder_index, br);
629630
self.tcx().mk_region(region)
630631
}
631632

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ where
8787
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
8888
};
8989

90-
tcx.replace_escaping_bound_vars(value, fld_r, fld_t, fld_c).0
90+
tcx.replace_escaping_bound_vars(value, fld_r, fld_t, fld_c)
9191
}
9292
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ fn msg_span_from_early_bound_and_free_regions(
165165
}
166166
(format!("the lifetime `{}` as defined on", br.name), sp)
167167
}
168-
ty::ReFree(ty::FreeRegion { bound_region: ty::BoundRegion::BrNamed(_, name), .. }) => {
168+
ty::ReFree(ty::FreeRegion {
169+
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
170+
}) => {
169171
let mut sp = sm.guess_head_span(tcx.hir().span(node));
170172
if let Some(param) =
171173
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
@@ -2279,7 +2281,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22792281
&self,
22802282
var_origin: RegionVariableOrigin,
22812283
) -> DiagnosticBuilder<'tcx> {
2282-
let br_string = |br: ty::BoundRegion| {
2284+
let br_string = |br: ty::BoundRegionKind| {
22832285
let mut s = match br {
22842286
ty::BrNamed(_, name) => name.to_string(),
22852287
_ => String::new(),

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2525
pub(super) fn find_anon_type(
2626
&self,
2727
region: Region<'tcx>,
28-
br: &ty::BoundRegion,
28+
br: &ty::BoundRegionKind,
2929
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> {
3030
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
3131
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id);
@@ -56,7 +56,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5656
fn find_component_for_bound_region(
5757
&self,
5858
arg: &'tcx hir::Ty<'tcx>,
59-
br: &ty::BoundRegion,
59+
br: &ty::BoundRegionKind,
6060
) -> Option<&'tcx hir::Ty<'tcx>> {
6161
let mut nested_visitor = FindNestedTypeVisitor {
6262
tcx: self.tcx(),
@@ -80,7 +80,7 @@ struct FindNestedTypeVisitor<'tcx> {
8080
tcx: TyCtxt<'tcx>,
8181
// The bound_region corresponding to the Refree(freeregion)
8282
// associated with the anonymous region we are looking for.
83-
bound_region: ty::BoundRegion,
83+
bound_region: ty::BoundRegionKind,
8484
// The type where the anonymous lifetime appears
8585
// for e.g., Vec<`&u8`> and <`&u8`>
8686
found_type: Option<&'tcx hir::Ty<'tcx>>,
@@ -207,7 +207,7 @@ impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
207207
struct TyPathVisitor<'tcx> {
208208
tcx: TyCtxt<'tcx>,
209209
found_it: bool,
210-
bound_region: ty::BoundRegion,
210+
bound_region: ty::BoundRegionKind,
211211
current_index: ty::DebruijnIndex,
212212
}
213213

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub(super) struct AnonymousParamInfo<'tcx> {
1414
pub param: &'tcx hir::Param<'tcx>,
1515
/// The type corresponding to the anonymous region parameter.
1616
pub param_ty: Ty<'tcx>,
17-
/// The ty::BoundRegion corresponding to the anonymous region.
18-
pub bound_region: ty::BoundRegion,
17+
/// The ty::BoundRegionKind corresponding to the anonymous region.
18+
pub bound_region: ty::BoundRegionKind,
1919
/// The `Span` of the parameter type.
2020
pub param_ty_span: Span,
2121
/// Signals that the argument is the first parameter in the declaration.
@@ -43,7 +43,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4343
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
4444
ty::ReEarlyBound(ebr) => (
4545
self.tcx().parent(ebr.def_id).unwrap(),
46-
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
46+
ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
4747
),
4848
_ => return None, // not a free region
4949
};
@@ -145,7 +145,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
145145
pub(super) fn is_return_type_anon(
146146
&self,
147147
scope_def_id: LocalDefId,
148-
br: ty::BoundRegion,
148+
br: ty::BoundRegionKind,
149149
decl: &hir::FnDecl<'_>,
150150
) -> Option<Span> {
151151
let ret_ty = self.tcx().type_of(scope_def_id);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7777
// (i.e., if there are no placeholders).
7878
let next_universe = self.universe().next_universe();
7979

80-
let fld_r = |br| {
80+
let fld_r = |br: ty::BoundRegion| {
8181
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
8282
universe: next_universe,
83-
name: br,
83+
name: br.kind,
8484
}))
8585
};
8686

compiler/rustc_infer/src/infer/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ pub enum RegionVariableOrigin {
450450

451451
/// Region variables created for bound regions
452452
/// in a function or method that is called
453-
LateBoundRegion(Span, ty::BoundRegion, LateBoundRegionConversionTime),
453+
LateBoundRegion(Span, ty::BoundRegionKind, LateBoundRegionConversionTime),
454454

455455
UpvarRegion(ty::UpvarId, Span),
456456

@@ -1421,7 +1421,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14211421
where
14221422
T: TypeFoldable<'tcx>,
14231423
{
1424-
let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
1424+
let fld_r =
1425+
|br: ty::BoundRegion| self.next_region_var(LateBoundRegion(span, br.kind, lbrct));
14251426
let fld_t = |_| {
14261427
self.next_ty_var(TypeVariableOrigin {
14271428
kind: TypeVariableOriginKind::MiscVariable,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ where
176176
universe
177177
});
178178

179-
let placeholder = ty::PlaceholderRegion { universe, name: br };
179+
let placeholder = ty::PlaceholderRegion { universe, name: br.kind };
180180
delegate.next_placeholder_region(placeholder)
181181
} else {
182182
delegate.next_existential_region_var(true)

compiler/rustc_middle/src/ich/impls_ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
7070
ty::ReEmpty(universe) => {
7171
universe.hash_stable(hcx, hasher);
7272
}
73-
ty::ReLateBound(db, ty::BrAnon(i)) => {
73+
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrAnon(i) }) => {
7474
db.hash_stable(hcx, hasher);
7575
i.hash_stable(hcx, hasher);
7676
}
77-
ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
77+
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrNamed(def_id, name) }) => {
7878
db.hash_stable(hcx, hasher);
7979
def_id.hash_stable(hcx, hasher);
8080
name.hash_stable(hcx, hasher);
8181
}
82-
ty::ReLateBound(db, ty::BrEnv) => {
82+
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrEnv }) => {
8383
db.hash_stable(hcx, hasher);
8484
}
8585
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {

compiler/rustc_middle/src/infer/canonical.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@ impl<'tcx> CanonicalVarValues<'tcx> {
323323
GenericArgKind::Type(..) => {
324324
tcx.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i).into())).into()
325325
}
326-
GenericArgKind::Lifetime(..) => tcx
327-
.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion::BrAnon(i)))
328-
.into(),
326+
GenericArgKind::Lifetime(..) => {
327+
let br = ty::BoundRegion { kind: ty::BrAnon(i) };
328+
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
329+
}
329330
GenericArgKind::Const(ct) => tcx
330331
.mk_const(ty::Const {
331332
ty: ct.ty,

compiler/rustc_middle/src/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ pub struct FreeRegionInfo {
890890
// `LocalDefId` corresponding to FreeRegion
891891
pub def_id: LocalDefId,
892892
// the bound region corresponding to FreeRegion
893-
pub boundregion: ty::BoundRegion,
893+
pub boundregion: ty::BoundRegionKind,
894894
// checks if bound region is in Impl Item
895895
pub is_impl_item: bool,
896896
}
@@ -1412,15 +1412,15 @@ impl<'tcx> TyCtxt<'tcx> {
14121412
})
14131413
}
14141414

1415-
// Returns the `DefId` and the `BoundRegion` corresponding to the given region.
1415+
// Returns the `DefId` and the `BoundRegionKind` corresponding to the given region.
14161416
pub fn is_suitable_region(self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
14171417
let (suitable_region_binding_scope, bound_region) = match *region {
14181418
ty::ReFree(ref free_region) => {
14191419
(free_region.scope.expect_local(), free_region.bound_region)
14201420
}
14211421
ty::ReEarlyBound(ref ebr) => (
14221422
self.parent(ebr.def_id).unwrap().expect_local(),
1423-
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
1423+
ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
14241424
),
14251425
_ => return None, // not a free region
14261426
};

compiler/rustc_middle/src/ty/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::traits::{ObligationCause, ObligationCauseCode};
22
use crate::ty::diagnostics::suggest_constraining_type_param;
3-
use crate::ty::{self, BoundRegion, Region, Ty, TyCtxt};
3+
use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt};
44
use rustc_ast as ast;
55
use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
66
use rustc_errors::{pluralize, DiagnosticBuilder};
@@ -42,8 +42,8 @@ pub enum TypeError<'tcx> {
4242
ArgCount,
4343

4444
RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),
45-
RegionsInsufficientlyPolymorphic(BoundRegion, Region<'tcx>),
46-
RegionsOverlyPolymorphic(BoundRegion, Region<'tcx>),
45+
RegionsInsufficientlyPolymorphic(BoundRegionKind, Region<'tcx>),
46+
RegionsOverlyPolymorphic(BoundRegionKind, Region<'tcx>),
4747
RegionsPlaceholderMismatch,
4848

4949
Sorts(ExpectedFound<Ty<'tcx>>),
@@ -94,7 +94,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
9494
}
9595
}
9696

97-
let br_string = |br: ty::BoundRegion| match br {
97+
let br_string = |br: ty::BoundRegionKind| match br {
9898
ty::BrNamed(_, name) => format!(" {}", name),
9999
_ => String::new(),
100100
};

compiler/rustc_middle/src/ty/fold.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ impl<'tcx> TyCtxt<'tcx> {
534534
/// results returned by the closure; the closure is expected to
535535
/// return a free region (relative to this binder), and hence the
536536
/// binder is removed in the return type. The closure is invoked
537-
/// once for each unique `BoundRegion`; multiple references to the
538-
/// same `BoundRegion` will reuse the previous result. A map is
537+
/// once for each unique `BoundRegionKind`; multiple references to the
538+
/// same `BoundRegionKind` will reuse the previous result. A map is
539539
/// returned at the end with each bound region and the free region
540540
/// that replaced it.
541541
///
@@ -544,7 +544,7 @@ impl<'tcx> TyCtxt<'tcx> {
544544
pub fn replace_late_bound_regions<T, F>(
545545
self,
546546
value: Binder<T>,
547-
fld_r: F,
547+
mut fld_r: F,
548548
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
549549
where
550550
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
@@ -555,7 +555,10 @@ impl<'tcx> TyCtxt<'tcx> {
555555
let fld_c = |bound_ct, ty| {
556556
self.mk_const(ty::Const { val: ty::ConstKind::Bound(ty::INNERMOST, bound_ct), ty })
557557
};
558-
self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t, fld_c)
558+
let mut region_map = BTreeMap::new();
559+
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
560+
let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
561+
(value, region_map)
559562
}
560563

561564
/// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
@@ -567,34 +570,18 @@ impl<'tcx> TyCtxt<'tcx> {
567570
mut fld_r: F,
568571
mut fld_t: G,
569572
mut fld_c: H,
570-
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
573+
) -> T
571574
where
572575
F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
573576
G: FnMut(ty::BoundTy) -> Ty<'tcx>,
574577
H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx>,
575578
T: TypeFoldable<'tcx>,
576579
{
577-
use rustc_data_structures::fx::FxHashMap;
578-
579-
let mut region_map = BTreeMap::new();
580-
let mut type_map = FxHashMap::default();
581-
let mut const_map = FxHashMap::default();
582-
583580
if !value.has_escaping_bound_vars() {
584-
(value, region_map)
581+
value
585582
} else {
586-
let mut real_fld_r = |br| *region_map.entry(br).or_insert_with(|| fld_r(br));
587-
588-
let mut real_fld_t =
589-
|bound_ty| *type_map.entry(bound_ty).or_insert_with(|| fld_t(bound_ty));
590-
591-
let mut real_fld_c =
592-
|bound_ct, ty| *const_map.entry(bound_ct).or_insert_with(|| fld_c(bound_ct, ty));
593-
594-
let mut replacer =
595-
BoundVarReplacer::new(self, &mut real_fld_r, &mut real_fld_t, &mut real_fld_c);
596-
let result = value.fold_with(&mut replacer);
597-
(result, region_map)
583+
let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t, &mut fld_c);
584+
value.fold_with(&mut replacer)
598585
}
599586
}
600587

@@ -604,7 +591,7 @@ impl<'tcx> TyCtxt<'tcx> {
604591
pub fn replace_bound_vars<T, F, G, H>(
605592
self,
606593
value: Binder<T>,
607-
fld_r: F,
594+
mut fld_r: F,
608595
fld_t: G,
609596
fld_c: H,
610597
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
@@ -614,7 +601,10 @@ impl<'tcx> TyCtxt<'tcx> {
614601
H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::Const<'tcx>,
615602
T: TypeFoldable<'tcx>,
616603
{
617-
self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t, fld_c)
604+
let mut region_map = BTreeMap::new();
605+
let real_fld_r = |br: ty::BoundRegion| *region_map.entry(br).or_insert_with(|| fld_r(br));
606+
let value = self.replace_escaping_bound_vars(value.skip_binder(), real_fld_r, fld_t, fld_c);
607+
(value, region_map)
618608
}
619609

620610
/// Replaces any late-bound regions bound in `value` with
@@ -626,7 +616,7 @@ impl<'tcx> TyCtxt<'tcx> {
626616
self.replace_late_bound_regions(value, |br| {
627617
self.mk_region(ty::ReFree(ty::FreeRegion {
628618
scope: all_outlive_scope,
629-
bound_region: br,
619+
bound_region: br.kind,
630620
}))
631621
})
632622
.0
@@ -639,7 +629,7 @@ impl<'tcx> TyCtxt<'tcx> {
639629
pub fn collect_constrained_late_bound_regions<T>(
640630
self,
641631
value: &Binder<T>,
642-
) -> FxHashSet<ty::BoundRegion>
632+
) -> FxHashSet<ty::BoundRegionKind>
643633
where
644634
T: TypeFoldable<'tcx>,
645635
{
@@ -650,7 +640,7 @@ impl<'tcx> TyCtxt<'tcx> {
650640
pub fn collect_referenced_late_bound_regions<T>(
651641
self,
652642
value: &Binder<T>,
653-
) -> FxHashSet<ty::BoundRegion>
643+
) -> FxHashSet<ty::BoundRegionKind>
654644
where
655645
T: TypeFoldable<'tcx>,
656646
{
@@ -661,7 +651,7 @@ impl<'tcx> TyCtxt<'tcx> {
661651
self,
662652
value: &Binder<T>,
663653
just_constraint: bool,
664-
) -> FxHashSet<ty::BoundRegion>
654+
) -> FxHashSet<ty::BoundRegionKind>
665655
where
666656
T: TypeFoldable<'tcx>,
667657
{
@@ -695,7 +685,8 @@ impl<'tcx> TyCtxt<'tcx> {
695685
let mut counter = 0;
696686
Binder::bind(
697687
self.replace_late_bound_regions(sig, |_| {
698-
let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter)));
688+
let br = ty::BoundRegion { kind: ty::BrAnon(counter) };
689+
let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, br));
699690
counter += 1;
700691
r
701692
})
@@ -955,7 +946,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
955946
/// into a hash set.
956947
struct LateBoundRegionsCollector {
957948
current_index: ty::DebruijnIndex,
958-
regions: FxHashSet<ty::BoundRegion>,
949+
regions: FxHashSet<ty::BoundRegionKind>,
959950

960951
/// `true` if we only want regions that are known to be
961952
/// "constrained" when you equate this type with another type. In
@@ -1014,7 +1005,7 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
10141005
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
10151006
if let ty::ReLateBound(debruijn, br) = *r {
10161007
if debruijn == self.current_index {
1017-
self.regions.insert(br);
1008+
self.regions.insert(br.kind);
10181009
}
10191010
}
10201011
ControlFlow::CONTINUE

0 commit comments

Comments
 (0)