mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 19:12:50 +00:00
move consts&statics to new id
This commit is contained in:
parent
f588535273
commit
1db2cbcb8b
@ -16,7 +16,7 @@ use crate::{
|
|||||||
code_model_impl::def_id_to_ast,
|
code_model_impl::def_id_to_ast,
|
||||||
docs::{Documentation, Docs, docs_from_ast},
|
docs::{Documentation, Docs, docs_from_ast},
|
||||||
module_tree::ModuleId,
|
module_tree::ModuleId,
|
||||||
ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef},
|
ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main interface with which
|
/// hir::Crate describes a single crate. It's the main interface with which
|
||||||
@ -47,8 +47,6 @@ impl Crate {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Def {
|
pub enum Def {
|
||||||
Const(Const),
|
|
||||||
Static(Static),
|
|
||||||
Trait(Trait),
|
Trait(Trait),
|
||||||
Type(Type),
|
Type(Type),
|
||||||
Item,
|
Item,
|
||||||
@ -67,11 +65,21 @@ pub enum ModuleDef {
|
|||||||
Function(Function),
|
Function(Function),
|
||||||
Struct(Struct),
|
Struct(Struct),
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
// Can't be directly declared, but can be imported.
|
|
||||||
EnumVariant(EnumVariant),
|
EnumVariant(EnumVariant),
|
||||||
|
Const(Const),
|
||||||
|
Static(Static),
|
||||||
|
// Can't be directly declared, but can be imported.
|
||||||
Def(DefId),
|
Def(DefId),
|
||||||
}
|
}
|
||||||
impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant);
|
impl_froms!(
|
||||||
|
ModuleDef: Module,
|
||||||
|
Function,
|
||||||
|
Struct,
|
||||||
|
Enum,
|
||||||
|
EnumVariant,
|
||||||
|
Const,
|
||||||
|
Static
|
||||||
|
);
|
||||||
|
|
||||||
impl From<DefId> for ModuleDef {
|
impl From<DefId> for ModuleDef {
|
||||||
fn from(it: DefId) -> ModuleDef {
|
fn from(it: DefId) -> ModuleDef {
|
||||||
@ -386,18 +394,14 @@ impl Docs for Function {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Const {
|
pub struct Const {
|
||||||
pub(crate) def_id: DefId,
|
pub(crate) id: ConstId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Const {
|
impl Const {
|
||||||
pub(crate) fn new(def_id: DefId) -> Const {
|
|
||||||
Const { def_id }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
|
||||||
def_id_to_ast(db, self.def_id)
|
self.id.source(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -407,18 +411,14 @@ impl Docs for Const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Static {
|
pub struct Static {
|
||||||
pub(crate) def_id: DefId,
|
pub(crate) id: StaticId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Static {
|
impl Static {
|
||||||
pub(crate) fn new(def_id: DefId) -> Static {
|
|
||||||
Static { def_id }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
|
||||||
def_id_to_ast(db, self.def_id)
|
self.id.source(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +147,11 @@ impl Module {
|
|||||||
None => PerNs::none(),
|
None => PerNs::none(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ModuleDef::Function(_) | ModuleDef::Struct(_) | ModuleDef::EnumVariant(_) => {
|
ModuleDef::Function(_)
|
||||||
|
| ModuleDef::Struct(_)
|
||||||
|
| ModuleDef::Const(_)
|
||||||
|
| ModuleDef::Static(_)
|
||||||
|
| ModuleDef::EnumVariant(_) => {
|
||||||
// could be an inherent method call in UFCS form
|
// could be an inherent method call in UFCS form
|
||||||
// (`Struct::method`), or some other kind of associated
|
// (`Struct::method`), or some other kind of associated
|
||||||
// item... Which we currently don't handle (TODO)
|
// item... Which we currently don't handle (TODO)
|
||||||
|
@ -9,7 +9,7 @@ use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Def,
|
HirDatabase, Def,
|
||||||
Module, Trait, Type, Static, Const,
|
Module, Trait, Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@ -20,6 +20,8 @@ pub struct HirInterner {
|
|||||||
structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
|
structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>,
|
||||||
enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
|
enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>,
|
||||||
enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
|
enum_variants: LocationIntener<ItemLoc<ast::EnumVariant>, EnumVariantId>,
|
||||||
|
consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>,
|
||||||
|
statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HirInterner {
|
impl HirInterner {
|
||||||
@ -246,6 +248,24 @@ impl AstItemDef<ast::EnumVariant> for EnumVariantId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct ConstId(RawId);
|
||||||
|
impl_arena_id!(ConstId);
|
||||||
|
impl AstItemDef<ast::ConstDef> for ConstId {
|
||||||
|
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::ConstDef>, Self> {
|
||||||
|
&interner.consts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct StaticId(RawId);
|
||||||
|
impl_arena_id!(StaticId);
|
||||||
|
impl AstItemDef<ast::StaticDef> for StaticId {
|
||||||
|
fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StaticDef>, Self> {
|
||||||
|
&interner.statics
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
|
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
|
||||||
/// in a specific module.
|
/// in a specific module.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
@ -261,8 +281,6 @@ pub struct DefLoc {
|
|||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub(crate) enum DefKind {
|
pub(crate) enum DefKind {
|
||||||
Const,
|
|
||||||
Static,
|
|
||||||
Trait,
|
Trait,
|
||||||
Type,
|
Type,
|
||||||
Item,
|
Item,
|
||||||
@ -286,14 +304,6 @@ impl DefId {
|
|||||||
pub fn resolve(self, db: &impl HirDatabase) -> Def {
|
pub fn resolve(self, db: &impl HirDatabase) -> Def {
|
||||||
let loc = self.loc(db);
|
let loc = self.loc(db);
|
||||||
match loc.kind {
|
match loc.kind {
|
||||||
DefKind::Const => {
|
|
||||||
let def = Const::new(self);
|
|
||||||
Def::Const(def)
|
|
||||||
}
|
|
||||||
DefKind::Static => {
|
|
||||||
let def = Static::new(self);
|
|
||||||
Def::Static(def)
|
|
||||||
}
|
|
||||||
DefKind::Trait => {
|
DefKind::Trait => {
|
||||||
let def = Trait::new(self);
|
let def = Trait::new(self);
|
||||||
Def::Trait(def)
|
Def::Trait(def)
|
||||||
|
@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
|
|||||||
use crate::{
|
use crate::{
|
||||||
SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
|
SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
|
||||||
HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
|
HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
|
||||||
ModuleDef, Module, Struct, Enum,
|
ModuleDef, Module, Struct, Enum, Const, Static,
|
||||||
ids::LocationCtx,
|
ids::LocationCtx,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -187,8 +187,22 @@ impl LoweredModule {
|
|||||||
// TODO
|
// TODO
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::ConstDef(it) => it.name(),
|
ast::ModuleItemKind::ConstDef(it) => {
|
||||||
ast::ModuleItemKind::StaticDef(it) => it.name(),
|
if let Some(name) = it.name() {
|
||||||
|
let c = Const { id: ctx.to_def(it) };
|
||||||
|
self.declarations
|
||||||
|
.insert(name.as_name(), PerNs::values(c.into()));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ast::ModuleItemKind::StaticDef(it) => {
|
||||||
|
if let Some(name) = it.name() {
|
||||||
|
let s = Static { id: ctx.to_def(it) };
|
||||||
|
self.declarations
|
||||||
|
.insert(name.as_name(), PerNs::values(s.into()));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
ast::ModuleItemKind::Module(_) => {
|
ast::ModuleItemKind::Module(_) => {
|
||||||
// modules are handled separately direclty by nameres
|
// modules are handled separately direclty by nameres
|
||||||
return;
|
return;
|
||||||
@ -246,8 +260,8 @@ impl DefKind {
|
|||||||
SyntaxKind::ENUM_DEF => unreachable!(),
|
SyntaxKind::ENUM_DEF => unreachable!(),
|
||||||
SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
|
SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
|
||||||
SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
|
SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
|
||||||
SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const),
|
SyntaxKind::CONST_DEF => unreachable!(),
|
||||||
SyntaxKind::STATIC_DEF => PerNs::values(DefKind::Static),
|
SyntaxKind::STATIC_DEF => unreachable!(),
|
||||||
_ => PerNs::none(),
|
_ => PerNs::none(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -693,7 +693,10 @@ impl From<ModuleDef> for Option<TypableDef> {
|
|||||||
ModuleDef::Struct(s) => s.into(),
|
ModuleDef::Struct(s) => s.into(),
|
||||||
ModuleDef::Enum(e) => e.into(),
|
ModuleDef::Enum(e) => e.into(),
|
||||||
ModuleDef::EnumVariant(v) => v.into(),
|
ModuleDef::EnumVariant(v) => v.into(),
|
||||||
ModuleDef::Def(_) | ModuleDef::Module(_) => return None,
|
ModuleDef::Const(_)
|
||||||
|
| ModuleDef::Static(_)
|
||||||
|
| ModuleDef::Def(_)
|
||||||
|
| ModuleDef::Module(_) => return None,
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
|||||||
hir::ModuleDef::Function(_)
|
hir::ModuleDef::Function(_)
|
||||||
| hir::ModuleDef::Struct(_)
|
| hir::ModuleDef::Struct(_)
|
||||||
| hir::ModuleDef::Def(_)
|
| hir::ModuleDef::Def(_)
|
||||||
|
| hir::ModuleDef::Const(_)
|
||||||
|
| hir::ModuleDef::Static(_)
|
||||||
| hir::ModuleDef::EnumVariant(_) => return,
|
| hir::ModuleDef::EnumVariant(_) => return,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -223,11 +223,11 @@ impl Builder {
|
|||||||
hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
||||||
hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
|
hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
|
||||||
hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
|
hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
|
||||||
|
hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
||||||
|
hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
||||||
hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) {
|
hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) {
|
||||||
hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
|
hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
|
||||||
hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
|
hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
|
||||||
hir::Def::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
|
||||||
hir::Def::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
|
||||||
_ => return self,
|
_ => return self,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -121,6 +121,20 @@ impl NavigationTarget {
|
|||||||
&*node,
|
&*node,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
hir::ModuleDef::Const(s) => {
|
||||||
|
let (file_id, node) = s.source(db);
|
||||||
|
return Some(NavigationTarget::from_named(
|
||||||
|
file_id.original_file(db),
|
||||||
|
&*node,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
hir::ModuleDef::Static(s) => {
|
||||||
|
let (file_id, node) = s.source(db);
|
||||||
|
return Some(NavigationTarget::from_named(
|
||||||
|
file_id.original_file(db),
|
||||||
|
&*node,
|
||||||
|
));
|
||||||
|
}
|
||||||
hir::ModuleDef::Enum(e) => {
|
hir::ModuleDef::Enum(e) => {
|
||||||
let (file_id, node) = e.source(db);
|
let (file_id, node) = e.source(db);
|
||||||
return Some(NavigationTarget::from_named(
|
return Some(NavigationTarget::from_named(
|
||||||
@ -146,14 +160,6 @@ impl NavigationTarget {
|
|||||||
let (file_id, node) = f.source(db);
|
let (file_id, node) = f.source(db);
|
||||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
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::Item => return None,
|
Def::Item => return None,
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
|
Loading…
Reference in New Issue
Block a user