From 1fee60181fea56ebe6b5e4aeb11cf9df25a1d087 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 1 Apr 2020 22:26:41 +0100 Subject: 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. --- crates/ra_hir/src/code_model.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src') 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::{ }; use hir_ty::{ autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, - Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, + Canonical, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; @@ -960,6 +960,38 @@ impl ImplDef { db.impl_data(self.id).target_trait.clone() } + pub fn target_trait_substs_matches(&self, db: &dyn HirDatabase, typs: &[Type]) -> bool { + let type_ref = match self.target_trait(db) { + Some(typ_ref) => typ_ref, + None => return false, + }; + let resolver = self.id.resolver(db.upcast()); + let ctx = hir_ty::TyLoweringContext::new(db, &resolver); + let ty = Ty::from_hir(&ctx, &type_ref); + let d = match ty.dyn_trait_ref() { + Some(d) => d, + None => return false, + }; + let mut matches = true; + let mut i = 0; + d.substs.walk(&mut |t| { + if matches { + if i >= typs.len() { + matches = false; + return; + } + match t { + Ty::Bound(_) => matches = i == 0, + _ => { + matches = *t == typs[i].ty.value; + i += 1; + } + } + } + }); + matches + } + pub fn target_type(&self, db: &dyn HirDatabase) -> TypeRef { db.impl_data(self.id).target_type.clone() } -- cgit v1.2.3