aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-11 18:08:00 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-11 18:08:00 +0000
commit738c958a044361dc84a0f27e57b40f66a5815990 (patch)
treec57734dc82350c2d853f557a40f7aafdc7470bdf /crates/ra_hir
parent2d3940d0ab862dbfaed4f4c844faaca6a38e31e9 (diff)
parentf193fbcbae0783953cfaa88aaec6a8d4e1255007 (diff)
Merge #498
498: actually produce missing def kinds r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model_api.rs97
-rw-r--r--crates/ra_hir/src/code_model_impl.rs15
-rw-r--r--crates/ra_hir/src/code_model_impl/function.rs22
-rw-r--r--crates/ra_hir/src/ids.rs32
-rw-r--r--crates/ra_hir/src/lib.rs2
-rw-r--r--crates/ra_hir/src/ty.rs8
6 files changed, 128 insertions, 48 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 098c7f40b..d4244f70c 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, Cancelable, FileId}; 4use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode}; 5use ra_syntax::{ast, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@@ -12,6 +12,7 @@ use crate::{
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::InferenceResult,
14 adt::VariantData, 14 adt::VariantData,
15 code_model_impl::def_id_to_ast,
15}; 16};
16 17
17/// hir::Crate describes a single crate. It's the main interface with which 18/// hir::Crate describes a single crate. It's the main interface with which
@@ -40,12 +41,17 @@ impl Crate {
40 } 41 }
41} 42}
42 43
44#[derive(Debug)]
43pub enum Def { 45pub enum Def {
44 Module(Module), 46 Module(Module),
45 Struct(Struct), 47 Struct(Struct),
46 Enum(Enum), 48 Enum(Enum),
47 EnumVariant(EnumVariant), 49 EnumVariant(EnumVariant),
48 Function(Function), 50 Function(Function),
51 Const(Const),
52 Static(Static),
53 Trait(Trait),
54 Type(Type),
49 Item, 55 Item,
50} 56}
51 57
@@ -186,13 +192,7 @@ impl Struct {
186 &self, 192 &self,
187 db: &impl HirDatabase, 193 db: &impl HirDatabase,
188 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> { 194 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
189 let (file_id, syntax) = self.def_id.source(db); 195 Ok(def_id_to_ast(db, self.def_id))
190 Ok((
191 file_id,
192 ast::StructDef::cast(&syntax)
193 .expect("struct def should point to StructDef node")
194 .to_owned(),
195 ))
196 } 196 }
197} 197}
198 198
@@ -219,13 +219,7 @@ impl Enum {
219 } 219 }
220 220
221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> { 221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
222 let (file_id, syntax) = self.def_id.source(db); 222 Ok(def_id_to_ast(db, self.def_id))
223 Ok((
224 file_id,
225 ast::EnumDef::cast(&syntax)
226 .expect("enum def should point to EnumDef node")
227 .to_owned(),
228 ))
229 } 223 }
230} 224}
231 225
@@ -259,13 +253,7 @@ impl EnumVariant {
259 &self, 253 &self,
260 db: &impl HirDatabase, 254 db: &impl HirDatabase,
261 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> { 255 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
262 let (file_id, syntax) = self.def_id.source(db); 256 Ok(def_id_to_ast(db, self.def_id))
263 Ok((
264 file_id,
265 ast::EnumVariant::cast(&syntax)
266 .expect("variant def should point to EnumVariant node")
267 .to_owned(),
268 ))
269 } 257 }
270} 258}
271 259
@@ -304,7 +292,7 @@ impl Function {
304 } 292 }
305 293
306 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 294 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
307 Ok(self.source_impl(db)) 295 Ok(def_id_to_ast(db, self.def_id))
308 } 296 }
309 297
310 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 298 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
@@ -328,3 +316,66 @@ impl Function {
328 db.infer(self.def_id) 316 db.infer(self.def_id)
329 } 317 }
330} 318}
319
320#[derive(Debug, Clone, PartialEq, Eq, Hash)]
321pub struct Const {
322 pub(crate) def_id: DefId,
323}
324
325impl Const {
326 pub(crate) fn new(def_id: DefId) -> Const {
327 Const { def_id }
328 }
329
330 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::ConstDef>)> {
331 Ok(def_id_to_ast(db, self.def_id))
332 }
333}
334
335#[derive(Debug, Clone, PartialEq, Eq, Hash)]
336pub struct Static {
337 pub(crate) def_id: DefId,
338}
339
340impl Static {
341 pub(crate) fn new(def_id: DefId) -> Static {
342 Static { def_id }
343 }
344
345 pub fn source(
346 &self,
347 db: &impl HirDatabase,
348 ) -> Cancelable<(HirFileId, TreeArc<ast::StaticDef>)> {
349 Ok(def_id_to_ast(db, self.def_id))
350 }
351}
352
353#[derive(Debug, Clone, PartialEq, Eq, Hash)]
354pub struct Trait {
355 pub(crate) def_id: DefId,
356}
357
358impl Trait {
359 pub(crate) fn new(def_id: DefId) -> Trait {
360 Trait { def_id }
361 }
362
363 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TraitDef>)> {
364 Ok(def_id_to_ast(db, self.def_id))
365 }
366}
367
368#[derive(Debug, Clone, PartialEq, Eq, Hash)]
369pub struct Type {
370 pub(crate) def_id: DefId,
371}
372
373impl Type {
374 pub(crate) fn new(def_id: DefId) -> Type {
375 Type { def_id }
376 }
377
378 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TypeDef>)> {
379 Ok(def_id_to_ast(db, self.def_id))
380 }
381}
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
index 1f28fab74..0cea9f7b6 100644
--- a/crates/ra_hir/src/code_model_impl.rs
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -1,3 +1,18 @@
1mod krate; // `crate` is invalid ident :( 1mod krate; // `crate` is invalid ident :(
2mod module; 2mod module;
3pub(crate) mod function; 3pub(crate) mod function;
4
5use ra_syntax::{AstNode, TreeArc};
6
7use crate::{HirDatabase, DefId, HirFileId};
8
9pub(crate) fn def_id_to_ast<N: AstNode>(
10 db: &impl HirDatabase,
11 def_id: DefId,
12) -> (HirFileId, TreeArc<N>) {
13 let (file_id, syntax) = def_id.source(db);
14 let ast = N::cast(&syntax)
15 .unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax))
16 .to_owned();
17 (file_id, ast)
18}
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs
index daf49e791..1ce939e05 100644
--- a/crates/ra_hir/src/code_model_impl/function.rs
+++ b/crates/ra_hir/src/code_model_impl/function.rs
@@ -3,16 +3,14 @@ mod scope;
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_db::Cancelable; 5use ra_db::Cancelable;
6use ra_syntax::{ 6use ra_syntax::{TreeArc, ast::{self, NameOwner}};
7 TreeArc,
8 ast::{self, AstNode, NameOwner},
9};
10 7
11use crate::{ 8use crate::{
12 DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId, 9 DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
13 type_ref::{TypeRef, Mutability}, 10 type_ref::{TypeRef, Mutability},
14 expr::Body, 11 expr::Body,
15 impl_block::ImplBlock, 12 impl_block::ImplBlock,
13 code_model_impl::def_id_to_ast,
16}; 14};
17 15
18pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; 16pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
@@ -22,16 +20,6 @@ impl Function {
22 Function { def_id } 20 Function { def_id }
23 } 21 }
24 22
25 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
26 let def_loc = self.def_id.loc(db);
27 assert!(def_loc.kind == DefKind::Function);
28 let syntax = db.file_item(def_loc.source_item_id);
29 (
30 def_loc.source_item_id.file_id,
31 ast::FnDef::cast(&syntax).unwrap().to_owned(),
32 )
33 }
34
35 pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> { 23 pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
36 db.body_hir(self.def_id) 24 db.body_hir(self.def_id)
37 } 25 }
@@ -48,8 +36,8 @@ impl Function {
48 36
49impl FnSignature { 37impl FnSignature {
50 pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> { 38 pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
51 let func = Function::new(def_id); 39 // FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
52 let node = func.source_impl(db).1; // TODO we're using source_impl here to avoid returning Cancelable... this is a bit hacky 40 let node: TreeArc<ast::FnDef> = def_id_to_ast(db, def_id).1;
53 let name = node 41 let name = node
54 .name() 42 .name()
55 .map(|n| n.as_name()) 43 .map(|n| n.as_name())
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 0805fd3db..316896dce 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -4,11 +4,10 @@ use ra_arena::{Arena, RawId, impl_arena_id};
4 4
5use crate::{ 5use crate::{
6 HirDatabase, PerNs, Def, Function, Struct, Enum, EnumVariant, ImplBlock, Crate, 6 HirDatabase, PerNs, Def, Function, Struct, Enum, EnumVariant, ImplBlock, Crate,
7 Module, Trait, Type, Static, Const,
7 module_tree::ModuleId, 8 module_tree::ModuleId,
8}; 9};
9 10
10use crate::code_model_api::Module;
11
12/// hir makes heavy use of ids: integer (u32) handlers to various things. You 11/// hir makes heavy use of ids: integer (u32) handlers to various things. You
13/// can think of id as a pointer (but without a lifetime) or a file descriptor 12/// can think of id as a pointer (but without a lifetime) or a file descriptor
14/// (but for hir objects). 13/// (but for hir objects).
@@ -146,6 +145,10 @@ pub(crate) enum DefKind {
146 Struct, 145 Struct,
147 Enum, 146 Enum,
148 EnumVariant, 147 EnumVariant,
148 Const,
149 Static,
150 Trait,
151 Type,
149 Item, 152 Item,
150 153
151 StructCtor, 154 StructCtor,
@@ -173,6 +176,23 @@ impl DefId {
173 } 176 }
174 DefKind::Enum => Def::Enum(Enum::new(self)), 177 DefKind::Enum => Def::Enum(Enum::new(self)),
175 DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)), 178 DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)),
179 DefKind::Const => {
180 let def = Const::new(self);
181 Def::Const(def)
182 }
183 DefKind::Static => {
184 let def = Static::new(self);
185 Def::Static(def)
186 }
187 DefKind::Trait => {
188 let def = Trait::new(self);
189 Def::Trait(def)
190 }
191 DefKind::Type => {
192 let def = Type::new(self);
193 Def::Type(def)
194 }
195
176 DefKind::StructCtor => Def::Item, 196 DefKind::StructCtor => Def::Item,
177 DefKind::Item => Def::Item, 197 DefKind::Item => Def::Item,
178 }; 198 };
@@ -218,10 +238,10 @@ impl DefKind {
218 SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), 238 SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor),
219 SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), 239 SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum),
220 // These define items, but don't have their own DefKinds yet: 240 // These define items, but don't have their own DefKinds yet:
221 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Item), 241 SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
222 SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Item), 242 SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
223 SyntaxKind::CONST_DEF => PerNs::values(DefKind::Item), 243 SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const),
224 SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Item), 244 SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static),
225 _ => PerNs::none(), 245 _ => PerNs::none(),
226 } 246 }
227 } 247 }
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index fe8be5700..45dda4f7f 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -60,4 +60,6 @@ pub use self::code_model_api::{
60 Module, ModuleSource, Problem, 60 Module, ModuleSource, Problem,
61 Struct, Enum, EnumVariant, 61 Struct, Enum, EnumVariant,
62 Function, FnSignature, ScopeEntryWithSyntax, 62 Function, FnSignature, ScopeEntryWithSyntax,
63 Static, Const,
64 Trait, Type,
63}; 65};
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 18c41a015..0c24a0652 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -470,8 +470,12 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T
470 Def::Struct(s) => type_for_struct(db, s), 470 Def::Struct(s) => type_for_struct(db, s),
471 Def::Enum(e) => type_for_enum(db, e), 471 Def::Enum(e) => type_for_enum(db, e),
472 Def::EnumVariant(ev) => type_for_enum_variant(db, ev), 472 Def::EnumVariant(ev) => type_for_enum_variant(db, ev),
473 Def::Item => { 473 _ => {
474 log::debug!("trying to get type for item of unknown type {:?}", def_id); 474 log::debug!(
475 "trying to get type for item of unknown type {:?} {:?}",
476 def_id,
477 def
478 );
475 Ok(Ty::Unknown) 479 Ok(Ty::Unknown)
476 } 480 }
477 } 481 }