From e30c533eb67ba8a24708a94385849854c17e67f9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:19:37 +0300 Subject: [PATCH 1/7] move stuct to code_model_api --- crates/ra_hir/src/adt.rs | 24 ++++-------------------- crates/ra_hir/src/code_model_api.rs | 22 +++++++++++++++++++++- crates/ra_hir/src/lib.rs | 3 ++- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index b75adda84e..4b35c01b3f 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -4,41 +4,25 @@ use ra_db::Cancelable; use ra_syntax::ast::{self, NameOwner, StructFlavor}; use crate::{ - DefId, Name, AsName, + DefId, Name, AsName, Struct, db::HirDatabase, type_ref::TypeRef, }; -pub struct Struct { - def_id: DefId, -} - impl Struct { pub(crate) fn new(def_id: DefId) -> Self { Struct { def_id } } - pub fn def_id(&self) -> DefId { - self.def_id - } - - pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id)?.variant_data.clone()) - } - - pub fn struct_data(&self, db: &impl HirDatabase) -> Cancelable> { + pub(crate) fn struct_data(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.struct_data(self.def_id)?) } - - pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id)?.name.clone()) - } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructData { - name: Option, - variant_data: Arc, + pub(crate) name: Option, + pub(crate) variant_data: Arc, } impl StructData { diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 43cddb5044..12947da2d3 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -1,8 +1,10 @@ +use std::sync::Arc; + use relative_path::RelativePathBuf; use ra_db::{CrateId, Cancelable, FileId}; use ra_syntax::{ast, TreePtr, SyntaxNode}; -use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope}; +use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope, adt::VariantData}; /// hir::Crate describes a single crate. It's the main inteface with which /// crate's dependencies interact. Mostly, it should be just a proxy for the @@ -111,3 +113,21 @@ impl Module { self.problems_impl(db) } } + +pub struct Struct { + pub(crate) def_id: DefId, +} + +impl Struct { + pub fn def_id(&self) -> DefId { + self.def_id + } + + pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { + Ok(self.struct_data(db)?.variant_data.clone()) + } + + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { + Ok(self.struct_data(db)?.name.clone()) + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 9f133f1749..9b66f5bb7c 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -50,7 +50,7 @@ pub use self::{ module_tree::ModuleId, nameres::{ItemMap, PerNs, Namespace, Resolution}, function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping}, - adt::{Struct, Enum}, + adt::Enum, ty::Ty, impl_block::{ImplBlock, ImplItem}, }; @@ -60,6 +60,7 @@ pub use self::function::FnSignatureInfo; pub use self::code_model_api::{ Crate, CrateDependency, Module, ModuleSource, Problem, + Struct, }; pub enum Def { From 2d4dc22af8db8f9ebb5f2e95cd25c473494a4b70 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:22:57 +0300 Subject: [PATCH 2/7] move enum to code_model_api --- crates/ra_hir/src/adt.rs | 28 +++------------------------- crates/ra_hir/src/code_model_api.rs | 24 ++++++++++++++++++++++-- crates/ra_hir/src/lib.rs | 3 +-- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 4b35c01b3f..935f39959b 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -1,11 +1,9 @@ use std::sync::Arc; -use ra_db::Cancelable; use ra_syntax::ast::{self, NameOwner, StructFlavor}; use crate::{ - DefId, Name, AsName, Struct, - db::HirDatabase, + DefId, Name, AsName, Struct, Enum, type_ref::TypeRef, }; @@ -13,10 +11,6 @@ impl Struct { pub(crate) fn new(def_id: DefId) -> Self { Struct { def_id } } - - pub(crate) fn struct_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id)?) - } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -42,32 +36,16 @@ impl StructData { } } -pub struct Enum { - def_id: DefId, -} - impl Enum { pub(crate) fn new(def_id: DefId) -> Self { Enum { def_id } } - - pub fn def_id(&self) -> DefId { - self.def_id - } - - pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.enum_data(self.def_id)?.name.clone()) - } - - pub fn variants(&self, db: &impl HirDatabase) -> Cancelable)>> { - Ok(db.enum_data(self.def_id)?.variants.clone()) - } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { - name: Option, - variants: Vec<(Name, Arc)>, + pub(crate) name: Option, + pub(crate) variants: Vec<(Name, Arc)>, } impl EnumData { diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 12947da2d3..43bd2003c0 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -114,6 +114,7 @@ impl Module { } } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Struct { pub(crate) def_id: DefId, } @@ -124,10 +125,29 @@ impl Struct { } pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(self.struct_data(db)?.variant_data.clone()) + Ok(db.struct_data(self.def_id)?.variant_data.clone()) } pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(self.struct_data(db)?.name.clone()) + Ok(db.struct_data(self.def_id)?.name.clone()) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Enum { + pub(crate) def_id: DefId, +} + +impl Enum { + pub fn def_id(&self) -> DefId { + self.def_id + } + + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { + Ok(db.enum_data(self.def_id)?.name.clone()) + } + + pub fn variants(&self, db: &impl HirDatabase) -> Cancelable)>> { + Ok(db.enum_data(self.def_id)?.variants.clone()) } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 9b66f5bb7c..8eff2aea51 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -50,7 +50,6 @@ pub use self::{ module_tree::ModuleId, nameres::{ItemMap, PerNs, Namespace, Resolution}, function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping}, - adt::Enum, ty::Ty, impl_block::{ImplBlock, ImplItem}, }; @@ -60,7 +59,7 @@ pub use self::function::FnSignatureInfo; pub use self::code_model_api::{ Crate, CrateDependency, Module, ModuleSource, Problem, - Struct, + Struct, Enum, }; pub enum Def { From 71c7936932c7471ce968223c20fd9bb812524c7a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:23:56 +0300 Subject: [PATCH 3/7] minor --- crates/ra_hir/src/code_model_api.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 43bd2003c0..25d710c736 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -124,13 +124,13 @@ impl Struct { self.def_id } - pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id)?.variant_data.clone()) - } - pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.struct_data(self.def_id)?.name.clone()) } + + pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { + Ok(db.struct_data(self.def_id)?.variant_data.clone()) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] From 2d0ab52212f62345ba9f9d5040c553e59460b349 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:27:00 +0300 Subject: [PATCH 4/7] move variants to API --- crates/ra_hir/src/adt.rs | 30 +++------------------------- crates/ra_hir/src/code_model_api.rs | 31 ++++++++++++++++++++++++++++- crates/ra_hir/src/lib.rs | 2 +- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 935f39959b..58deea1c7e 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use ra_syntax::ast::{self, NameOwner, StructFlavor}; use crate::{ - DefId, Name, AsName, Struct, Enum, + DefId, Name, AsName, Struct, Enum, VariantData, StructField, type_ref::TypeRef, }; @@ -67,30 +67,6 @@ impl EnumData { } } -/// A single field of an enum variant or struct -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct StructField { - name: Name, - type_ref: TypeRef, -} - -impl StructField { - pub fn name(&self) -> Name { - self.name.clone() - } - pub fn type_ref(&self) -> &TypeRef { - &self.type_ref - } -} - -/// Fields of an enum variant or struct -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum VariantData { - Struct(Vec), - Tuple(Vec), - Unit, -} - impl VariantData { pub fn new(flavor: StructFlavor) -> Self { match flavor { @@ -122,8 +98,8 @@ impl VariantData { pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { self.fields() .iter() - .find(|f| f.name == *field_name) - .map(|f| &f.type_ref) + .find(|f| f.name() == field_name) + .map(|f| f.type_ref()) } pub fn fields(&self) -> &[StructField] { diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 25d710c736..3bb42ac584 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -4,7 +4,12 @@ use relative_path::RelativePathBuf; use ra_db::{CrateId, Cancelable, FileId}; use ra_syntax::{ast, TreePtr, SyntaxNode}; -use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope, adt::VariantData}; +use crate::{ + Name, DefId, Path, PerNs, + type_ref::TypeRef, + nameres::ModuleScope, + db::HirDatabase, +}; /// hir::Crate describes a single crate. It's the main inteface with which /// crate's dependencies interact. Mostly, it should be just a proxy for the @@ -114,6 +119,30 @@ impl Module { } } +/// A single field of an enum variant or struct +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StructField { + pub(crate) name: Name, + pub(crate) type_ref: TypeRef, +} + +impl StructField { + pub fn name(&self) -> &Name { + &self.name + } + pub fn type_ref(&self) -> &TypeRef { + &self.type_ref + } +} + +/// Fields of an enum variant or struct +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum VariantData { + Struct(Vec), + Tuple(Vec), + Unit, +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Struct { pub(crate) def_id: DefId, diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 8eff2aea51..cd04575d17 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -59,7 +59,7 @@ pub use self::function::FnSignatureInfo; pub use self::code_model_api::{ Crate, CrateDependency, Module, ModuleSource, Problem, - Struct, Enum, + Struct, Enum, VariantData, StructField, }; pub enum Def { From 64f202bdd7f74b081e08f2b5faee4bd91c9b44a8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:32:27 +0300 Subject: [PATCH 5/7] move variant public api to api --- crates/ra_hir/src/adt.rs | 35 +---------------------------- crates/ra_hir/src/code_model_api.rs | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 58deea1c7e..d565707548 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -26,14 +26,6 @@ impl StructData { let variant_data = Arc::new(variant_data); StructData { name, variant_data } } - - pub fn name(&self) -> Option<&Name> { - self.name.as_ref() - } - - pub fn variant_data(&self) -> &Arc { - &self.variant_data - } } impl Enum { @@ -68,7 +60,7 @@ impl EnumData { } impl VariantData { - pub fn new(flavor: StructFlavor) -> Self { + pub(crate) fn new(flavor: StructFlavor) -> Self { match flavor { StructFlavor::Tuple(fl) => { let fields = fl @@ -101,29 +93,4 @@ impl VariantData { .find(|f| f.name() == field_name) .map(|f| f.type_ref()) } - - pub fn fields(&self) -> &[StructField] { - match *self { - VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields, - _ => &[], - } - } - pub fn is_struct(&self) -> bool { - match self { - VariantData::Struct(..) => true, - _ => false, - } - } - pub fn is_tuple(&self) -> bool { - match self { - VariantData::Tuple(..) => true, - _ => false, - } - } - pub fn is_unit(&self) -> bool { - match self { - VariantData::Unit => true, - _ => false, - } - } } diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 3bb42ac584..f06f1ae663 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -143,6 +143,33 @@ pub enum VariantData { Unit, } +impl VariantData { + pub fn fields(&self) -> &[StructField] { + match self { + VariantData::Struct(fields) | VariantData::Tuple(fields) => fields, + _ => &[], + } + } + pub fn is_struct(&self) -> bool { + match self { + VariantData::Struct(..) => true, + _ => false, + } + } + pub fn is_tuple(&self) -> bool { + match self { + VariantData::Tuple(..) => true, + _ => false, + } + } + pub fn is_unit(&self) -> bool { + match self { + VariantData::Unit => true, + _ => false, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Struct { pub(crate) def_id: DefId, From 5edcf313f6b265c626b75f92c11fe62385ab9556 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:38:29 +0300 Subject: [PATCH 6/7] move query impls to adt --- crates/ra_hir/src/adt.rs | 32 ++++++++++++++++++++++---- crates/ra_hir/src/db.rs | 4 ++-- crates/ra_hir/src/query_definitions.rs | 20 +--------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index d565707548..8639bbd5c5 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -1,9 +1,10 @@ use std::sync::Arc; -use ra_syntax::ast::{self, NameOwner, StructFlavor}; +use ra_db::Cancelable; +use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; use crate::{ - DefId, Name, AsName, Struct, Enum, VariantData, StructField, + DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, type_ref::TypeRef, }; @@ -20,12 +21,24 @@ pub struct StructData { } impl StructData { - pub(crate) fn new(struct_def: &ast::StructDef) -> StructData { + fn new(struct_def: &ast::StructDef) -> StructData { let name = struct_def.name().map(|n| n.as_name()); let variant_data = VariantData::new(struct_def.flavor()); let variant_data = Arc::new(variant_data); StructData { name, variant_data } } + + pub(crate) fn struct_data_query( + db: &impl HirDatabase, + def_id: DefId, + ) -> Cancelable> { + let def_loc = def_id.loc(db); + assert!(def_loc.kind == DefKind::Struct); + let syntax = db.file_item(def_loc.source_item_id); + let struct_def = + ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); + Ok(Arc::new(StructData::new(struct_def))) + } } impl Enum { @@ -41,7 +54,7 @@ pub struct EnumData { } impl EnumData { - pub(crate) fn new(enum_def: &ast::EnumDef) -> Self { + fn new(enum_def: &ast::EnumDef) -> Self { let name = enum_def.name().map(|n| n.as_name()); let variants = if let Some(evl) = enum_def.variant_list() { evl.variants() @@ -57,6 +70,17 @@ impl EnumData { }; EnumData { name, variants } } + + pub(crate) fn enum_data_query( + db: &impl HirDatabase, + def_id: DefId, + ) -> Cancelable> { + let def_loc = def_id.loc(db); + assert!(def_loc.kind == DefKind::Enum); + let syntax = db.file_item(def_loc.source_item_id); + let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); + Ok(Arc::new(EnumData::new(enum_def))) + } } impl VariantData { diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 03e65387d8..bb4fb3d663 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -38,12 +38,12 @@ pub trait HirDatabase: SyntaxDatabase fn struct_data(def_id: DefId) -> Cancelable> { type StructDataQuery; - use fn query_definitions::struct_data; + use fn crate::adt::StructData::struct_data_query; } fn enum_data(def_id: DefId) -> Cancelable> { type EnumDataQuery; - use fn query_definitions::enum_data; + use fn crate::adt::EnumData::enum_data_query; } fn infer(def_id: DefId) -> Cancelable> { diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 380ea54104..ab4e6e629f 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -11,13 +11,12 @@ use ra_syntax::{ use ra_db::{SourceRootId, Cancelable,}; use crate::{ - SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource, + SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, MacroCallLoc, db::HirDatabase, function::FnScopes, module_tree::ModuleId, nameres::{InputModuleItems, ItemMap, Resolver}, - adt::{StructData, EnumData}, }; pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable> { @@ -26,23 +25,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable Cancelable> { - let def_loc = def_id.loc(db); - assert!(def_loc.kind == DefKind::Struct); - let syntax = db.file_item(def_loc.source_item_id); - let struct_def = - ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); - Ok(Arc::new(StructData::new(struct_def))) -} - -pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable> { - let def_loc = def_id.loc(db); - assert!(def_loc.kind == DefKind::Enum); - let syntax = db.file_item(def_loc.source_item_id); - let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); - Ok(Arc::new(EnumData::new(enum_def))) -} - pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc { let source_file = db.hir_source_file(file_id); let res = SourceFileItems::new(file_id, &source_file); From 54e5c4f0859ecbac0762051ca680a567c67cff6f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:40:02 +0300 Subject: [PATCH 7/7] minimize visibility --- crates/ra_hir/src/adt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 8639bbd5c5..602e7db745 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -84,7 +84,7 @@ impl EnumData { } impl VariantData { - pub(crate) fn new(flavor: StructFlavor) -> Self { + fn new(flavor: StructFlavor) -> Self { match flavor { StructFlavor::Tuple(fl) => { let fields = fl