Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/lib.rs')
-rw-r--r--crates/hir-def/src/lib.rs189
1 files changed, 54 insertions, 135 deletions
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index b5333861cc..22ba3aab4e 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -63,7 +63,7 @@ use std::{
panic::{RefUnwindSafe, UnwindSafe},
};
-use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId, ProcMacroKind};
+use base_db::{impl_intern_key, salsa, CrateId, Edition};
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
attrs::{Attr, AttrId, AttrInput},
@@ -72,24 +72,27 @@ use hir_expand::{
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
eager::expand_eager_macro_input,
+ impl_intern_lookup,
name::Name,
- proc_macro::ProcMacroExpander,
+ proc_macro::{CustomProcMacroExpander, ProcMacroKind},
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
MacroDefId, MacroDefKind,
};
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
+use span::Span;
use stdx::impl_from;
use syntax::{ast, AstNode};
-pub use hir_expand::tt;
+pub use hir_expand::{tt, Intern, Lookup};
use crate::{
builtin_type::BuiltinType,
data::adt::VariantData,
+ db::DefDatabase,
item_tree::{
- Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, MacroDef, MacroRules,
+ Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use,
},
};
@@ -101,7 +104,7 @@ pub struct CrateRootModuleId {
}
impl CrateRootModuleId {
- pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
+ pub fn def_map(&self, db: &dyn DefDatabase) -> Arc<DefMap> {
db.crate_def_map(self.krate)
}
@@ -163,7 +166,7 @@ pub struct ModuleId {
}
impl ModuleId {
- pub fn def_map(self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
+ pub fn def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
match self.block {
Some(block) => db.block_def_map(block),
None => db.crate_def_map(self.krate),
@@ -174,7 +177,7 @@ impl ModuleId {
self.krate
}
- pub fn name(self, db: &dyn db::DefDatabase) -> Option<Name> {
+ pub fn name(self, db: &dyn DefDatabase) -> Option<Name> {
let def_map = self.def_map(db);
let parent = def_map[self.local_id].parent?;
def_map[parent].children.iter().find_map(|(name, module_id)| {
@@ -186,7 +189,7 @@ impl ModuleId {
})
}
- pub fn containing_module(self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
+ pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
self.def_map(db).containing_module(self.local_id)
}
@@ -263,20 +266,7 @@ impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id);
-
- impl Intern for $loc {
- type ID = $id;
- fn intern(self, db: &dyn db::DefDatabase) -> $id {
- db.$intern(self)
- }
- }
-
- impl Lookup for $id {
- type Data = $loc;
- fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
- db.$lookup(*self)
- }
- }
+ impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
};
}
@@ -376,9 +366,10 @@ pub struct Macro2Id(salsa::InternId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Macro2Loc {
pub container: ModuleId,
- pub id: ItemTreeId<MacroDef>,
+ pub id: ItemTreeId<Macro2>,
pub expander: MacroExpander,
pub allow_internal_unsafe: bool,
+ pub edition: Edition,
}
impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2);
@@ -389,19 +380,28 @@ pub struct MacroRulesLoc {
pub container: ModuleId,
pub id: ItemTreeId<MacroRules>,
pub expander: MacroExpander,
- pub allow_internal_unsafe: bool,
- pub local_inner: bool,
+ pub flags: MacroRulesLocFlags,
+ pub edition: Edition,
}
impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules);
+bitflags::bitflags! {
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+ pub struct MacroRulesLocFlags: u8 {
+ const ALLOW_INTERNAL_UNSAFE = 1 << 0;
+ const LOCAL_INNER = 1 << 1;
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ProcMacroId(salsa::InternId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProcMacroLoc {
pub container: CrateRootModuleId,
pub id: ItemTreeId<Function>,
- pub expander: ProcMacroExpander,
+ pub expander: CustomProcMacroExpander,
pub kind: ProcMacroKind,
+ pub edition: Edition,
}
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);
@@ -510,7 +510,7 @@ pub enum MacroId {
impl_from!(Macro2Id, MacroRulesId, ProcMacroId for MacroId);
impl MacroId {
- pub fn is_attribute(self, db: &dyn db::DefDatabase) -> bool {
+ pub fn is_attribute(self, db: &dyn DefDatabase) -> bool {
matches!(self, MacroId::ProcMacroId(it) if it.lookup(db).kind == ProcMacroKind::Attr)
}
}
@@ -722,7 +722,7 @@ impl PartialEq for InTypeConstLoc {
}
impl InTypeConstId {
- pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
+ pub fn source(&self, db: &dyn DefDatabase) -> ast::ConstArg {
let src = self.lookup(db).id;
let file_id = src.file_id;
let root = &db.parse_or_expand(file_id);
@@ -742,7 +742,7 @@ pub enum GeneralConstId {
impl_from!(ConstId, ConstBlockId, InTypeConstId for GeneralConstId);
impl GeneralConstId {
- pub fn generic_def(self, db: &dyn db::DefDatabase) -> Option<GenericDefId> {
+ pub fn generic_def(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
match self {
GeneralConstId::ConstId(it) => Some(it.into()),
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(),
@@ -750,7 +750,7 @@ impl GeneralConstId {
}
}
- pub fn name(self, db: &dyn db::DefDatabase) -> String {
+ pub fn name(self, db: &dyn DefDatabase) -> String {
match self {
GeneralConstId::ConstId(const_id) => db
.const_data(const_id)
@@ -933,7 +933,7 @@ pub enum VariantId {
impl_from!(EnumVariantId, StructId, UnionId for VariantId);
impl VariantId {
- pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
+ pub fn variant_data(self, db: &dyn DefDatabase) -> Arc<VariantData> {
match self {
VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
@@ -943,7 +943,7 @@ impl VariantId {
}
}
- pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
+ pub fn file_id(self, db: &dyn DefDatabase) -> HirFileId {
match self {
VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
VariantId::StructId(it) => it.lookup(db).id.file_id(),
@@ -960,22 +960,12 @@ impl VariantId {
}
}
-trait Intern {
- type ID;
- fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
-}
-
-pub trait Lookup {
- type Data;
- fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
-}
-
pub trait HasModule {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId;
}
impl HasModule for ItemContainerId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match *self {
ItemContainerId::ModuleId(it) => it,
ItemContainerId::ImplId(it) => it.lookup(db).container,
@@ -986,13 +976,13 @@ impl HasModule for ItemContainerId {
}
impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.container.module(db)
}
}
impl HasModule for AdtId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
AdtId::StructId(it) => it.lookup(db).container,
AdtId::UnionId(it) => it.lookup(db).container,
@@ -1002,13 +992,13 @@ impl HasModule for AdtId {
}
impl HasModule for ExternCrateId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).container
}
}
impl HasModule for VariantId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
VariantId::StructId(it) => it.lookup(db).container,
@@ -1018,7 +1008,7 @@ impl HasModule for VariantId {
}
impl HasModule for MacroId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
MacroId::MacroRulesId(it) => it.lookup(db).container,
MacroId::Macro2Id(it) => it.lookup(db).container,
@@ -1028,7 +1018,7 @@ impl HasModule for MacroId {
}
impl HasModule for TypeOwnerId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
TypeOwnerId::FunctionId(it) => it.lookup(db).module(db),
TypeOwnerId::StaticId(it) => it.lookup(db).module(db),
@@ -1045,7 +1035,7 @@ impl HasModule for TypeOwnerId {
}
impl HasModule for DefWithBodyId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
@@ -1057,7 +1047,7 @@ impl HasModule for DefWithBodyId {
}
impl HasModule for GenericDefId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
match self {
GenericDefId::FunctionId(it) => it.lookup(db).module(db),
GenericDefId::AdtId(it) => it.module(db),
@@ -1072,13 +1062,13 @@ impl HasModule for GenericDefId {
}
impl HasModule for TypeAliasId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).module(db)
}
}
impl HasModule for TraitId {
- fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
+ fn module(&self, db: &dyn DefDatabase) -> ModuleId {
self.lookup(db).container
}
}
@@ -1087,7 +1077,7 @@ impl ModuleDefId {
/// Returns the module containing `self` (or `self`, if `self` is itself a module).
///
/// Returns `None` if `self` refers to a primitive type.
- pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
+ pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {
Some(match self {
ModuleDefId::ModuleId(id) => *id,
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
@@ -1105,7 +1095,7 @@ impl ModuleDefId {
}
impl AttrDefId {
- pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
+ pub fn krate(&self, db: &dyn DefDatabase) -> CrateId {
match self {
AttrDefId::ModuleId(it) => it.krate,
AttrDefId::FieldId(it) => it.parent.module(db).krate,
@@ -1171,7 +1161,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
};
- let call_site = span_map.span_for_range(self.value.syntax().text_range()).ctx;
+ let call_site = span_map.span_for_range(self.value.syntax().text_range());
macro_call_as_call_id_with_eager(
db,
@@ -1201,7 +1191,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
fn macro_call_as_call_id(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
- call_site: SyntaxContextId,
+ call_site: Span,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
@@ -1213,7 +1203,7 @@ fn macro_call_as_call_id(
fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
- call_site: SyntaxContextId,
+ call_site: Span,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
@@ -1243,83 +1233,12 @@ fn macro_call_as_call_id_with_eager(
Ok(res)
}
-pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
- match id {
- MacroId::Macro2Id(it) => {
- let loc = it.lookup(db);
-
- let item_tree = loc.id.item_tree(db);
- let makro = &item_tree[loc.id.value];
- let in_file = |m: FileAstId<ast::MacroDef>| InFile::new(loc.id.file_id(), m.upcast());
- MacroDefId {
- krate: loc.container.krate,
- kind: match loc.expander {
- MacroExpander::Declarative => MacroDefKind::Declarative(in_file(makro.ast_id)),
- MacroExpander::BuiltIn(it) => MacroDefKind::BuiltIn(it, in_file(makro.ast_id)),
- MacroExpander::BuiltInAttr(it) => {
- MacroDefKind::BuiltInAttr(it, in_file(makro.ast_id))
- }
- MacroExpander::BuiltInDerive(it) => {
- MacroDefKind::BuiltInDerive(it, in_file(makro.ast_id))
- }
- MacroExpander::BuiltInEager(it) => {
- MacroDefKind::BuiltInEager(it, in_file(makro.ast_id))
- }
- },
- local_inner: false,
- allow_internal_unsafe: loc.allow_internal_unsafe,
- }
- }
- MacroId::MacroRulesId(it) => {
- let loc = it.lookup(db);
-
- let item_tree = loc.id.item_tree(db);
- let makro = &item_tree[loc.id.value];
- let in_file = |m: FileAstId<ast::MacroRules>| InFile::new(loc.id.file_id(), m.upcast());
- MacroDefId {
- krate: loc.container.krate,
- kind: match loc.expander {
- MacroExpander::Declarative => MacroDefKind::Declarative(in_file(makro.ast_id)),
- MacroExpander::BuiltIn(it) => MacroDefKind::BuiltIn(it, in_file(makro.ast_id)),
- MacroExpander::BuiltInAttr(it) => {
- MacroDefKind::BuiltInAttr(it, in_file(makro.ast_id))
- }
- MacroExpander::BuiltInDerive(it) => {
- MacroDefKind::BuiltInDerive(it, in_file(makro.ast_id))
- }
- MacroExpander::BuiltInEager(it) => {
- MacroDefKind::BuiltInEager(it, in_file(makro.ast_id))
- }
- },
- local_inner: loc.local_inner,
- allow_internal_unsafe: loc.allow_internal_unsafe,
- }
- }
- MacroId::ProcMacroId(it) => {
- let loc = it.lookup(db);
-
- let item_tree = loc.id.item_tree(db);
- let makro = &item_tree[loc.id.value];
- MacroDefId {
- krate: loc.container.krate,
- kind: MacroDefKind::ProcMacro(
- loc.expander,
- loc.kind,
- InFile::new(loc.id.file_id(), makro.ast_id),
- ),
- local_inner: false,
- allow_internal_unsafe: false,
- }
- }
- }
-}
-
fn derive_macro_as_call_id(
- db: &dyn db::DefDatabase,
+ db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
- call_site: SyntaxContextId,
+ call_site: Span,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
@@ -1340,7 +1259,7 @@ fn derive_macro_as_call_id(
}
fn attr_macro_as_call_id(
- db: &dyn db::DefDatabase,
+ db: &dyn DefDatabase,
item_attr: &AstIdWithPath<ast::Item>,
macro_attr: &Attr,
krate: CrateId,
@@ -1349,7 +1268,7 @@ fn attr_macro_as_call_id(
let arg = match macro_attr.input.as_deref() {
Some(AttrInput::TokenTree(tt)) => {
let mut tt = tt.as_ref().clone();
- tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
+ tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
Some(tt)
}
@@ -1364,7 +1283,7 @@ fn attr_macro_as_call_id(
attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
- macro_attr.ctxt,
+ macro_attr.span,
)
}