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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 5 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 8 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 5 deletions
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
{

0 commit comments

Comments
 (0)