aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir/src/code_model/src.rs2
-rw-r--r--crates/ra_hir/src/from_source.rs15
-rw-r--r--crates/ra_hir/src/source_binder.rs12
-rw-r--r--crates/ra_hir/src/ty/tests.rs2
-rw-r--r--crates/ra_hir_def/src/attr.rs2
-rw-r--r--crates/ra_hir_def/src/body.rs3
-rw-r--r--crates/ra_hir_def/src/data.rs2
-rw-r--r--crates/ra_hir_def/src/db.rs5
-rw-r--r--crates/ra_hir_def/src/docs.rs2
-rw-r--r--crates/ra_hir_def/src/lib.rs37
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs7
-rw-r--r--crates/ra_hir_def/src/resolver.rs2
13 files changed, 65 insertions, 28 deletions
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 {
734 734
735impl Static { 735impl Static {
736 pub fn module(self, db: &impl DefDatabase) -> Module { 736 pub fn module(self, db: &impl DefDatabase) -> Module {
737 Module { id: self.id.module(db) } 737 Module { id: self.id.lookup(db).module(db) }
738 } 738 }
739 739
740 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 740 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
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 {
88impl HasSource for Static { 88impl HasSource for Static {
89 type Ast = ast::StaticDef; 89 type Ast = ast::StaticDef;
90 fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> { 90 fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> {
91 self.id.source(db) 91 self.id.lookup(db).source(db)
92 } 92 }
93} 93}
94impl HasSource for Trait { 94impl 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 {
104impl FromSource for Static { 104impl FromSource for Static {
105 type Ast = ast::StaticDef; 105 type Ast = ast::StaticDef;
106 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { 106 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
107 let id = from_source(db, src)?; 107 let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
108 Some(Static { id }) 108 Container::Module(it) => it,
109 Container::Trait(_) | Container::ImplBlock(_) => return None,
110 };
111 module
112 .declarations(db)
113 .into_iter()
114 .filter_map(|it| match it {
115 ModuleDef::Static(it) => Some(it),
116 _ => None,
117 })
118 .find(|it| same_source(&it.source(db), &src))
109 } 119 }
110} 120}
121
111impl FromSource for TypeAlias { 122impl FromSource for TypeAlias {
112 type Ast = ast::TypeAliasDef; 123 type Ast = ast::TypeAliasDef;
113 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { 124 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
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::{
11 expr::{ExprId, PatId}, 11 expr::{ExprId, PatId},
12 path::known, 12 path::known,
13 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, 13 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
14 DefWithBodyId, LocationCtx, 14 DefWithBodyId,
15}; 15};
16use hir_expand::{ 16use hir_expand::{
17 name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source, 17 name::AsName, AstId, HirFileId, MacroCallId, MacroCallLoc, MacroFileKind, Source,
@@ -28,8 +28,8 @@ use crate::{
28 expr::{BodySourceMap, ExprScopes, ScopeId}, 28 expr::{BodySourceMap, ExprScopes, ScopeId},
29 ty::method_resolution::{self, implements_trait}, 29 ty::method_resolution::{self, implements_trait},
30 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, 30 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
31 GenericParam, HasBody, Local, MacroDef, Module, Name, Path, ScopeDef, Static, Struct, Trait, 31 GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty,
32 Ty, TypeAlias, 32 TypeAlias,
33}; 33};
34 34
35fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { 35fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@@ -68,16 +68,12 @@ fn def_with_body_from_child_node(
68 db: &impl HirDatabase, 68 db: &impl HirDatabase,
69 child: Source<&SyntaxNode>, 69 child: Source<&SyntaxNode>,
70) -> Option<DefWithBody> { 70) -> Option<DefWithBody> {
71 let module_source = crate::ModuleSource::from_child_node(db, child);
72 let module = Module::from_definition(db, Source::new(child.file_id, module_source))?;
73 let ctx = LocationCtx::new(db, module.id, child.file_id);
74
75 child.value.ancestors().find_map(|node| { 71 child.value.ancestors().find_map(|node| {
76 match_ast! { 72 match_ast! {
77 match node { 73 match node {
78 ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, 74 ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); },
79 ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); }, 75 ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); },
80 ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) }, 76 ast::StaticDef(def) => { return Static::from_source(db, child.with_value(def)).map(DefWithBody::from); },
81 _ => { None }, 77 _ => { None },
82 } 78 }
83 } 79 }
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() {
2550 [233; 246) 'GLOBAL_STATIC': u32 2550 [233; 246) 'GLOBAL_STATIC': u32
2551 [256; 257) 'w': u32 2551 [256; 257) 'w': u32
2552 [260; 277) 'GLOBAL...IC_MUT': u32 2552 [260; 277) 'GLOBAL...IC_MUT': u32
2553 [118; 120) '99': u32
2554 [161; 163) '99': u32
2555 "### 2553 "###
2556 ); 2554 );
2557} 2555}
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 48ce8cd93..87f411599 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -63,11 +63,11 @@ impl Attrs {
63 AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 63 AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
64 AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), 64 AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
65 }, 65 },
66 AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
67 AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 66 AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
68 AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), 67 AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db),
69 AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 68 AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
70 AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), 69 AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db),
70 AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db),
71 AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), 71 AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db),
72 AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db), 72 AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db),
73 } 73 }
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 225638b42..1589085b5 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -17,7 +17,7 @@ use crate::{
17 expr::{Expr, ExprId, Pat, PatId}, 17 expr::{Expr, ExprId, Pat, PatId},
18 nameres::CrateDefMap, 18 nameres::CrateDefMap,
19 path::Path, 19 path::Path,
20 AstItemDef, DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, 20 DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
21}; 21};
22 22
23pub struct Expander { 23pub struct Expander {
@@ -160,6 +160,7 @@ impl Body {
160 (src.file_id, c.module(db), src.value.body()) 160 (src.file_id, c.module(db), src.value.body())
161 } 161 }
162 DefWithBodyId::StaticId(s) => { 162 DefWithBodyId::StaticId(s) => {
163 let s = s.lookup(db);
163 let src = s.source(db); 164 let src = s.source(db);
164 (src.file_id, s.module(db), src.value.body()) 165 (src.file_id, s.module(db), src.value.body())
165 } 166 }
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index f0b3e198a..81a8ec18d 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -204,7 +204,7 @@ impl ConstData {
204 } 204 }
205 205
206 pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> { 206 pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> {
207 let node = konst.source(db).value; 207 let node = konst.lookup(db).source(db).value;
208 const_data_for(&node) 208 const_data_for(&node)
209 } 209 }
210} 210}
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 7fec2e8c0..32adb11bd 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -18,7 +18,8 @@ use crate::{
18 CrateDefMap, 18 CrateDefMap,
19 }, 19 },
20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, 20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId,
21 ImplId, ItemLoc, ModuleId, StaticId, StructOrUnionId, TraitId, TypeAliasId, TypeAliasLoc, 21 ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructOrUnionId, TraitId, TypeAliasId,
22 TypeAliasLoc,
22}; 23};
23 24
24#[salsa::query_group(InternDatabaseStorage)] 25#[salsa::query_group(InternDatabaseStorage)]
@@ -32,7 +33,7 @@ pub trait InternDatabase: SourceDatabase {
32 #[salsa::interned] 33 #[salsa::interned]
33 fn intern_const(&self, loc: ConstLoc) -> ConstId; 34 fn intern_const(&self, loc: ConstLoc) -> ConstId;
34 #[salsa::interned] 35 #[salsa::interned]
35 fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> StaticId; 36 fn intern_static(&self, loc: StaticLoc) -> StaticId;
36 #[salsa::interned] 37 #[salsa::interned]
37 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> TraitId; 38 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> TraitId;
38 #[salsa::interned] 39 #[salsa::interned]
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
index 69846fd1b..225511428 100644
--- a/crates/ra_hir_def/src/docs.rs
+++ b/crates/ra_hir_def/src/docs.rs
@@ -52,10 +52,10 @@ impl Documentation {
52 let src = it.parent.child_source(db); 52 let src = it.parent.child_source(db);
53 docs_from_ast(&src.value[it.local_id]) 53 docs_from_ast(&src.value[it.local_id])
54 } 54 }
55 AttrDefId::StaticId(it) => docs_from_ast(&it.source(db).value),
56 AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), 55 AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value),
57 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)), 56 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)),
58 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), 57 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
58 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
59 AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value), 59 AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
60 AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value), 60 AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value),
61 AttrDefId::ImplId(_) => None, 61 AttrDefId::ImplId(_) => None,
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index b063530c2..89f1ceb58 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -245,12 +245,24 @@ impl Lookup for ConstId {
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 245#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
246pub struct StaticId(salsa::InternId); 246pub struct StaticId(salsa::InternId);
247impl_intern_key!(StaticId); 247impl_intern_key!(StaticId);
248impl AstItemDef<ast::StaticDef> for StaticId { 248
249 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self { 249#[derive(Debug, Clone, PartialEq, Eq, Hash)]
250 db.intern_static(loc) 250pub struct StaticLoc {
251 pub container: ModuleId,
252 pub ast_id: AstId<ast::StaticDef>,
253}
254
255impl Intern for StaticLoc {
256 type ID = StaticId;
257 fn intern(self, db: &impl db::DefDatabase) -> StaticId {
258 db.intern_static(self)
251 } 259 }
252 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> { 260}
253 db.lookup_intern_static(self) 261
262impl Lookup for StaticId {
263 type Data = StaticLoc;
264 fn lookup(&self, db: &impl db::DefDatabase) -> StaticLoc {
265 db.lookup_intern_static(*self)
254 } 266 }
255} 267}
256 268
@@ -481,6 +493,12 @@ impl HasModule for ConstLoc {
481 } 493 }
482} 494}
483 495
496impl HasModule for StaticLoc {
497 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId {
498 self.container
499 }
500}
501
484pub trait HasSource { 502pub trait HasSource {
485 type Value; 503 type Value;
486 fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>; 504 fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>;
@@ -513,6 +531,15 @@ impl HasSource for ConstLoc {
513 } 531 }
514} 532}
515 533
534impl HasSource for StaticLoc {
535 type Value = ast::StaticDef;
536
537 fn source(&self, db: &impl db::DefDatabase) -> Source<ast::StaticDef> {
538 let node = self.ast_id.to_node(db);
539 Source::new(self.ast_id.file_id(), node)
540 }
541}
542
516pub trait HasChildSource { 543pub trait HasChildSource {
517 type ChildId; 544 type ChildId;
518 type Value; 545 type Value;
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 1d004b6a6..7b2487999 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -21,7 +21,7 @@ use crate::{
21 path::{Path, PathKind}, 21 path::{Path, PathKind},
22 per_ns::PerNs, 22 per_ns::PerNs,
23 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, 23 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId,
24 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, 24 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId,
25 StructOrUnionId, TraitId, TypeAliasLoc, UnionId, 25 StructOrUnionId, TraitId, TypeAliasLoc, UnionId,
26}; 26};
27 27
@@ -715,7 +715,10 @@ where
715 PerNs::values(def.into()) 715 PerNs::values(def.into())
716 } 716 }
717 raw::DefKind::Static(ast_id) => { 717 raw::DefKind::Static(ast_id) => {
718 PerNs::values(StaticId::from_ast_id(ctx, ast_id).into()) 718 let def = StaticLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
719 .intern(self.def_collector.db);
720
721 PerNs::values(def.into())
719 } 722 }
720 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), 723 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()),
721 raw::DefKind::TypeAlias(ast_id) => { 724 raw::DefKind::TypeAlias(ast_id) => {
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index b56de44dd..4ff0a091b 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -540,7 +540,7 @@ impl HasResolver for ConstId {
540 540
541impl HasResolver for StaticId { 541impl HasResolver for StaticId {
542 fn resolver(self, db: &impl DefDatabase) -> Resolver { 542 fn resolver(self, db: &impl DefDatabase) -> Resolver {
543 self.module(db).resolver(db) 543 self.lookup(db).container.resolver(db)
544 } 544 }
545} 545}
546 546