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)*) };
44 pub mod child_by_source;
53 mod macro_expansion_tests;
61 use base_db::{impl_intern_key, salsa, CrateId};
63 ast_id_map::FileAstId,
64 eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
66 AstId, ExpandError, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId,
67 MacroDefKind, UnresolvedMacro,
69 use item_tree::ExternBlock;
78 builtin_type::BuiltinType,
80 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
85 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
89 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
90 /// `DefMap` of `krate`.
91 block: Option<BlockId>,
92 /// The module's ID in its originating `DefMap`.
93 pub local_id: LocalModuleId,
97 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
100 db.block_def_map(block).unwrap_or_else(|| {
101 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
102 // so the `DefMap` here must exist.
103 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
106 None => db.crate_def_map(self.krate),
110 pub fn krate(&self) -> CrateId {
114 pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
115 self.def_map(db).containing_module(self.local_id)
118 pub fn containing_block(&self) -> Option<BlockId> {
123 /// An ID of a module, **local** to a specific crate
124 pub type LocalModuleId = Idx<nameres::ModuleData>;
127 pub struct ItemLoc<N: ItemTreeNode> {
128 pub container: ModuleId,
129 pub id: ItemTreeId<N>,
132 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
133 fn clone(&self) -> Self {
134 Self { container: self.container, id: self.id }
138 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
140 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
141 fn eq(&self, other: &Self) -> bool {
142 self.container == other.container && self.id == other.id
146 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
148 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
149 fn hash<H: Hasher>(&self, state: &mut H) {
150 self.container.hash(state);
156 pub struct AssocItemLoc<N: ItemTreeNode> {
157 pub container: ItemContainerId,
158 pub id: ItemTreeId<N>,
161 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
162 fn clone(&self) -> Self {
163 Self { container: self.container, id: self.id }
167 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
169 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
170 fn eq(&self, other: &Self) -> bool {
171 self.container == other.container && self.id == other.id
175 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
177 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
178 fn hash<H: Hasher>(&self, state: &mut H) {
179 self.container.hash(state);
184 macro_rules! impl_intern {
185 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
186 impl_intern_key!($id);
188 impl Intern for $loc {
190 fn intern(self, db: &dyn db::DefDatabase) -> $id {
195 impl Lookup for $id {
197 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
204 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
205 pub struct FunctionId(salsa::InternId);
206 type FunctionLoc = AssocItemLoc<Function>;
207 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
209 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
210 pub struct StructId(salsa::InternId);
211 type StructLoc = ItemLoc<Struct>;
212 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
214 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
215 pub struct UnionId(salsa::InternId);
216 pub type UnionLoc = ItemLoc<Union>;
217 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
219 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
220 pub struct EnumId(salsa::InternId);
221 pub type EnumLoc = ItemLoc<Enum>;
222 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
224 // FIXME: rename to `VariantId`, only enums can ave variants
225 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
226 pub struct EnumVariantId {
228 pub local_id: LocalEnumVariantId,
231 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
233 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
235 pub parent: VariantId,
236 pub local_id: LocalFieldId,
239 pub type LocalFieldId = Idx<adt::FieldData>;
241 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
242 pub struct ConstId(salsa::InternId);
243 type ConstLoc = AssocItemLoc<Const>;
244 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
246 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
247 pub struct StaticId(salsa::InternId);
248 pub type StaticLoc = AssocItemLoc<Static>;
249 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
251 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
252 pub struct TraitId(salsa::InternId);
253 pub type TraitLoc = ItemLoc<Trait>;
254 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
256 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
257 pub struct TypeAliasId(salsa::InternId);
258 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
259 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
261 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
262 pub struct ImplId(salsa::InternId);
263 type ImplLoc = ItemLoc<Impl>;
264 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
266 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
267 pub struct ExternBlockId(salsa::InternId);
268 type ExternBlockLoc = ItemLoc<ExternBlock>;
269 impl_intern!(ExternBlockId, ExternBlockLoc, intern_extern_block, lookup_intern_extern_block);
271 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
272 pub struct BlockId(salsa::InternId);
273 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
274 pub struct BlockLoc {
275 ast_id: AstId<ast::BlockExpr>,
276 /// The containing module.
279 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
281 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
282 pub struct TypeParamId {
283 pub parent: GenericDefId,
284 pub local_id: LocalTypeParamId,
287 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
289 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
290 pub struct LifetimeParamId {
291 pub parent: GenericDefId,
292 pub local_id: LocalLifetimeParamId,
294 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
296 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
297 pub struct ConstParamId {
298 pub parent: GenericDefId,
299 pub local_id: LocalConstParamId,
301 pub type LocalConstParamId = Idx<generics::ConstParamData>;
303 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
304 pub enum ItemContainerId {
305 ExternBlockId(ExternBlockId),
310 impl_from!(ModuleId for ItemContainerId);
313 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
319 impl_from!(StructId, UnionId, EnumId for AdtId);
322 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
323 pub enum GenericParamId {
324 TypeParamId(TypeParamId),
325 LifetimeParamId(LifetimeParamId),
326 ConstParamId(ConstParamId),
328 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
330 /// The defs which can be visible in the module.
331 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
332 pub enum ModuleDefId {
334 FunctionId(FunctionId),
336 // Can't be directly declared, but can be imported.
337 EnumVariantId(EnumVariantId),
341 TypeAliasId(TypeAliasId),
342 BuiltinType(BuiltinType),
347 AdtId(StructId, EnumId, UnionId),
357 /// The defs which have a body.
358 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
359 pub enum DefWithBodyId {
360 FunctionId(FunctionId),
365 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
368 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
370 DefWithBodyId::FunctionId(f) => Some(f.into()),
371 DefWithBodyId::StaticId(_) => None,
372 DefWithBodyId::ConstId(c) => Some(c.into()),
377 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
378 pub enum AssocItemId {
379 FunctionId(FunctionId),
381 TypeAliasId(TypeAliasId),
383 // FIXME: not every function, ... is actually an assoc item. maybe we should make
384 // sure that you can only turn actual assoc items into AssocItemIds. This would
385 // require not implementing From, and instead having some checked way of
386 // casting them, and somehow making the constructors private, which would be annoying.
387 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
389 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
390 pub enum GenericDefId {
391 FunctionId(FunctionId),
394 TypeAliasId(TypeAliasId),
396 // enum variants cannot have generics themselves, but their parent enums
397 // can, and this makes some code easier to write
398 EnumVariantId(EnumVariantId),
399 // consts can have type parameters from their parents (i.e. associated consts of traits)
404 AdtId(StructId, EnumId, UnionId),
413 impl From<AssocItemId> for GenericDefId {
414 fn from(item: AssocItemId) -> Self {
416 AssocItemId::FunctionId(f) => f.into(),
417 AssocItemId::ConstId(c) => c.into(),
418 AssocItemId::TypeAliasId(t) => t.into(),
423 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
428 FunctionId(FunctionId),
429 EnumVariantId(EnumVariantId),
433 TypeAliasId(TypeAliasId),
434 MacroDefId(MacroDefId),
436 GenericParamId(GenericParamId),
437 ExternBlockId(ExternBlockId),
443 AdtId(StructId, EnumId, UnionId),
456 impl From<ItemContainerId> for AttrDefId {
457 fn from(acid: ItemContainerId) -> Self {
459 ItemContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
460 ItemContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
461 ItemContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
462 ItemContainerId::ExternBlockId(id) => AttrDefId::ExternBlockId(id),
467 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
469 EnumVariantId(EnumVariantId),
473 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
476 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
478 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
479 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
480 VariantId::EnumVariantId(it) => {
481 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
486 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
488 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
489 VariantId::StructId(it) => it.lookup(db).id.file_id(),
490 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
494 pub fn adt_id(self) -> AdtId {
496 VariantId::EnumVariantId(it) => it.parent.into(),
497 VariantId::StructId(it) => it.into(),
498 VariantId::UnionId(it) => it.into(),
505 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
510 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
513 pub trait HasModule {
514 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
517 impl HasModule for ItemContainerId {
518 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
520 ItemContainerId::ModuleId(it) => it,
521 ItemContainerId::ImplId(it) => it.lookup(db).container,
522 ItemContainerId::TraitId(it) => it.lookup(db).container,
523 ItemContainerId::ExternBlockId(it) => it.lookup(db).container,
528 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
529 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
530 self.container.module(db)
534 impl HasModule for AdtId {
535 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
537 AdtId::StructId(it) => it.lookup(db).container,
538 AdtId::UnionId(it) => it.lookup(db).container,
539 AdtId::EnumId(it) => it.lookup(db).container,
544 impl HasModule for VariantId {
545 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
547 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
548 VariantId::StructId(it) => it.lookup(db).container,
549 VariantId::UnionId(it) => it.lookup(db).container,
554 impl HasModule for DefWithBodyId {
555 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
557 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
558 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
559 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
565 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
567 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
568 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
569 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
574 impl HasModule for GenericDefId {
575 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
577 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
578 GenericDefId::AdtId(it) => it.module(db),
579 GenericDefId::TraitId(it) => it.lookup(db).container,
580 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
581 GenericDefId::ImplId(it) => it.lookup(db).container,
582 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
583 GenericDefId::ConstId(it) => it.lookup(db).module(db),
588 impl HasModule for TypeAliasId {
589 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
590 self.lookup(db).module(db)
594 impl HasModule for TraitId {
595 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
596 self.lookup(db).container
601 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
603 /// Returns `None` if `self` refers to a primitive type.
604 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
606 ModuleDefId::ModuleId(id) => *id,
607 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
608 ModuleDefId::AdtId(id) => id.module(db),
609 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
610 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
611 ModuleDefId::StaticId(id) => id.lookup(db).module(db),
612 ModuleDefId::TraitId(id) => id.lookup(db).container,
613 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
614 ModuleDefId::BuiltinType(_) => return None,
620 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
622 AttrDefId::ModuleId(it) => it.krate,
623 AttrDefId::FieldId(it) => it.parent.module(db).krate,
624 AttrDefId::AdtId(it) => it.module(db).krate,
625 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
626 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
627 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
628 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
629 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
630 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
631 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
632 AttrDefId::ExternBlockId(it) => it.lookup(db).container.krate,
633 AttrDefId::GenericParamId(it) => {
635 GenericParamId::TypeParamId(it) => it.parent,
636 GenericParamId::LifetimeParamId(it) => it.parent,
637 GenericParamId::ConstParamId(it) => it.parent,
642 // FIXME: `MacroDefId` should store the defining module, then this can implement
644 AttrDefId::MacroDefId(it) => it.krate,
649 /// A helper trait for converting to MacroCallId
650 pub trait AsMacroCall {
653 db: &dyn db::DefDatabase,
655 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
656 ) -> Option<MacroCallId> {
657 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
660 fn as_call_id_with_errors(
662 db: &dyn db::DefDatabase,
664 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
665 error_sink: &mut dyn FnMut(ExpandError),
666 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
669 impl AsMacroCall for InFile<&ast::MacroCall> {
670 fn as_call_id_with_errors(
672 db: &dyn db::DefDatabase,
674 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
675 mut error_sink: &mut dyn FnMut(ExpandError),
676 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
677 let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
678 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
679 let h = Hygiene::new(db.upcast(), self.file_id);
681 self.value.path().and_then(|path| path::ModPath::from_src(db.upcast(), path, &h));
683 let path = match error_sink
684 .option(path, || ExpandError::Other("malformed macro invocation".into()))
688 return Ok(Err(error));
692 macro_call_as_call_id(
694 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
703 /// Helper wrapper for `AstId` with `ModPath`
704 #[derive(Clone, Debug, Eq, PartialEq)]
705 struct AstIdWithPath<T: ast::AstNode> {
710 impl<T: ast::AstNode> AstIdWithPath<T> {
711 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
712 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
716 fn macro_call_as_call_id(
717 db: &dyn db::DefDatabase,
718 call: &AstIdWithPath<ast::MacroCall>,
721 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
722 error_sink: &mut dyn FnMut(ExpandError),
723 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
724 let def: MacroDefId =
725 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
727 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
728 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
730 expand_eager_macro(db.upcast(), krate, macro_call, def, &resolver, error_sink)?
732 Ok(def.as_lazy_macro(
735 MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
741 fn derive_macro_as_call_id(
742 db: &dyn db::DefDatabase,
743 item_attr: &AstIdWithPath<ast::Adt>,
747 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
748 ) -> Result<MacroCallId, UnresolvedMacro> {
749 let def: MacroDefId = resolver(item_attr.path.clone())
750 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
751 let res = def.as_lazy_macro(
754 MacroCallKind::Derive {
755 ast_id: item_attr.ast_id,
756 derive_index: derive_pos,
757 derive_attr_index: derive_attr.ast_index,
763 fn attr_macro_as_call_id(
764 db: &dyn db::DefDatabase,
765 item_attr: &AstIdWithPath<ast::Item>,
771 let mut arg = match macro_attr.input.as_deref() {
772 Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()),
773 _ => Default::default(),
776 // The parentheses are always disposed here.
777 arg.0.delimiter = None;
779 let res = def.as_lazy_macro(
782 MacroCallKind::Attr {
783 ast_id: item_attr.ast_id,
784 attr_args: Arc::new(arg),
785 invoc_attr_index: macro_attr.id.ast_index,