498: actually produce missing def kinds r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-11 18:08:00 +00:00
commit 738c958a04
9 changed files with 168 additions and 48 deletions

View File

@ -2,7 +2,7 @@ use std::sync::Arc;
use relative_path::RelativePathBuf;
use ra_db::{CrateId, Cancelable, FileId};
use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode};
use ra_syntax::{ast, TreeArc, SyntaxNode};
use crate::{
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@ -12,6 +12,7 @@ use crate::{
expr::BodySyntaxMapping,
ty::InferenceResult,
adt::VariantData,
code_model_impl::def_id_to_ast,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -40,12 +41,17 @@ impl Crate {
}
}
#[derive(Debug)]
pub enum Def {
Module(Module),
Struct(Struct),
Enum(Enum),
EnumVariant(EnumVariant),
Function(Function),
Const(Const),
Static(Static),
Trait(Trait),
Type(Type),
Item,
}
@ -186,13 +192,7 @@ impl Struct {
&self,
db: &impl HirDatabase,
) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::StructDef::cast(&syntax)
.expect("struct def should point to StructDef node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -219,13 +219,7 @@ impl Enum {
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::EnumDef::cast(&syntax)
.expect("enum def should point to EnumDef node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -259,13 +253,7 @@ impl EnumVariant {
&self,
db: &impl HirDatabase,
) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::EnumVariant::cast(&syntax)
.expect("variant def should point to EnumVariant node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -304,7 +292,7 @@ impl Function {
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
Ok(self.source_impl(db))
Ok(def_id_to_ast(db, self.def_id))
}
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
@ -328,3 +316,66 @@ impl Function {
db.infer(self.def_id)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Const {
pub(crate) def_id: DefId,
}
impl Const {
pub(crate) fn new(def_id: DefId) -> Const {
Const { def_id }
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::ConstDef>)> {
Ok(def_id_to_ast(db, self.def_id))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Static {
pub(crate) def_id: DefId,
}
impl Static {
pub(crate) fn new(def_id: DefId) -> Static {
Static { def_id }
}
pub fn source(
&self,
db: &impl HirDatabase,
) -> Cancelable<(HirFileId, TreeArc<ast::StaticDef>)> {
Ok(def_id_to_ast(db, self.def_id))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Trait {
pub(crate) def_id: DefId,
}
impl Trait {
pub(crate) fn new(def_id: DefId) -> Trait {
Trait { def_id }
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TraitDef>)> {
Ok(def_id_to_ast(db, self.def_id))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Type {
pub(crate) def_id: DefId,
}
impl Type {
pub(crate) fn new(def_id: DefId) -> Type {
Type { def_id }
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TypeDef>)> {
Ok(def_id_to_ast(db, self.def_id))
}
}

View File

@ -1,3 +1,18 @@
mod krate; // `crate` is invalid ident :(
mod module;
pub(crate) mod function;
use ra_syntax::{AstNode, TreeArc};
use crate::{HirDatabase, DefId, HirFileId};
pub(crate) fn def_id_to_ast<N: AstNode>(
db: &impl HirDatabase,
def_id: DefId,
) -> (HirFileId, TreeArc<N>) {
let (file_id, syntax) = def_id.source(db);
let ast = N::cast(&syntax)
.unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax))
.to_owned();
(file_id, ast)
}

View File

@ -3,16 +3,14 @@ mod scope;
use std::sync::Arc;
use ra_db::Cancelable;
use ra_syntax::{
TreeArc,
ast::{self, AstNode, NameOwner},
};
use ra_syntax::{TreeArc, ast::{self, NameOwner}};
use crate::{
DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId,
DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
type_ref::{TypeRef, Mutability},
expr::Body,
impl_block::ImplBlock,
code_model_impl::def_id_to_ast,
};
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
@ -22,16 +20,6 @@ impl Function {
Function { def_id }
}
pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
let def_loc = self.def_id.loc(db);
assert!(def_loc.kind == DefKind::Function);
let syntax = db.file_item(def_loc.source_item_id);
(
def_loc.source_item_id.file_id,
ast::FnDef::cast(&syntax).unwrap().to_owned(),
)
}
pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
db.body_hir(self.def_id)
}
@ -48,8 +36,8 @@ impl Function {
impl FnSignature {
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
let func = Function::new(def_id);
let node = func.source_impl(db).1; // TODO we're using source_impl here to avoid returning Cancelable... this is a bit hacky
// FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
let node: TreeArc<ast::FnDef> = def_id_to_ast(db, def_id).1;
let name = node
.name()
.map(|n| n.as_name())

View File

@ -4,11 +4,10 @@ use ra_arena::{Arena, RawId, impl_arena_id};
use crate::{
HirDatabase, PerNs, Def, Function, Struct, Enum, EnumVariant, ImplBlock, Crate,
Module, Trait, Type, Static, Const,
module_tree::ModuleId,
};
use crate::code_model_api::Module;
/// hir makes heavy use of ids: integer (u32) handlers to various things. You
/// can think of id as a pointer (but without a lifetime) or a file descriptor
/// (but for hir objects).
@ -146,6 +145,10 @@ pub(crate) enum DefKind {
Struct,
Enum,
EnumVariant,
Const,
Static,
Trait,
Type,
Item,
StructCtor,
@ -173,6 +176,23 @@ impl DefId {
}
DefKind::Enum => Def::Enum(Enum::new(self)),
DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)),
DefKind::Const => {
let def = Const::new(self);
Def::Const(def)
}
DefKind::Static => {
let def = Static::new(self);
Def::Static(def)
}
DefKind::Trait => {
let def = Trait::new(self);
Def::Trait(def)
}
DefKind::Type => {
let def = Type::new(self);
Def::Type(def)
}
DefKind::StructCtor => Def::Item,
DefKind::Item => Def::Item,
};
@ -218,10 +238,10 @@ impl DefKind {
SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor),
SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum),
// These define items, but don't have their own DefKinds yet:
SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Item),
SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Item),
SyntaxKind::CONST_DEF => PerNs::values(DefKind::Item),
SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Item),
SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const),
SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static),
_ => PerNs::none(),
}
}

