From a3d6ddbe694498a1bf69c6253422efb89431164e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:27:21 +0100 Subject: More natural trait setup --- crates/ra_hir/src/from_source.rs | 21 +++++++++--------- crates/ra_hir/src/source_binder.rs | 45 ++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 30e818892..caaff012a 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -14,8 +14,7 @@ use ra_syntax::{ use crate::{ db::{DefDatabase, HirDatabase}, - Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static, - Struct, Trait, TypeAlias, TypeParam, + DefWithBody, InFile, Local, Module, SourceBinder, TypeParam, }; impl Local { @@ -25,9 +24,9 @@ impl Local { let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - ast::ConstDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, - ast::StaticDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, - ast::FnDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, + ast::ConstDef(value) => { sb.to_def(InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { sb.to_def(InFile { value, file_id})?.into() }, + ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.into() }, _ => return None, } }; @@ -47,12 +46,12 @@ impl TypeParam { let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - ast::FnDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, - ast::StructDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, - ast::EnumDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, - ast::TraitDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, - ast::TypeAliasDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, - ast::ImplBlock(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, + ast::StructDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, + ast::EnumDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, + ast::TraitDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, + ast::TypeAliasDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, + ast::ImplBlock(value) => { sb.to_def(InFile { value, file_id})?.id.into() }, _ => return None, } }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 66930e492..c02175c06 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -52,11 +52,7 @@ impl SourceBinder<'_, DB> { SourceAnalyzer::new_for_resolver(resolver, src) } - pub fn to_def(&mut self, src: InFile) -> Option - where - D: From, - T: ToId, - { + pub fn to_def(&mut self, src: InFile) -> Option { let id: T::ID = self.to_id(src)?; Some(id.into()) } @@ -114,6 +110,39 @@ impl SourceBinder<'_, DB> { } } +pub trait ToId: Sized + AstNode + 'static { + type ID: Sized + Copy + 'static; + fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) + -> Option; +} + +pub trait ToDef: ToId { + type Def: From; +} + +macro_rules! to_def_impls { + ($(($def:path, $ast:path)),* ,) => {$( + impl ToDef for $ast { + type Def = $def; + } + )*} +} + +to_def_impls![ + (crate::Struct, ast::StructDef), + (crate::Enum, ast::EnumDef), + (crate::Union, ast::UnionDef), + (crate::Trait, ast::TraitDef), + (crate::ImplBlock, ast::ImplBlock), + (crate::TypeAlias, ast::TypeAliasDef), + (crate::Const, ast::ConstDef), + (crate::Static, ast::StaticDef), + (crate::Function, ast::FnDef), + (crate::StructField, ast::RecordFieldDef), + (crate::EnumVariant, ast::EnumVariant), + (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros +]; + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] enum ChildContainer { DefWithBodyId(DefWithBodyId), @@ -133,12 +162,6 @@ impl_froms! { VariantId, } -pub trait ToId: Sized + AstNode + 'static { - type ID: Sized + Copy + 'static; - fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) - -> Option; -} - pub trait ToIdByKey: Sized + AstNode + 'static { type ID: Sized + Copy + 'static; const KEY: Key; -- cgit v1.2.3