Skip to content

Commit 66fec39

Browse files
committed
More TyBuilder use
1 parent cd227f5 commit 66fec39

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

crates/hir_ty/src/autoderef.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ use log::{info, warn};
1313

1414
use crate::{
1515
db::HirDatabase,
16-
to_assoc_type_id, to_chalk_trait_id,
1716
traits::{InEnvironment, Solution},
18-
utils::generics,
19-
AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, Interner,
20-
ProjectionTy, Substitution, TraitRef, Ty, TyKind,
17+
AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, Interner, Ty,
18+
TyBuilder, TyKind,
2119
};
2220

2321
const AUTODEREF_RECURSION_LIMIT: usize = 10;
@@ -57,21 +55,20 @@ fn deref_by_trait(
5755
};
5856
let target = db.trait_data(deref_trait).associated_type_by_name(&name![Target])?;
5957

60-
let generic_params = generics(db.upcast(), target.into());
61-
if generic_params.len() != 1 {
62-
// the Target type + Deref trait should only have one generic parameter,
63-
// namely Deref's Self type
64-
return None;
65-
}
58+
let projection = {
59+
let b = TyBuilder::assoc_type_projection(db, target);
60+
if b.remaining() != 1 {
61+
// the Target type + Deref trait should only have one generic parameter,
62+
// namely Deref's Self type
63+
return None;
64+
}
65+
b.push(ty.goal.value.clone()).build()
66+
};
6667

6768
// FIXME make the Canonical / bound var handling nicer
6869

69-
let parameters =
70-
Substitution::build_for_generics(&generic_params).push(ty.goal.value.clone()).build();
71-
7270
// Check that the type implements Deref at all
73-
let trait_ref =
74-
TraitRef { trait_id: to_chalk_trait_id(deref_trait), substitution: parameters.clone() };
71+
let trait_ref = projection.trait_ref(db);
7572
let implements_goal = Canonical {
7673
binders: ty.goal.binders.clone(),
7774
value: InEnvironment {
@@ -84,21 +81,16 @@ fn deref_by_trait(
8481
}
8582

8683
// Now do the assoc type projection
87-
let projection = AliasEq {
88-
alias: AliasTy::Projection(ProjectionTy {
89-
associated_ty_id: to_assoc_type_id(target),
90-
substitution: parameters,
91-
}),
84+
let alias_eq = AliasEq {
85+
alias: AliasTy::Projection(projection),
9286
ty: TyKind::BoundVar(BoundVar::new(
9387
DebruijnIndex::INNERMOST,
9488
ty.goal.binders.len(&Interner),
9589
))
9690
.intern(&Interner),
9791
};
9892

99-
let obligation = projection.cast(&Interner);
100-
101-
let in_env = InEnvironment { goal: obligation, environment: ty.environment };
93+
let in_env = InEnvironment { goal: alias_eq.cast(&Interner), environment: ty.environment };
10294

10395
let canonical = Canonical {
10496
value: in_env,

0 commit comments

Comments
 (0)