diff options
author | Matthew Hall <[email protected]> | 2020-04-01 22:26:41 +0100 |
---|---|---|
committer | Matthew Hall <[email protected]> | 2020-04-01 22:26:41 +0100 |
commit | 1fee60181fea56ebe6b5e4aeb11cf9df25a1d087 (patch) | |
tree | 0a157b2f478a2b4334167da483f401d0ba5e4938 /crates/ra_hir | |
parent | 1c2d4135db867efe335a0654d86429bea7bb9caf (diff) |
Add impl From for enum variant assist
Basically adds a From impl for tuple enum variants with one field. Added
to cover the fairly common case of implementing your own Error that can
be created from another one, although other use cases exist.
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index cd2a8fc62..3889a7e5a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -23,7 +23,7 @@ use hir_expand::{ | |||
23 | }; | 23 | }; |
24 | use hir_ty::{ | 24 | use hir_ty::{ |
25 | autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, | 25 | autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, |
26 | Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, | 26 | Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, |
27 | }; | 27 | }; |
28 | use ra_db::{CrateId, Edition, FileId}; | 28 | use ra_db::{CrateId, Edition, FileId}; |
29 | use ra_prof::profile; | 29 | use ra_prof::profile; |
@@ -960,6 +960,38 @@ impl ImplDef { | |||
960 | db.impl_data(self.id).target_trait.clone() | 960 | db.impl_data(self.id).target_trait.clone() |
961 | } | 961 | } |
962 | 962 | ||
963 | pub fn target_trait_substs_matches(&self, db: &dyn HirDatabase, typs: &[Type]) -> bool { | ||
964 | let type_ref = match self.target_trait(db) { | ||
965 | Some(typ_ref) => typ_ref, | ||
966 | None => return false, | ||
967 | }; | ||
968 | let resolver = self.id.resolver(db.upcast()); | ||
969 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | ||
970 | let ty = Ty::from_hir(&ctx, &type_ref); | ||
971 | let d = match ty.dyn_trait_ref() { | ||
972 | Some(d) => d, | ||
973 | None => return false, | ||
974 | }; | ||
975 | let mut matches = true; | ||
976 | let mut i = 0; | ||
977 | d.substs.walk(&mut |t| { | ||
978 | if matches { | ||
979 | if i >= typs.len() { | ||
980 | matches = false; | ||
981 | return; | ||
982 | } | ||
983 | match t { | ||
984 | Ty::Bound(_) => matches = i == 0, | ||
985 | _ => { | ||
986 | matches = *t == typs[i].ty.value; | ||
987 | i += 1; | ||
988 | } | ||
989 | } | ||
990 | } | ||
991 | }); | ||
992 | matches | ||
993 | } | ||
994 | |||
963 | pub fn target_type(&self, db: &dyn HirDatabase) -> TypeRef { | 995 | pub fn target_type(&self, db: &dyn HirDatabase) -> TypeRef { |
964 | db.impl_data(self.id).target_type.clone() | 996 | db.impl_data(self.id).target_type.clone() |
965 | } | 997 | } |