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.rs105
1 files changed, 61 insertions, 44 deletions
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index 682d169adb..f6fe0c618a 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -56,6 +56,7 @@ pub mod find_path;
pub mod import_map;
pub mod visibility;
+use intern::Interned;
pub use rustc_abi as layout;
use triomphe::Arc;
@@ -72,7 +73,7 @@ use std::{
use base_db::{
impl_intern_key,
- salsa::{self, impl_intern_value_trivial},
+ salsa::{self, InternValueTrivial},
CrateId,
};
use hir_expand::{
@@ -90,7 +91,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
-use span::{AstIdNode, Edition, FileAstId, FileId, SyntaxContextId};
+use span::{AstIdNode, Edition, FileAstId, SyntaxContextId};
use stdx::impl_from;
use syntax::{ast, AstNode};
@@ -186,7 +187,7 @@ pub trait ItemTreeLoc {
macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id);
- impl_intern_value_trivial!($loc);
+ impl InternValueTrivial for $loc {}
impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
};
}
@@ -396,6 +397,23 @@ impl PartialEq<ModuleId> for CrateRootModuleId {
other.block.is_none() && other.local_id == DefMap::ROOT && self.krate == other.krate
}
}
+impl PartialEq<CrateRootModuleId> for ModuleId {
+ fn eq(&self, other: &CrateRootModuleId) -> bool {
+ other == self
+ }
+}
+
+impl From<CrateRootModuleId> for ModuleId {
+ fn from(CrateRootModuleId { krate }: CrateRootModuleId) -> Self {
+ ModuleId { krate, block: None, local_id: DefMap::ROOT }
+ }
+}
+
+impl From<CrateRootModuleId> for ModuleDefId {
+ fn from(value: CrateRootModuleId) -> Self {
+ ModuleDefId::ModuleId(value.into())
+ }
+}
impl From<CrateId> for CrateRootModuleId {
fn from(krate: CrateId) -> Self {
@@ -472,6 +490,7 @@ impl ModuleId {
self.block.is_some()
}
+ /// Returns the [`CrateRootModuleId`] for this module if it is the crate root module.
pub fn as_crate_root(&self) -> Option<CrateRootModuleId> {
if self.local_id == DefMap::ROOT && self.block.is_none() {
Some(CrateRootModuleId { krate: self.krate })
@@ -480,33 +499,17 @@ impl ModuleId {
}
}
+ /// Returns the [`CrateRootModuleId`] for this module.
pub fn derive_crate_root(&self) -> CrateRootModuleId {
CrateRootModuleId { krate: self.krate }
}
+ /// Whether this module represents the crate root module
fn is_crate_root(&self) -> bool {
self.local_id == DefMap::ROOT && self.block.is_none()
}
}
-impl PartialEq<CrateRootModuleId> for ModuleId {
- fn eq(&self, other: &CrateRootModuleId) -> bool {
- other == self
- }
-}
-
-impl From<CrateRootModuleId> for ModuleId {
- fn from(CrateRootModuleId { krate }: CrateRootModuleId) -> Self {
- ModuleId { krate, block: None, local_id: DefMap::ROOT }
- }
-}
-
-impl From<CrateRootModuleId> for ModuleDefId {
- fn from(value: CrateRootModuleId) -> Self {
- ModuleDefId::ModuleId(value.into())
- }
-}
-
/// An ID of a module, **local** to a `DefMap`.
pub type LocalModuleId = Idx<nameres::ModuleData>;
@@ -532,7 +535,7 @@ pub struct TypeOrConstParamId {
pub parent: GenericDefId,
pub local_id: LocalTypeOrConstParamId,
}
-impl_intern_value_trivial!(TypeOrConstParamId);
+impl InternValueTrivial for TypeOrConstParamId {}
/// A TypeOrConstParamId with an invariant that it actually belongs to a type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -594,7 +597,7 @@ pub struct LifetimeParamId {
pub local_id: LocalLifetimeParamId,
}
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
-impl_intern_value_trivial!(LifetimeParamId);
+impl InternValueTrivial for LifetimeParamId {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemContainerId {
@@ -920,6 +923,7 @@ pub enum GenericDefId {
ImplId(ImplId),
// enum variants cannot have generics themselves, but their parent enums
// can, and this makes some code easier to write
+ // FIXME: Try to remove this as that will reduce the amount of query slots generated per enum?
EnumVariantId(EnumVariantId),
// consts can have type parameters from their parents (i.e. associated consts of traits)
ConstId(ConstId),
@@ -956,15 +960,14 @@ impl GenericDefId {
match self {
GenericDefId::FunctionId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::TypeAliasId(it) => file_id_and_params_of_item_loc(db, it),
- GenericDefId::ConstId(_) => (FileId::BOGUS.into(), None),
GenericDefId::AdtId(AdtId::StructId(it)) => file_id_and_params_of_item_loc(db, it),
GenericDefId::AdtId(AdtId::UnionId(it)) => file_id_and_params_of_item_loc(db, it),
GenericDefId::AdtId(AdtId::EnumId(it)) => file_id_and_params_of_item_loc(db, it),
GenericDefId::TraitId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::TraitAliasId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::ImplId(it) => file_id_and_params_of_item_loc(db, it),
- // We won't be using this ID anyway
- GenericDefId::EnumVariantId(_) => (FileId::BOGUS.into(), None),
+ GenericDefId::ConstId(it) => (it.lookup(db).id.file_id(), None),
+ GenericDefId::EnumVariantId(it) => (it.lookup(db).id.file_id(), None),
}
}
@@ -1368,7 +1371,7 @@ pub trait AsMacroCall {
&self,
db: &dyn ExpandDatabase,
krate: CrateId,
- resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
+ resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Option<MacroCallId> {
self.as_call_id_with_errors(db, krate, resolver).ok()?.value
}
@@ -1377,7 +1380,7 @@ pub trait AsMacroCall {
&self,
db: &dyn ExpandDatabase,
krate: CrateId,
- resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
+ resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
}
@@ -1386,7 +1389,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
&self,
db: &dyn ExpandDatabase,
krate: CrateId,
- resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
+ resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
@@ -1406,7 +1409,8 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
macro_call_as_call_id_with_eager(
db,
- &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
+ ast_id,
+ &path,
call_site.ctx,
expands_to,
krate,
@@ -1420,11 +1424,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
#[derive(Clone, Debug, Eq, PartialEq)]
struct AstIdWithPath<T: AstIdNode> {
ast_id: AstId<T>,
- path: path::ModPath,
+ path: Interned<path::ModPath>,
}
impl<T: AstIdNode> AstIdWithPath<T> {
- fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
+ fn new(
+ file_id: HirFileId,
+ ast_id: FileAstId<T>,
+ path: Interned<path::ModPath>,
+ ) -> AstIdWithPath<T> {
AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
}
}
@@ -1435,30 +1443,39 @@ fn macro_call_as_call_id(
call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
- resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
+ resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<Option<MacroCallId>, UnresolvedMacro> {
- macro_call_as_call_id_with_eager(db, call, call_site, expand_to, krate, resolver, resolver)
- .map(|res| res.value)
+ macro_call_as_call_id_with_eager(
+ db,
+ call.ast_id,
+ &call.path,
+ call_site,
+ expand_to,
+ krate,
+ resolver,
+ resolver,
+ )
+ .map(|res| res.value)
}
fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase,
- call: &AstIdWithPath<ast::MacroCall>,
+ ast_id: AstId<ast::MacroCall>,
+ path: &path::ModPath,
call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
- resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
- eager_resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
+ resolver: impl FnOnce(&path::ModPath) -> Option<MacroDefId>,
+ eager_resolver: impl Fn(&path::ModPath) -> Option<MacroDefId>,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
- let def =
- resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
+ let def = resolver(path).ok_or_else(|| UnresolvedMacro { path: path.clone() })?;
let res = match def.kind {
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(
db,
krate,
- &call.ast_id.to_node(db),
- call.ast_id,
+ &ast_id.to_node(db),
+ ast_id,
def,
call_site,
&|path| eager_resolver(path).filter(MacroDefId::is_fn_like),
@@ -1467,12 +1484,12 @@ fn macro_call_as_call_id_with_eager(
value: Some(def.make_call(
db,
krate,
- MacroCallKind::FnLike { ast_id: call.ast_id, expand_to, eager: None },
+ MacroCallKind::FnLike { ast_id, expand_to, eager: None },
call_site,
)),
err: None,
},
- _ => return Err(UnresolvedMacro { path: call.path.clone() }),
+ _ => return Err(UnresolvedMacro { path: path.clone() }),
};
Ok(res)
}