Skip to content

Commit a316d58

Browse files
committed
Rename shift_bound_vars{_out} to align with Chalk
1 parent fbab69c commit a316d58

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

crates/hir_ty/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<T> Binders<T> {
124124
where
125125
T: TypeWalk,
126126
{
127-
Binders::empty(&Interner, value.shift_bound_vars(DebruijnIndex::ONE))
127+
Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
128128
}
129129
}
130130

@@ -209,7 +209,8 @@ impl CallableSig {
209209
params_and_return: fn_ptr
210210
.substs
211211
.clone()
212-
.shift_bound_vars_out(DebruijnIndex::ONE)
212+
.shifted_out_to(DebruijnIndex::ONE)
213+
.expect("unexpected lifetime vars in fn ptr")
213214
.interned()
214215
.iter()
215216
.map(|arg| arg.assert_ty_ref(&Interner).clone())

crates/hir_ty/src/lower.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl<'a> TyLoweringContext<'a> {
483483
};
484484
// We need to shift in the bound vars, since
485485
// associated_type_shorthand_candidates does not do that
486-
let substs = substs.shift_bound_vars(self.in_binders);
486+
let substs = substs.shifted_in_from(self.in_binders);
487487
// FIXME handle type parameters on the segment
488488
return Some(
489489
TyKind::Alias(AliasTy::Projection(ProjectionTy {
@@ -831,20 +831,20 @@ pub fn associated_type_shorthand_candidates<R>(
831831
};
832832

833833
match res {
834-
// FIXME: how to correctly handle higher-ranked bounds here?
835834
TypeNs::SelfType(impl_id) => search(
836-
db.impl_trait(impl_id)?
837-
.into_value_and_skipped_binders()
838-
.0
839-
.shift_bound_vars_out(DebruijnIndex::ONE),
835+
// we're _in_ the impl -- the binders get added back later. Correct,
836+
// but it would be nice to make this more explicit
837+
db.impl_trait(impl_id)?.into_value_and_skipped_binders().0,
840838
),
841839
TypeNs::GenericParam(param_id) => {
842840
let predicates = db.generic_predicates_for_param(param_id);
843841
let res = predicates.iter().find_map(|pred| match pred.skip_binders().skip_binders() {
844842
// FIXME: how to correctly handle higher-ranked bounds here?
845-
WhereClause::Implemented(tr) => {
846-
search(tr.clone().shift_bound_vars_out(DebruijnIndex::ONE))
847-
}
843+
WhereClause::Implemented(tr) => search(
844+
tr.clone()
845+
.shifted_out_to(DebruijnIndex::ONE)
846+
.expect("FIXME unexpected higher-ranked trait bound"),
847+
),
848848
_ => None,
849849
});
850850
if let res @ Some(_) = res {

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ pub(super) fn generic_predicate_to_inline_bound(
531531
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
532532
// An InlineBound is like a GenericPredicate, except the self type is left out.
533533
// We don't have a special type for this, but Chalk does.
534-
let self_ty_shifted_in = self_ty.clone().shift_bound_vars(DebruijnIndex::ONE);
534+
let self_ty_shifted_in = self_ty.clone().shifted_in_from(DebruijnIndex::ONE);
535535
let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
536536
match pred {
537537
WhereClause::Implemented(trait_ref) => {

crates/hir_ty/src/utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef) -> Vec<Tr
6666
.filter_map(|pred| {
6767
pred.as_ref().filter_map(|pred| match pred.skip_binders() {
6868
// FIXME: how to correctly handle higher-ranked bounds here?
69-
WhereClause::Implemented(tr) => {
70-
Some(tr.clone().shift_bound_vars_out(DebruijnIndex::ONE))
71-
}
69+
WhereClause::Implemented(tr) => Some(
70+
tr.clone()
71+
.shifted_out_to(DebruijnIndex::ONE)
72+
.expect("FIXME unexpected higher-ranked trait bound"),
73+
),
7274
_ => None,
7375
})
7476
})
@@ -103,6 +105,8 @@ pub(super) fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<Tra
103105
/// we have `Self: Trait<u32, i32>` and `Trait<T, U>: OtherTrait<U>` we'll get
104106
/// `Self: OtherTrait<i32>`.
105107
pub(super) fn all_super_trait_refs(db: &dyn HirDatabase, trait_ref: TraitRef) -> Vec<TraitRef> {
108+
// FIXME: replace by Chalk's `super_traits`, maybe make this a query
109+
106110
// we need to take care a bit here to avoid infinite loops in case of cycles
107111
// (i.e. if we have `trait A: B; trait B: A;`)
108112
let mut result = vec![trait_ref];

crates/hir_ty/src/walk.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub trait TypeWalk {
8282
*ty = substs.interned()[bound.index]
8383
.assert_ty_ref(&Interner)
8484
.clone()
85-
.shift_bound_vars(binders);
85+
.shifted_in_from(binders);
8686
}
8787
}
8888
},
@@ -92,7 +92,7 @@ pub trait TypeWalk {
9292
}
9393

9494
/// Shifts up debruijn indices of `TyKind::Bound` vars by `n`.
95-
fn shift_bound_vars(self, n: DebruijnIndex) -> Self
95+
fn shifted_in_from(self, n: DebruijnIndex) -> Self
9696
where
9797
Self: Sized,
9898
{
@@ -108,20 +108,22 @@ pub trait TypeWalk {
108108
}
109109

110110
/// Shifts debruijn indices of `TyKind::Bound` vars out (down) by `n`.
111-
fn shift_bound_vars_out(self, n: DebruijnIndex) -> Self
111+
fn shifted_out_to(self, n: DebruijnIndex) -> Option<Self>
112112
where
113113
Self: Sized + std::fmt::Debug,
114114
{
115-
self.fold_binders(
116-
&mut |ty, binders| match ty.kind(&Interner) {
117-
TyKind::BoundVar(bound) if bound.debruijn >= binders => {
118-
TyKind::BoundVar(bound.shifted_out_to(n).unwrap_or(bound.clone()))
119-
.intern(&Interner)
115+
Some(self.fold_binders(
116+
&mut |ty, binders| {
117+
match ty.kind(&Interner) {
118+
TyKind::BoundVar(bound) if bound.debruijn >= binders => {
119+
TyKind::BoundVar(bound.shifted_out_to(n).unwrap_or(bound.clone()))
120+
.intern(&Interner)
121+
}
122+
_ => ty,
120123
}
121-
_ => ty,
122124
},
123125
DebruijnIndex::INNERMOST,
124-
)
126+
))
125127
}
126128
}
127129

0 commit comments

Comments
 (0)