Skip to content

Commit 4fdac23

Browse files
committed
Auto merge of #76814 - jackh726:binder-refactor, r=nikomatsakis
Refactor `Binder` to track bound vars c.c. `@rust-lang/wg-traits` This is super early (and might just get closed at some point), but want to get at least an initial idea of the perf impact. r? `@ghost`
2 parents a5029ac + 7108918 commit 4fdac23

File tree

106 files changed

+1851
-714
lines changed

Some content is hidden

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

106 files changed

+1851
-714
lines changed

compiler/rustc_hir/src/hir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub enum TraitBoundModifier {
402402
/// `typeck::collect::compute_bounds` matches these against
403403
/// the "special" built-in traits (see `middle::lang_items`) and
404404
/// detects `Copy`, `Send` and `Sync`.
405-
#[derive(Debug, HashStable_Generic)]
405+
#[derive(Clone, Debug, HashStable_Generic)]
406406
pub enum GenericBound<'hir> {
407407
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
408408
// FIXME(davidtwco): Introduce `PolyTraitRef::LangItem`
@@ -2556,7 +2556,7 @@ pub enum UseKind {
25562556
/// that the `ref_id` is for. Note that `ref_id`'s value is not the `HirId` of the
25572557
/// trait being referred to but just a unique `HirId` that serves as a key
25582558
/// within the resolution map.
2559-
#[derive(Debug, HashStable_Generic)]
2559+
#[derive(Clone, Debug, HashStable_Generic)]
25602560
pub struct TraitRef<'hir> {
25612561
pub path: &'hir Path<'hir>,
25622562
// Don't hash the `ref_id`. It is tracked via the thing it is used to access.
@@ -2575,7 +2575,7 @@ impl TraitRef<'_> {
25752575
}
25762576
}
25772577

2578-
#[derive(Debug, HashStable_Generic)]
2578+
#[derive(Clone, Debug, HashStable_Generic)]
25792579
pub struct PolyTraitRef<'hir> {
25802580
/// The `'a` in `for<'a> Foo<&'a T>`.
25812581
pub bound_generic_params: &'hir [GenericParam<'hir>],

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
293293
self.tcx
294294
}
295295

296-
fn fold_binder<T>(&mut self, t: ty::Binder<T>) -> ty::Binder<T>
296+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
297297
where
298298
T: TypeFoldable<'tcx>,
299299
{
@@ -621,7 +621,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
621621
r: ty::Region<'tcx>,
622622
) -> ty::Region<'tcx> {
623623
let var = self.canonical_var(info, r.into());
624-
let br = ty::BoundRegion { kind: ty::BrAnon(var.as_u32()) };
624+
let br = ty::BoundRegion { var, kind: ty::BrAnon(var.as_u32()) };
625625
let region = ty::ReLateBound(self.binder_index, br);
626626
self.tcx().mk_region(region)
627627
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
439439

