1 //! `hir_def` crate contains everything between macro expansion and type
4 //! It defines various items (structs, enums, traits) which comprises Rust code,
5 //! as well as an algorithm for resolving paths to such entities.
7 //! Note that `hir_def` is a work in progress, so not all of the above is
11 macro_rules! eprintln {
12 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
45 pub mod child_by_source;
60 use base_db::{impl_intern_key, salsa, CrateId};
62 ast_id_map::FileAstId,
63 eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
65 AstId, AttrId, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
73 use crate::builtin_type::BuiltinType;
75 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
83 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
84 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
85 /// `DefMap` of `krate`.
86 block: Option<BlockId>,
87 /// The module's ID in its originating `DefMap`.
88 pub local_id: LocalModuleId,
92 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
95 db.block_def_map(block).unwrap_or_else(|| {
96 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
97 // so the `DefMap` here must exist.
98 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
101 None => db.crate_def_map(self.krate),
105 pub fn krate(&self) -> CrateId {
109 pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
110 self.def_map(db).containing_module(self.local_id)
113 /// Returns `true` if this module represents a block expression.
115 /// Returns `false` if this module is a submodule *inside* a block expression
116 /// (eg. `m` in `{ mod m {} }`).
117 pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool {
118 if self.block.is_none() {
122 self.def_map(db)[self.local_id].parent.is_none()
126 /// An ID of a module, **local** to a specific crate
127 pub type LocalModuleId = Idx<nameres::ModuleData>;
130 pub struct ItemLoc<N: ItemTreeNode> {
131 pub container: ModuleId,
132 pub id: ItemTreeId<N>,
135 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
136 fn clone(&self) -> Self {
137 Self { container: self.container, id: self.id }
141 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
143 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
144 fn eq(&self, other: &Self) -> bool {
145 self.container == other.container && self.id == other.id
149 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
151 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
152 fn hash<H: Hasher>(&self, state: &mut H) {
153 self.container.hash(state);
159 pub struct AssocItemLoc<N: ItemTreeNode> {
160 pub container: AssocContainerId,
161 pub id: ItemTreeId<N>,
164 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
165 fn clone(&self) -> Self {
166 Self { container: self.container, id: self.id }
170 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
172 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
173 fn eq(&self, other: &Self) -> bool {
174 self.container == other.container && self.id == other.id
178 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
180 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
181 fn hash<H: Hasher>(&self, state: &mut H) {
182 self.container.hash(state);
187 macro_rules! impl_intern {
188 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
189 impl_intern_key!($id);
191 impl Intern for $loc {
193 fn intern(self, db: &dyn db::DefDatabase) -> $id {
198 impl Lookup for $id {
200 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
207 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
208 pub struct FunctionId(salsa::InternId);
209 type FunctionLoc = AssocItemLoc<Function>;
210 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
212 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
213 pub struct StructId(salsa::InternId);
214 type StructLoc = ItemLoc<Struct>;
215 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
217 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
218 pub struct UnionId(salsa::InternId);
219 pub type UnionLoc = ItemLoc<Union>;
220 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
222 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
223 pub struct EnumId(salsa::InternId);
224 pub type EnumLoc = ItemLoc<Enum>;
225 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
227 // FIXME: rename to `VariantId`, only enums can ave variants
228 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
229 pub struct EnumVariantId {
231 pub local_id: LocalEnumVariantId,
234 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
236 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
238 pub parent: VariantId,
239 pub local_id: LocalFieldId,
242 pub type LocalFieldId = Idx<adt::FieldData>;
244 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
245 pub struct ConstId(salsa::InternId);
246 type ConstLoc = AssocItemLoc<Const>;
247 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
249 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
250 pub struct StaticId(salsa::InternId);
251 pub type StaticLoc = ItemLoc<Static>;
252 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
254 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
255 pub struct TraitId(salsa::InternId);
256 pub type TraitLoc = ItemLoc<Trait>;
257 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
259 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
260 pub struct TypeAliasId(salsa::InternId);
261 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
262 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
264 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
265 pub struct ImplId(salsa::InternId);
266 type ImplLoc = ItemLoc<Impl>;
267 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
269 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
270 pub struct BlockId(salsa::InternId);
271 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
272 pub struct BlockLoc {
273 ast_id: AstId<ast::BlockExpr>,
274 /// The containing module.
277 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
279 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
280 pub struct TypeParamId {
281 pub parent: GenericDefId,
282 pub local_id: LocalTypeParamId,
285 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
287 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
288 pub struct LifetimeParamId {
289 pub parent: GenericDefId,
290 pub local_id: LocalLifetimeParamId,
292 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
294 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
295 pub struct ConstParamId {
296 pub parent: GenericDefId,
297 pub local_id: LocalConstParamId,
299 pub type LocalConstParamId = Idx<generics::ConstParamData>;
301 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
302 pub enum AssocContainerId {
307 impl_from!(ModuleId for AssocContainerId);
310 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
316 impl_from!(StructId, UnionId, EnumId for AdtId);
319 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
320 pub enum GenericParamId {
321 TypeParamId(TypeParamId),
322 LifetimeParamId(LifetimeParamId),
323 ConstParamId(ConstParamId),
325 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
327 /// The defs which can be visible in the module.
328 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
329 pub enum ModuleDefId {
331 FunctionId(FunctionId),
333 // Can't be directly declared, but can be imported.
334 EnumVariantId(EnumVariantId),
338 TypeAliasId(TypeAliasId),
339 BuiltinType(BuiltinType),
344 AdtId(StructId, EnumId, UnionId),
354 /// The defs which have a body.
355 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
356 pub enum DefWithBodyId {
357 FunctionId(FunctionId),
362 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
365 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
367 DefWithBodyId::FunctionId(f) => Some(f.into()),
368 DefWithBodyId::StaticId(_) => None,
369 DefWithBodyId::ConstId(c) => Some(c.into()),
374 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
375 pub enum AssocItemId {
376 FunctionId(FunctionId),
378 TypeAliasId(TypeAliasId),
380 // FIXME: not every function, ... is actually an assoc item. maybe we should make
381 // sure that you can only turn actual assoc items into AssocItemIds. This would
382 // require not implementing From, and instead having some checked way of
383 // casting them, and somehow making the constructors private, which would be annoying.
384 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
386 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
387 pub enum GenericDefId {
388 FunctionId(FunctionId),
391 TypeAliasId(TypeAliasId),
393 // enum variants cannot have generics themselves, but their parent enums
394 // can, and this makes some code easier to write
395 EnumVariantId(EnumVariantId),
396 // consts can have type parameters from their parents (i.e. associated consts of traits)
401 AdtId(StructId, EnumId, UnionId),
410 impl From<AssocItemId> for GenericDefId {
411 fn from(item: AssocItemId) -> Self {
413 AssocItemId::FunctionId(f) => f.into(),
414 AssocItemId::ConstId(c) => c.into(),
415 AssocItemId::TypeAliasId(t) => t.into(),
420 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
425 FunctionId(FunctionId),
426 EnumVariantId(EnumVariantId),
430 TypeAliasId(TypeAliasId),
431 MacroDefId(MacroDefId),
433 GenericParamId(GenericParamId),
439 AdtId(StructId, EnumId, UnionId),
452 impl From<AssocContainerId> for AttrDefId {
453 fn from(acid: AssocContainerId) -> Self {
455 AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
456 AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
457 AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
462 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
464 EnumVariantId(EnumVariantId),
468 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
471 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
473 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
474 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
475 VariantId::EnumVariantId(it) => {
476 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
481 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
483 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
484 VariantId::StructId(it) => it.lookup(db).id.file_id(),
485 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
492 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
497 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
500 pub trait HasModule {
501 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
504 impl HasModule for AssocContainerId {
505 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
507 AssocContainerId::ModuleId(it) => it,
508 AssocContainerId::ImplId(it) => it.lookup(db).container,
509 AssocContainerId::TraitId(it) => it.lookup(db).container,
514 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
515 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
516 self.container.module(db)
520 impl HasModule for AdtId {
521 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
523 AdtId::StructId(it) => it.lookup(db).container,
524 AdtId::UnionId(it) => it.lookup(db).container,
525 AdtId::EnumId(it) => it.lookup(db).container,
530 impl HasModule for VariantId {
531 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
533 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
534 VariantId::StructId(it) => it.lookup(db).container,
535 VariantId::UnionId(it) => it.lookup(db).container,
540 impl HasModule for DefWithBodyId {
541 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
543 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
544 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
545 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
551 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
553 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
554 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
555 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
560 impl HasModule for GenericDefId {
561 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
563 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
564 GenericDefId::AdtId(it) => it.module(db),
565 GenericDefId::TraitId(it) => it.lookup(db).container,
566 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
567 GenericDefId::ImplId(it) => it.lookup(db).container,
568 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
569 GenericDefId::ConstId(it) => it.lookup(db).module(db),
574 impl HasModule for StaticLoc {
575 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
581 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
583 /// Returns `None` if `self` refers to a primitive type.
584 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
586 ModuleDefId::ModuleId(id) => *id,
587 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
588 ModuleDefId::AdtId(id) => id.module(db),
589 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
590 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
591 ModuleDefId::StaticId(id) => id.lookup(db).container,
592 ModuleDefId::TraitId(id) => id.lookup(db).container,
593 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
594 ModuleDefId::BuiltinType(_) => return None,
600 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
602 AttrDefId::ModuleId(it) => it.krate,
603 AttrDefId::FieldId(it) => it.parent.module(db).krate,
604 AttrDefId::AdtId(it) => it.module(db).krate,
605 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
606 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
607 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
608 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
609 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
610 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
611 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
612 AttrDefId::GenericParamId(it) => {
614 GenericParamId::TypeParamId(it) => it.parent,
615 GenericParamId::LifetimeParamId(it) => it.parent,
616 GenericParamId::ConstParamId(it) => it.parent,
621 // FIXME: `MacroDefId` should store the defining module, then this can implement
623 AttrDefId::MacroDefId(it) => it.krate,
628 /// A helper trait for converting to MacroCallId
629 pub trait AsMacroCall {
632 db: &dyn db::DefDatabase,
634 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
635 ) -> Option<MacroCallId> {
636 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
639 fn as_call_id_with_errors(
641 db: &dyn db::DefDatabase,
643 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
644 error_sink: &mut dyn FnMut(mbe::ExpandError),
645 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
648 impl AsMacroCall for InFile<&ast::MacroCall> {
649 fn as_call_id_with_errors(
651 db: &dyn db::DefDatabase,
653 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
654 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
655 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
656 let fragment = hir_expand::to_fragment_kind(self.value);
657 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
658 let h = Hygiene::new(db.upcast(), self.file_id);
659 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
661 let path = match error_sink
662 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
666 return Ok(Err(error));
670 macro_call_as_call_id(
671 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
681 /// Helper wrapper for `AstId` with `ModPath`
682 #[derive(Clone, Debug, Eq, PartialEq)]
683 struct AstIdWithPath<T: ast::AstNode> {
688 impl<T: ast::AstNode> AstIdWithPath<T> {
689 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
690 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
695 pub struct UnresolvedMacro {
699 fn macro_call_as_call_id(
700 call: &AstIdWithPath<ast::MacroCall>,
701 fragment: FragmentKind,
702 db: &dyn db::DefDatabase,
704 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
705 error_sink: &mut dyn FnMut(mbe::ExpandError),
706 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
707 let def: MacroDefId =
708 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
710 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
711 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
712 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
719 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
722 .map(MacroCallId::from)
728 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
735 fn derive_macro_as_call_id(
736 item_attr: &AstIdWithPath<ast::Item>,
738 db: &dyn db::DefDatabase,
740 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
741 ) -> Result<MacroCallId, UnresolvedMacro> {
742 let def: MacroDefId = resolver(item_attr.path.clone())
743 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
744 let last_segment = item_attr
748 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
753 MacroCallKind::Derive {
754 ast_id: item_attr.ast_id,
755 derive_name: last_segment.to_string(),