diff options
Diffstat (limited to 'crates/hir')
-rw-r--r-- | crates/hir/src/code_model.rs | 37 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 2 |
2 files changed, 36 insertions, 3 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index c75d46bff..031c91ccf 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -30,8 +30,12 @@ use hir_expand::{ | |||
30 | use hir_ty::{ | 30 | use hir_ty::{ |
31 | autoderef, | 31 | autoderef, |
32 | display::{HirDisplayError, HirFormatter}, | 32 | display::{HirDisplayError, HirFormatter}, |
33 | method_resolution, ApplicationTy, CallableDefId, Canonical, FnSig, GenericPredicate, | 33 | method_resolution, |
34 | InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, | 34 | traits::Solution, |
35 | traits::SolutionVariables, | ||
36 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, | ||
37 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, | ||
38 | TyDefId, TyKind, TypeCtor, | ||
35 | }; | 39 | }; |
36 | use rustc_hash::FxHashSet; | 40 | use rustc_hash::FxHashSet; |
37 | use stdx::impl_from; | 41 | use stdx::impl_from; |
@@ -1362,6 +1366,35 @@ impl Type { | |||
1362 | db.trait_solve(self.krate, goal).is_some() | 1366 | db.trait_solve(self.krate, goal).is_some() |
1363 | } | 1367 | } |
1364 | 1368 | ||
1369 | pub fn normalize_trait_assoc_type( | ||
1370 | &self, | ||
1371 | db: &dyn HirDatabase, | ||
1372 | r#trait: Trait, | ||
1373 | args: &[Type], | ||
1374 | alias: TypeAlias, | ||
1375 | ) -> Option<Ty> { | ||
1376 | let subst = Substs::build_for_def(db, r#trait.id) | ||
1377 | .push(self.ty.value.clone()) | ||
1378 | .fill(args.iter().map(|t| t.ty.value.clone())) | ||
1379 | .build(); | ||
1380 | let predicate = ProjectionPredicate { | ||
1381 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | ||
1382 | ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)), | ||
1383 | }; | ||
1384 | let goal = Canonical { | ||
1385 | value: InEnvironment::new( | ||
1386 | self.ty.environment.clone(), | ||
1387 | Obligation::Projection(predicate), | ||
1388 | ), | ||
1389 | kinds: Arc::new([TyKind::General]), | ||
1390 | }; | ||
1391 | |||
1392 | match db.trait_solve(self.krate, goal)? { | ||
1393 | Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(), | ||
1394 | Solution::Ambig(_) => None, | ||
1395 | } | ||
1396 | } | ||
1397 | |||
1365 | pub fn is_copy(&self, db: &dyn HirDatabase) -> bool { | 1398 | pub fn is_copy(&self, db: &dyn HirDatabase) -> bool { |
1366 | let lang_item = db.lang_item(self.krate, SmolStr::new("copy")); | 1399 | let lang_item = db.lang_item(self.krate, SmolStr::new("copy")); |
1367 | let copy_trait = match lang_item { | 1400 | let copy_trait = match lang_item { |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 171118d98..4094a76cb 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -55,7 +55,7 @@ pub use hir_def::{ | |||
55 | type_ref::{Mutability, TypeRef}, | 55 | type_ref::{Mutability, TypeRef}, |
56 | }; | 56 | }; |
57 | pub use hir_expand::{ | 57 | pub use hir_expand::{ |
58 | name::AsName, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, | 58 | name::known, name::AsName, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, |
59 | /* FIXME */ MacroDefId, MacroFile, Origin, | 59 | /* FIXME */ MacroDefId, MacroFile, Origin, |
60 | }; | 60 | }; |
61 | pub use hir_ty::display::HirDisplay; | 61 | pub use hir_ty::display::HirDisplay; |