View File

@ -60,4 +60,6 @@ pub use self::code_model_api::{
Module, ModuleSource, Problem,
Struct, Enum, EnumVariant,
Function, FnSignature, ScopeEntryWithSyntax,
Static, Const,
Trait, Type,
};

View File

@ -470,8 +470,12 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable<T
Def::Struct(s) => type_for_struct(db, s),
Def::Enum(e) => type_for_enum(db, e),
Def::EnumVariant(ev) => type_for_enum_variant(db, ev),
Def::Item => {
log::debug!("trying to get type for item of unknown type {:?}", def_id);
_ => {
log::debug!(
"trying to get type for item of unknown type {:?} {:?}",
def_id,
def
);
Ok(Ty::Unknown)
}
}

View File

@ -33,6 +33,10 @@ pub enum CompletionItemKind {
EnumVariant,
Binding,
Field,
Static,
Const,
Trait,
TypeAlias,
}
#[derive(Debug, PartialEq, Eq)]
@ -153,6 +157,22 @@ impl Builder {
types: Some(hir::Def::Enum(..)),
..
} => CompletionItemKind::Enum,
PerNs {
types: Some(hir::Def::Trait(..)),
..
} => CompletionItemKind::Trait,
PerNs {
types: Some(hir::Def::Type(..)),
..
} => CompletionItemKind::TypeAlias,
PerNs {
values: Some(hir::Def::Const(..)),
..
} => CompletionItemKind::Const,
PerNs {
values: Some(hir::Def::Static(..)),
..
} => CompletionItemKind::Static,
PerNs {
values: Some(hir::Def::Function(function)),
..

View File

@ -108,6 +108,22 @@ impl NavigationTarget {
let (file_id, node) = f.source(db)?;
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Trait(f) => {
let (file_id, node) = f.source(db)?;
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Type(f) => {
let (file_id, node) = f.source(db)?;
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Static(f) => {
let (file_id, node) = f.source(db)?;
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Const(f) => {
let (file_id, node) = f.source(db)?;
NavigationTarget::from_named(file_id.original_file(db), &*node)
}
Def::Module(m) => NavigationTarget::from_module(db, m)?,
Def::Item => return Ok(None),
};

View File

@ -65,6 +65,10 @@ impl Conv for CompletionItemKind {
CompletionItemKind::EnumVariant => EnumMember,
CompletionItemKind::Binding => Variable,
CompletionItemKind::Field => Field,
CompletionItemKind::Trait => Interface,
CompletionItemKind::TypeAlias => Struct,
CompletionItemKind::Const => Constant,
CompletionItemKind::Static => Value,
}
}
}