440440
// We only allow a `ty::INNERMOST` index in substitutions.
441441
assert_eq!(debruijn, ty::INNERMOST);
442-
opt_values[br.assert_bound_var()] = Some(*original_value);
442+
opt_values[br.var] = Some(*original_value);
443443
}
444444
}
445445
GenericArgKind::Const(result_value) => {

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ where
7171
if var_values.var_values.is_empty() {
7272
value
7373
} else {
74-
let fld_r =
75-
|br: ty::BoundRegion| match var_values.var_values[br.assert_bound_var()].unpack() {
76-
GenericArgKind::Lifetime(l) => l,
77-
r => bug!("{:?} is a region but value is {:?}", br, r),
78-
};
74+
let fld_r = |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
75+
GenericArgKind::Lifetime(l) => l,
76+
r => bug!("{:?} is a region but value is {:?}", br, r),
77+
};
7978

8079
let fld_t = |bound_ty: ty::BoundTy| match var_values.var_values[bound_ty.var].unpack() {
8180
GenericArgKind::Type(ty) => ty,

compiler/rustc_infer/src/infer/combine.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,9 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
545545

546546
fn binders<T>(
547547
&mut self,
548-
a: ty::Binder<T>,
549-
b: ty::Binder<T>,
550-
) -> RelateResult<'tcx, ty::Binder<T>>
548+
a: ty::Binder<'tcx, T>,
549+
b: ty::Binder<'tcx, T>,
550+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
551551
where
552552
T: Relate<'tcx>,
553553
{
@@ -840,9 +840,9 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
840840

841841
fn binders<T>(
842842
&mut self,
843-
a: ty::Binder<T>,
844-
b: ty::Binder<T>,
845-
) -> RelateResult<'tcx, ty::Binder<T>>
843+
a: ty::Binder<'tcx, T>,
844+
b: ty::Binder<'tcx, T>,
845+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
846846
where
847847
T: Relate<'tcx>,
848848
{

compiler/rustc_infer/src/infer/equate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> {
124124

125125
fn binders<T>(
126126
&mut self,
127-
a: ty::Binder<T>,
128-
b: ty::Binder<T>,
129-
) -> RelateResult<'tcx, ty::Binder<T>>
127+
a: ty::Binder<'tcx, T>,
128+
b: ty::Binder<'tcx, T>,
129+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
130130
where
131131
T: Relate<'tcx>,
132132
{

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
514514

515515
fn print_dyn_existential(
516516
self,
517-
_predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
517+
_predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
518518
) -> Result<Self::DynExistential, Self::Error> {
519519
Err(NonTrivialPath)
520520
}

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
115115
// error. We will then search the function parameters for a bound
116116
// region at the right depth with the same index
117117
(
118-
Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
118+
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
119119
ty::BrAnon(br_index),
120120
) => {
121121
debug!(
@@ -143,7 +143,7 @@ impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
143143
// error. We will then search the function parameters for a bound
144144
// region at the right depth with the same index
145145
(
146-
Some(rl::Region::LateBound(debruijn_index, id, _)),
146+
Some(rl::Region::LateBound(debruijn_index, _, id, _)),
147147
ty::BrNamed(def_id, _),
148148
) => {
149149
debug!(
@@ -162,8 +162,8 @@ impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
162162
rl::Region::Static
163163
| rl::Region::Free(_, _)
164164
| rl::Region::EarlyBound(_, _, _)
165-
| rl::Region::LateBound(_, _, _)
166-
| rl::Region::LateBoundAnon(_, _),
165+
| rl::Region::LateBound(_, _, _, _)
166+
| rl::Region::LateBoundAnon(_, _, _),
167167
)
168168
| None,
169169
_,
@@ -217,7 +217,10 @@ impl Visitor<'tcx> for TyPathVisitor<'tcx> {
217217
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
218218
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
219219
// the lifetime of the TyPath!
220-
(Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
220+
(
221+
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
222+
ty::BrAnon(br_index),
223+
) => {
221224
if debruijn_index == self.current_index && anon_index == br_index {
222225
self.found_it = true;
223226
return;
@@ -232,7 +235,7 @@ impl Visitor<'tcx> for TyPathVisitor<'tcx> {
232235
}
233236
}
234237

235-
(Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
238+
(Some(rl::Region::LateBound(debruijn_index, _, id, _)), ty::BrNamed(def_id, _)) => {
236239
debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
237240
debug!("id={:?}", id);
238241
debug!("def_id={:?}", def_id);
@@ -246,8 +249,8 @@ impl Visitor<'tcx> for TyPathVisitor<'tcx> {
246249
Some(
247250
rl::Region::Static
248251
| rl::Region::EarlyBound(_, _, _)
249-
| rl::Region::LateBound(_, _, _)
250-
| rl::Region::LateBoundAnon(_, _)
252+
| rl::Region::LateBound(_, _, _, _)
253+
| rl::Region::LateBoundAnon(_, _, _)
251254
| rl::Region::Free(_, _),
252255
)
253256
| None,

compiler/rustc_infer/src/infer/glb.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> {
8585

8686
fn binders<T>(
8787
&mut self,
88-
a: ty::Binder<T>,
89-
b: ty::Binder<T>,
90-
) -> RelateResult<'tcx, ty::Binder<T>>
88+
a: ty::Binder<'tcx, T>,
89+
b: ty::Binder<'tcx, T>,
90+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
9191
where
9292
T: Relate<'tcx>,
9393
{

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use rustc_middle::ty::{self, Binder, TypeFoldable};
1111
impl<'a, 'tcx> CombineFields<'a, 'tcx> {
1212
pub fn higher_ranked_sub<T>(
1313
&mut self,
14-
a: Binder<T>,
15-
b: Binder<T>,
14+
a: Binder<'tcx, T>,
15+
b: Binder<'tcx, T>,
1616
a_is_expected: bool,
17-
) -> RelateResult<'tcx, Binder<T>>
17+
) -> RelateResult<'tcx, Binder<'tcx, T>>
1818
where
1919
T: Relate<'tcx>,
2020
{
@@ -50,7 +50,10 @@ impl<'a, 'tcx> CombineFields<'a, 'tcx> {
5050

5151
debug!("higher_ranked_sub: OK result={:?}", result);
5252

53-
Ok(ty::Binder::bind(result))
53+
// We related `a_prime` and `b_prime`, which just had any bound vars
54+
// replaced with placeholders or infer vars, respectively. Relating
55+
// them should not introduce new bound vars.
56+
Ok(ty::Binder::dummy(result))
5457
})
5558
}
5659
}
@@ -66,7 +69,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
6669
/// the [rustc dev guide].
6770
///
6871
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
69-
pub fn replace_bound_vars_with_placeholders<T>(&self, binder: ty::Binder<T>) -> T
72+
pub fn replace_bound_vars_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
7073
where
7174
T: TypeFoldable<'tcx>,
7275
{

compiler/rustc_infer/src/infer/lub.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> {
8585

8686
fn binders<T>(
8787
&mut self,
88-
a: ty::Binder<T>,
89-
b: ty::Binder<T>,
90-
) -> RelateResult<'tcx, ty::Binder<T>>
88+
a: ty::Binder<'tcx, T>,
89+
b: ty::Binder<'tcx, T>,
90+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
9191
where
9292
T: Relate<'tcx>,
9393
{

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14061406
&self,
14071407
span: Span,
14081408
lbrct: LateBoundRegionConversionTime,
1409-
value: ty::Binder<T>,
1409+
value: ty::Binder<'tcx, T>,
14101410
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
14111411
where
14121412
T: TypeFoldable<'tcx>,

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ where
157157

158158
fn create_scope(
159159
&mut self,
160-
value: ty::Binder<impl Relate<'tcx>>,
160+
value: ty::Binder<'tcx, impl Relate<'tcx>>,
161161
universally_quantified: UniversallyQuantified,
162162
) -> BoundRegionScope<'tcx> {
163163
let mut scope = BoundRegionScope::default();
@@ -608,9 +608,9 @@ where
608608

609609
fn binders<T>(
610610
&mut self,
611-
a: ty::Binder<T>,
612-
b: ty::Binder<T>,
613-
) -> RelateResult<'tcx, ty::Binder<T>>
611+
a: ty::Binder<'tcx, T>,
612+
b: ty::Binder<'tcx, T>,
613+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
614614
where
615615
T: Relate<'tcx>,
616616
{
@@ -744,7 +744,7 @@ struct ScopeInstantiator<'me, 'tcx> {
744744
impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
745745
fn visit_binder<T: TypeFoldable<'tcx>>(
746746
&mut self,
747-
t: &ty::Binder<T>,
747+
t: &ty::Binder<'tcx, T>,
748748
) -> ControlFlow<Self::BreakTy> {
749749
self.target_index.shift_in(1);
750750
t.super_visit_with(self);
@@ -997,9 +997,9 @@ where
997997

998998
fn binders<T>(
999999
&mut self,
1000-
a: ty::Binder<T>,
1001-
_: ty::Binder<T>,
1002-
) -> RelateResult<'tcx, ty::Binder<T>>
1000+
a: ty::Binder<'tcx, T>,
1001+
_: ty::Binder<'tcx, T>,
1002+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
10031003
where
10041004
T: Relate<'tcx>,
10051005
{

compiler/rustc_infer/src/infer/sub.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
162162

163163
fn binders<T>(
164164
&mut self,
165-
a: ty::Binder<T>,
166-
b: ty::Binder<T>,
167-
) -> RelateResult<'tcx, ty::Binder<T>>
165+
a: ty::Binder<'tcx, T>,
166+
b: ty::Binder<'tcx, T>,
167+
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
168168
where
169169
T: Relate<'tcx>,
170170
{

compiler/rustc_infer/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'tcx> FulfillmentError<'tcx> {
128128
}
129129

130130
impl<'tcx> TraitObligation<'tcx> {
131-
pub fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
131+
pub fn self_ty(&self) -> ty::Binder<'tcx, Ty<'tcx>> {
132132
self.predicate.map_bound(|p| p.self_ty())
133133
}
134134
}

compiler/rustc_lint/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'tcx> LateContext<'tcx> {
909909

910910
fn print_dyn_existential(
911911
self,
912-
_predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
912+
_predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
913913
) -> Result<Self::DynExistential, Self::Error> {
914914
Ok(())
915915
}

compiler/rustc_middle/src/ich/impls_ty.rs

+5-4
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::BoundRegion { kind: 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::BoundRegion { kind: 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::BoundRegion { kind: 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 }) => {
@@ -118,12 +118,13 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for ty::BoundVar {
118118
}
119119
}
120120

121-
impl<'a, T> HashStable<StableHashingContext<'a>> for ty::Binder<T>
121+
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for ty::Binder<'tcx, T>
122122
where
123123
T: HashStable<StableHashingContext<'a>>,
124124
{
125125
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
126126
self.as_ref().skip_binder().hash_stable(hcx, hasher);
127+
self.bound_vars().hash_stable(hcx, hasher);
127128
}
128129
}
129130

compiler/rustc_middle/src/infer/canonical.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'tcx, V> Canonical<'tcx, V> {
277277
}
278278

279279
pub type QueryOutlivesConstraint<'tcx> =
280-
ty::Binder<ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>>;
280+
ty::Binder<'tcx, ty::OutlivesPredicate<GenericArg<'tcx>, Region<'tcx>>>;
281281

282282
TrivialTypeFoldableAndLiftImpls! {
283283
for <'tcx> {
@@ -314,7 +314,8 @@ impl<'tcx> CanonicalVarValues<'tcx> {
314314
tcx.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i).into())).into()
315315
}
316316
GenericArgKind::Lifetime(..) => {
317-
let br = ty::BoundRegion { kind: ty::BrAnon(i) };
317+
let br =
318+
ty::BoundRegion { var: ty::BoundVar::from_u32(i), kind: ty::BrAnon(i) };
318319
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
319320
}
320321
GenericArgKind::Const(ct) => tcx

0 commit comments

Comments
 (0)