Skip to content

Commit

Permalink
Rename shift_bound_vars{_out} to align with Chalk
Browse files Browse the repository at this point in the history
  • Loading branch information
flodiebold committed Apr 5, 2021
1 parent 0bf8482 commit 5737e73
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
5 changes: 3 additions & 2 deletions crates/hir_ty/src/lib.rs
Expand Up @@ -124,7 +124,7 @@ impl<T> Binders<T> {
where
T: TypeWalk,
{
Binders::empty(&Interner, value.shift_bound_vars(DebruijnIndex::ONE))
Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
}
}

Expand Down Expand Up @@ -209,7 +209,8 @@ impl CallableSig {
params_and_return: fn_ptr
.substs
.clone()
.shift_bound_vars_out(DebruijnIndex::ONE)
.shifted_out_to(DebruijnIndex::ONE)
.expect("unexpected lifetime vars in fn ptr")
.interned()
.iter()
.map(|arg| arg.assert_ty_ref(&Interner).clone())
Expand Down
18 changes: 9 additions & 9 deletions crates/hir_ty/src/lower.rs
Expand Up @@ -483,7 +483,7 @@ impl<'a> TyLoweringContext<'a> {
};
// We need to shift in the bound vars, since
// associated_type_shorthand_candidates does not do that
let substs = substs.shift_bound_vars(self.in_binders);
let substs = substs.shifted_in_from(self.in_binders);
// FIXME handle type parameters on the segment
return Some(
TyKind::Alias(AliasTy::Projection(ProjectionTy {
Expand Down Expand Up @@ -831,20 +831,20 @@ pub fn associated_type_shorthand_candidates<R>(
};

match res {
// FIXME: how to correctly handle higher-ranked bounds here?
TypeNs::SelfType(impl_id) => search(
db.impl_trait(impl_id)?
.into_value_and_skipped_binders()
.0
.shift_bound_vars_out(DebruijnIndex::ONE),
// we're _in_ the impl -- the binders get added back later. Correct,
// but it would be nice to make this more explicit
db.impl_trait(impl_id)?.into_value_and_skipped_binders().0,
),
TypeNs::GenericParam(param_id) => {
let predicates = db.generic_predicates_for_param(param_id);
let res = predicates.iter().find_map(|pred| match pred.skip_binders().skip_binders() {
// FIXME: how to correctly handle higher-ranked bounds here?
WhereClause::Implemented(tr) => {
search(tr.clone().shift_bound_vars_out(DebruijnIndex::ONE))
}
WhereClause::Implemented(tr) => search(
tr.clone()
.shifted_out_to(DebruijnIndex::ONE)
.expect("FIXME unexpected higher-ranked trait bound"),
),
_ => None,
});
if let res @ Some(_) = res {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_ty/src/traits/chalk/mapping.rs
Expand Up @@ -531,7 +531,7 @@ pub(super) fn generic_predicate_to_inline_bound(
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
// An InlineBound is like a GenericPredicate, except the self type is left out.
// We don't have a special type for this, but Chalk does.
let self_ty_shifted_in = self_ty.clone().shift_bound_vars(DebruijnIndex::ONE);
let self_ty_shifted_in = self_ty.clone().shifted_in_from(DebruijnIndex::ONE);
let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
match pred {
WhereClause::Implemented(trait_ref) => {
Expand Down
10 changes: 7 additions & 3 deletions crates/hir_ty/src/utils.rs
Expand Up @@ -66,9 +66,11 @@ fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef) -> Vec<Tr
.filter_map(|pred| {
pred.as_ref().filter_map(|pred| match pred.skip_binders() {
// FIXME: how to correctly handle higher-ranked bounds here?
WhereClause::Implemented(tr) => {
Some(tr.clone().shift_bound_vars_out(DebruijnIndex::ONE))
}
WhereClause::Implemented(tr) => Some(
tr.clone()
.shifted_out_to(DebruijnIndex::ONE)
.expect("FIXME unexpected higher-ranked trait bound"),
),
_ => None,
})
})
Expand Down Expand Up @@ -103,6 +105,8 @@ pub(super) fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<Tra
/// we have `Self: Trait<u32, i32>` and `Trait<T, U>: OtherTrait<U>` we'll get
/// `Self: OtherTrait<i32>`.
pub(super) fn all_super_trait_refs(db: &dyn HirDatabase, trait_ref: TraitRef) -> Vec<TraitRef> {
// FIXME: replace by Chalk's `super_traits`, maybe make this a query

// we need to take care a bit here to avoid infinite loops in case of cycles
// (i.e. if we have `trait A: B; trait B: A;`)
let mut result = vec![trait_ref];
Expand Down
22 changes: 12 additions & 10 deletions crates/hir_ty/src/walk.rs
Expand Up @@ -82,7 +82,7 @@ pub trait TypeWalk {
*ty = substs.interned()[bound.index]
.assert_ty_ref(&Interner)
.clone()
.shift_bound_vars(binders);
.shifted_in_from(binders);
}
}
},
Expand All @@ -92,7 +92,7 @@ pub trait TypeWalk {
}

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

/// Shifts debruijn indices of `TyKind::Bound` vars out (down) by `n`.
fn shift_bound_vars_out(self, n: DebruijnIndex) -> Self
fn shifted_out_to(self, n: DebruijnIndex) -> Option<Self>
where
Self: Sized + std::fmt::Debug,
{
self.fold_binders(
&mut |ty, binders| match ty.kind(&Interner) {
TyKind::BoundVar(bound) if bound.debruijn >= binders => {
TyKind::BoundVar(bound.shifted_out_to(n).unwrap_or(bound.clone()))
.intern(&Interner)
Some(self.fold_binders(
&mut |ty, binders| {
match ty.kind(&Interner) {
TyKind::BoundVar(bound) if bound.debruijn >= binders => {
TyKind::BoundVar(bound.shifted_out_to(n).unwrap_or(bound.clone()))
.intern(&Interner)
}
_ => ty,
}
_ => ty,
},
DebruijnIndex::INNERMOST,
)
))
}
}

Expand Down

0 comments on commit 5737e73

Please sign in to comment.