aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model_api.rs38
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs6
-rw-r--r--crates/ra_hir/src/ids.rs32
-rw-r--r--crates/ra_hir/src/nameres/lower.rs24
-rw-r--r--crates/ra_hir/src/ty.rs5
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs2
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs4
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs22
8 files changed, 86 insertions, 47 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 1fa591ea4..d82dda79a 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -16,7 +16,7 @@ use crate::{
16 code_model_impl::def_id_to_ast, 16 code_model_impl::def_id_to_ast,
17 docs::{Documentation, Docs, docs_from_ast}, 17 docs::{Documentation, Docs, docs_from_ast},
18 module_tree::ModuleId, 18 module_tree::ModuleId,
19 ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef}, 19 ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId},
20}; 20};
21 21
22/// hir::Crate describes a single crate. It's the main interface with which 22/// hir::Crate describes a single crate. It's the main interface with which
@@ -47,8 +47,6 @@ impl Crate {
47 47
48#[derive(Debug)] 48#[derive(Debug)]
49pub enum Def { 49pub enum Def {
50 Const(Const),
51 Static(Static),
52 Trait(Trait), 50 Trait(Trait),
53 Type(Type), 51 Type(Type),
54 Item, 52 Item,
@@ -67,11 +65,21 @@ pub enum ModuleDef {
67 Function(Function), 65 Function(Function),
68 Struct(Struct), 66 Struct(Struct),
69 Enum(Enum), 67 Enum(Enum),
70 // Can't be directly declared, but can be imported.
71 EnumVariant(EnumVariant), 68 EnumVariant(EnumVariant),
69 Const(Const),
70 Static(Static),
71 // Can't be directly declared, but can be imported.
72 Def(DefId), 72 Def(DefId),
73} 73}
74impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant); 74impl_froms!(
75 ModuleDef: Module,
76 Function,
77 Struct,
78 Enum,
79 EnumVariant,
80 Const,
81 Static
82);
75 83
76impl From<DefId> for ModuleDef { 84impl From<DefId> for ModuleDef {
77 fn from(it: DefId) -> ModuleDef { 85 fn from(it: DefId) -> ModuleDef {
@@ -386,18 +394,14 @@ impl Docs for Function {
386 } 394 }
387} 395}
388 396
389#[derive(Debug, Clone, PartialEq, Eq, Hash)] 397#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
390pub struct Const { 398pub struct Const {
391 pub(crate) def_id: DefId, 399 pub(crate) id: ConstId,
392} 400}
393 401
394impl Const { 402impl Const {
395 pub(crate) fn new(def_id: DefId) -> Const {
396 Const { def_id }
397 }
398
399 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) { 403 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
400 def_id_to_ast(db, self.def_id) 404 self.id.source(db)
401 } 405 }
402} 406}
403 407
@@ -407,18 +411,14 @@ impl Docs for Const {
407 } 411 }
408} 412}
409 413
410#[derive(Debug, Clone, PartialEq, Eq, Hash)] 414#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
411pub struct Static { 415pub struct Static {
412 pub(crate) def_id: DefId, 416 pub(crate) id: StaticId,
413} 417}
414 418
415impl Static { 419impl Static {
416 pub(crate) fn new(def_id: DefId) -> Static {
417 Static { def_id }
418 }
419
420 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) { 420 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
421 def_id_to_ast(db, self.def_id) 421 self.id.source(db)
422 } 422 }
423} 423}
424 424
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index aa5e5d689..1518825c7 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -147,7 +147,11 @@ impl Module {
147 None => PerNs::none(), 147 None => PerNs::none(),
148 } 148 }
149 } 149 }
150 ModuleDef::Function(_) | ModuleDef::Struct(_) | ModuleDef::EnumVariant(_) => { 150 ModuleDef::Function(_)
151 | ModuleDef::Struct(_)
152 | ModuleDef::Const(_)
153 | ModuleDef::Static(_)
154 | ModuleDef::EnumVariant(_) => {
151 // could be an inherent method call in UFCS form 155 // could be an inherent method call in UFCS form
152 // (`Struct::method`), or some other kind of associated 156 // (`Struct::method`), or some other kind of associated
153 // item... Which we currently don't handle (TODO) 157 // item... Which we currently don't handle (TODO)
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 4b4e07e58..9aae58bb6 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -9,7 +9,7 @@ use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
9 9
10use crate::{ 10use crate::{
11 HirDatabase, Def, 11 HirDatabase, Def,
12 Module, Trait, Type, Static, Const, 12 Module, Trait, Type,
13}; 13};
14 14
15#[derive(Debug, Default)] 15#[derive(Debug, Default)]
@@ -20,6 +20,8 @@ pub struct HirInterner {
20 structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, 20 structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
21 enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, 21 enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
22 enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>, 22 enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
23 consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>,
24 statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>,
23} 25}
24 26
25impl HirInterner { 27impl HirInterner {
@@ -246,6 +248,24 @@ impl AstItemDef<ast::EnumVariant> for EnumVariantId {
246 } 248 }
247} 249}
248 250
251#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
252pub struct ConstId(RawId);
253impl_arena_id!(ConstId);
254impl AstItemDef<ast::ConstDef> for ConstId {
255 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::ConstDef>, Self> {
256 &interner.consts
257 }
258}
259
260#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
261pub struct StaticId(RawId);
262impl_arena_id!(StaticId);
263impl AstItemDef<ast::StaticDef> for StaticId {
264 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StaticDef>, Self> {
265 &interner.statics
266 }
267}
268
249/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) 269/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
250/// in a specific module. 270/// in a specific module.
251#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 271#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -261,8 +281,6 @@ pub struct DefLoc {
261 281
262#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 282#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
263pub(crate) enum DefKind { 283pub(crate) enum DefKind {
264 Const,
265 Static,
266 Trait, 284 Trait,
267 Type, 285 Type,
268 Item, 286 Item,
@@ -286,14 +304,6 @@ impl DefId {
286 pub fn resolve(self, db: &impl HirDatabase) -> Def { 304 pub fn resolve(self, db: &impl HirDatabase) -> Def {
287 let loc = self.loc(db); 305 let loc = self.loc(db);
288 match loc.kind { 306 match loc.kind {
289 DefKind::Const => {
290 let def = Const::new(self);
291 Def::Const(def)
292 }
293 DefKind::Static => {
294 let def = Static::new(self);
295 Def::Static(def)
296 }
297 DefKind::Trait => { 307 DefKind::Trait => {
298 let def = Trait::new(self); 308 let def = Trait::new(self);
299 Def::Trait(def) 309 Def::Trait(def)
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs
index 0056bdd5a..6a86e5fd4 100644
--- a/crates/ra_hir/src/nameres/lower.rs
+++ b/crates/ra_hir/src/nameres/lower.rs
@@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
10use crate::{ 10use crate::{
11 SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, 11 SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
12 HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, 12 HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
13 ModuleDef, Module, Struct, Enum, 13 ModuleDef, Module, Struct, Enum, Const, Static,
14 ids::LocationCtx, 14 ids::LocationCtx,
15}; 15};
16 16
@@ -187,8 +187,22 @@ impl LoweredModule {
187 // TODO 187 // TODO
188 return; 188 return;
189 } 189 }
190 ast::ModuleItemKind::ConstDef(it) => it.name(), 190 ast::ModuleItemKind::ConstDef(it) => {
191 ast::ModuleItemKind::StaticDef(it) => it.name(), 191 if let Some(name) = it.name() {
192 let c = Const { id: ctx.to_def(it) };
193 self.declarations
194 .insert(name.as_name(), PerNs::values(c.into()));
195 }
196 return;
197 }
198 ast::ModuleItemKind::StaticDef(it) => {
199 if let Some(name) = it.name() {
200 let s = Static { id: ctx.to_def(it) };
201 self.declarations
202 .insert(name.as_name(), PerNs::values(s.into()));
203 }
204 return;
205 }
192 ast::ModuleItemKind::Module(_) => { 206 ast::ModuleItemKind::Module(_) => {
193 // modules are handled separately direclty by nameres 207 // modules are handled separately direclty by nameres
194 return; 208 return;
@@ -246,8 +260,8 @@ impl DefKind {
246 SyntaxKind::ENUM_DEF => unreachable!(), 260 SyntaxKind::ENUM_DEF => unreachable!(),
247 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), 261 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
248 SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), 262 SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
249 SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const), 263 SyntaxKind::CONST_DEF => unreachable!(),
250 SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static), 264 SyntaxKind::STATIC_DEF => unreachable!(),
251 _ => PerNs::none(), 265 _ => PerNs::none(),
252 } 266 }
253 } 267 }
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 9a1a90eed..6d6150096 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -693,7 +693,10 @@ impl From<ModuleDef> for Option<TypableDef> {
693 ModuleDef::Struct(s) => s.into(), 693 ModuleDef::Struct(s) => s.into(),
694 ModuleDef::Enum(e) => e.into(), 694 ModuleDef::Enum(e) => e.into(),
695 ModuleDef::EnumVariant(v) => v.into(), 695 ModuleDef::EnumVariant(v) => v.into(),
696 ModuleDef::Def(_) | ModuleDef::Module(_) => return None, 696 ModuleDef::Const(_)
697 | ModuleDef::Static(_)
698 | ModuleDef::Def(_)
699 | ModuleDef::Module(_) => return None,
697 }; 700 };
698 Some(res) 701 Some(res)
699 } 702 }
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 4768efdc1..bd5484516 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -43,6 +43,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
43 hir::ModuleDef::Function(_) 43 hir::ModuleDef::Function(_)
44 | hir::ModuleDef::Struct(_) 44 | hir::ModuleDef::Struct(_)
45 | hir::ModuleDef::Def(_) 45 | hir::ModuleDef::Def(_)
46 | hir::ModuleDef::Const(_)
47 | hir::ModuleDef::Static(_)
46 | hir::ModuleDef::EnumVariant(_) => return, 48 | hir::ModuleDef::EnumVariant(_) => return,
47 }; 49 };
48} 50}
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index ee1f8bce0..2f8ec41d2 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -223,11 +223,11 @@ impl Builder {
223 hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), 223 hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
224 hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), 224 hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
225 hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), 225 hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
226 hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
227 hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
226 hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { 228 hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) {
227 hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)), 229 hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
228 hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), 230 hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
229 hir::Def::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
230 hir::Def::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
231 _ => return self, 231 _ => return self,
232 }, 232 },
233 }; 233 };
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index fc76338d2..8406fee31 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -121,6 +121,20 @@ impl NavigationTarget {
121 &*node, 121 &*node,
122 )); 122 ));
123 } 123 }
124 hir::ModuleDef::Const(s) => {
125 let (file_id, node) = s.source(db);
126 return Some(NavigationTarget::from_named(
127 file_id.original_file(db),
128 &*node,
129 ));
130 }
131 hir::ModuleDef::Static(s) => {
132 let (file_id, node) = s.source(db);
133 return Some(NavigationTarget::from_named(
134 file_id.original_file(db),
135 &*node,
136 ));
137 }
124 hir::ModuleDef::Enum(e) => { 138 hir::ModuleDef::Enum(e) => {
125 let (file_id, node) = e.source(db); 139 let (file_id, node) = e.source(db);
126 return Some(NavigationTarget::from_named( 140 return Some(NavigationTarget::from_named(
@@ -146,14 +160,6 @@ impl NavigationTarget {
146 let (file_id, node) = f.source(db); 160 let (file_id, node) = f.source(db);
147 NavigationTarget::from_named(file_id.original_file(db), &*node) 161 NavigationTarget::from_named(file_id.original_file(db), &*node)
148 } 162 }
149 Def::Static(f) => {
150 let (file_id, node) = f.source(db);
151 NavigationTarget::from_named(file_id.original_file(db), &*node)
152 }
153 Def::Const(f) => {
154 let (file_id, node) = f.source(db);
155 NavigationTarget::from_named(file_id.original_file(db), &*node)
156 }
157 Def::Item => return None, 163 Def::Item => return None,
158 }; 164 };
159 Some(res) 165 Some(res)