From e0b06cb672b7aae770fea24e4a5efdbec8cbf5c6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:13:56 +0300 Subject: Switch to StaticLoc for statics --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/code_model/src.rs | 2 +- crates/ra_hir/src/from_source.rs | 15 +++++++++++++-- crates/ra_hir/src/source_binder.rs | 12 ++++-------- crates/ra_hir/src/ty/tests.rs | 2 -- 5 files changed, 19 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 36ea8d8bf..905bb5bcb 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -734,7 +734,7 @@ pub struct Static { impl Static { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 59cda2e89..b9d21bdd7 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -88,7 +88,7 @@ impl HasSource for Const { impl HasSource for Static { type Ast = ast::StaticDef; fn source(self, db: &impl DefDatabase) -> Source { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Trait { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index c3c3b05ed..e6eefcace 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -104,10 +104,21 @@ impl FromSource for Const { impl FromSource for Static { type Ast = ast::StaticDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { - let id = from_source(db, src)?; - Some(Static { id }) + let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { + Container::Module(it) => it, + Container::Trait(_) | Container::ImplBlock(_) => return None, + }; + module + .declarations(db) + .into_iter() + .filter_map(|it| match it { + ModuleDef::Static(it) => Some(it), + _ => None, + }) + .find(|it| same_source(&it.source(db), &src)) } } + impl FromSource for TypeAlias { type Ast = ast::TypeAliasDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0a836c913..cfc4bd326 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -11,7 +11,7 @@ use hir_def::{ expr::{ExprId, PatId}, path::known, resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, - DefWithBodyId, LocationCtx, + DefWithBodyId, }; use hir_expand::{ name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source, @@ -28,8 +28,8 @@ use crate::{ expr::{BodySourceMap, ExprScopes, ScopeId}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, - GenericParam, HasBody, Local, MacroDef, Module, Name, Path, ScopeDef, Static, Struct, Trait, - Ty, TypeAlias, + GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, + TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { @@ -68,16 +68,12 @@ fn def_with_body_from_child_node( db: &impl HirDatabase, child: Source<&SyntaxNode>, ) -> Option { - let module_source = crate::ModuleSource::from_child_node(db, child); - let module = Module::from_definition(db, Source::new(child.file_id, module_source))?; - let ctx = LocationCtx::new(db, module.id, child.file_id); - child.value.ancestors().find_map(|node| { match_ast! { match node { ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); }, - ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) }, + ast::StaticDef(def) => { return Static::from_source(db, child.with_value(def)).map(DefWithBody::from); }, _ => { None }, } } diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 17a50cf74..3209c66bd 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2550,8 +2550,6 @@ fn test() { [233; 246) 'GLOBAL_STATIC': u32 [256; 257) 'w': u32 [260; 277) 'GLOBAL...IC_MUT': u32 - [118; 120) '99': u32 - [161; 163) '99': u32 "### ); } -- cgit v1.2.3 From 151180057b0304ee4dee75401e8f1aaa7aaaec21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:20:59 +0300 Subject: Simplify --- crates/ra_hir/src/from_source.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index e6eefcace..f506bba70 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -282,7 +282,9 @@ where let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); let module = Module::from_definition(db, Source::new(src.file_id, module_src))?; let ctx = LocationCtx::new(db, module.id, src.file_id); - Some(DEF::from_ast(ctx, &src.value)) + let items = db.ast_id_map(src.file_id); + let item_id = items.ast_id(&src.value); + Some(DEF::from_ast_id(ctx, item_id)) } enum Container { -- cgit v1.2.3