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;
59 use base_db::{impl_intern_key, salsa, CrateId};
61 ast_id_map::FileAstId,
62 eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
64 AstId, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
75 builtin_type::BuiltinType,
77 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
82 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
85 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
86 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
87 /// `DefMap` of `krate`.
88 block: Option<BlockId>,
89 /// The module's ID in its originating `DefMap`.
90 pub local_id: LocalModuleId,
94 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
97 db.block_def_map(block).unwrap_or_else(|| {
98 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
99 // so the `DefMap` here must exist.
100 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
103 None => db.crate_def_map(self.krate),
107 pub fn krate(&self) -> CrateId {
111 pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
112 self.def_map(db).containing_module(self.local_id)
115 pub fn containing_block(&self) -> Option<BlockId> {
119 /// Returns `true` if this module represents a block expression.
121 /// Returns `false` if this module is a submodule *inside* a block expression
122 /// (eg. `m` in `{ mod m {} }`).
123 pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool {
124 if self.block.is_none() {
128 self.def_map(db)[self.local_id].parent.is_none()
132 /// An ID of a module, **local** to a specific crate
133 pub type LocalModuleId = Idx<nameres::ModuleData>;
136 pub struct ItemLoc<N: ItemTreeNode> {
137 pub container: ModuleId,
138 pub id: ItemTreeId<N>,
141 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
142 fn clone(&self) -> Self {
143 Self { container: self.container, id: self.id }
147 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
149 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
150 fn eq(&self, other: &Self) -> bool {
151 self.container == other.container && self.id == other.id
155 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
157 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
158 fn hash<H: Hasher>(&self, state: &mut H) {
159 self.container.hash(state);
165 pub struct AssocItemLoc<N: ItemTreeNode> {
166 pub container: AssocContainerId,
167 pub id: ItemTreeId<N>,
170 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
171 fn clone(&self) -> Self {
172 Self { container: self.container, id: self.id }
176 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
178 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
179 fn eq(&self, other: &Self) -> bool {
180 self.container == other.container && self.id == other.id
184 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
186 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
187 fn hash<H: Hasher>(&self, state: &mut H) {
188 self.container.hash(state);
193 macro_rules! impl_intern {
194 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
195 impl_intern_key!($id);
197 impl Intern for $loc {
199 fn intern(self, db: &dyn db::DefDatabase) -> $id {
204 impl Lookup for $id {
206 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
213 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
214 pub struct FunctionId(salsa::InternId);
215 type FunctionLoc = AssocItemLoc<Function>;
216 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
218 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
219 pub struct StructId(salsa::InternId);
220 type StructLoc = ItemLoc<Struct>;
221 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
223 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
224 pub struct UnionId(salsa::InternId);
225 pub type UnionLoc = ItemLoc<Union>;
226 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
228 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
229 pub struct EnumId(salsa::InternId);
230 pub type EnumLoc = ItemLoc<Enum>;
231 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
233 // FIXME: rename to `VariantId`, only enums can ave variants
234 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
235 pub struct EnumVariantId {
237 pub local_id: LocalEnumVariantId,
240 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
242 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
244 pub parent: VariantId,
245 pub local_id: LocalFieldId,
248 pub type LocalFieldId = Idx<adt::FieldData>;
250 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
251 pub struct ConstId(salsa::InternId);
252 type ConstLoc = AssocItemLoc<Const>;
253 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
255 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
256 pub struct StaticId(salsa::InternId);
257 pub type StaticLoc = ItemLoc<Static>;
258 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
260 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
261 pub struct TraitId(salsa::InternId);
262 pub type TraitLoc = ItemLoc<Trait>;
263 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
265 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
266 pub struct TypeAliasId(salsa::InternId);
267 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
268 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
270 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
271 pub struct ImplId(salsa::InternId);
272 type ImplLoc = ItemLoc<Impl>;
273 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
275 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
276 pub struct BlockId(salsa::InternId);
277 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
278 pub struct BlockLoc {
279 ast_id: AstId<ast::BlockExpr>,
280 /// The containing module.
283 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
285 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
286 pub struct TypeParamId {
287 pub parent: GenericDefId,
288 pub local_id: LocalTypeParamId,
291 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
293 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
294 pub struct LifetimeParamId {
295 pub parent: GenericDefId,
296 pub local_id: LocalLifetimeParamId,
298 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
300 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
301 pub struct ConstParamId {
302 pub parent: GenericDefId,
303 pub local_id: LocalConstParamId,
305 pub type LocalConstParamId = Idx<generics::ConstParamData>;
307 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
308 pub enum AssocContainerId {
313 impl_from!(ModuleId for AssocContainerId);
316 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
322 impl_from!(StructId, UnionId, EnumId for AdtId);
325 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
326 pub enum GenericParamId {
327 TypeParamId(TypeParamId),
328 LifetimeParamId(LifetimeParamId),
329 ConstParamId(ConstParamId),
331 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
333 /// The defs which can be visible in the module.
334 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
335 pub enum ModuleDefId {
337 FunctionId(FunctionId),
339 // Can't be directly declared, but can be imported.
340 EnumVariantId(EnumVariantId),
344 TypeAliasId(TypeAliasId),
345 BuiltinType(BuiltinType),
350 AdtId(StructId, EnumId, UnionId),
360 /// The defs which have a body.
361 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
362 pub enum DefWithBodyId {
363 FunctionId(FunctionId),
368 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
371 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
373 DefWithBodyId::FunctionId(f) => Some(f.into()),
374 DefWithBodyId::StaticId(_) => None,
375 DefWithBodyId::ConstId(c) => Some(c.into()),
380 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
381 pub enum AssocItemId {
382 FunctionId(FunctionId),
384 TypeAliasId(TypeAliasId),
386 // FIXME: not every function, ... is actually an assoc item. maybe we should make
387 // sure that you can only turn actual assoc items into AssocItemIds. This would
388 // require not implementing From, and instead having some checked way of
389 // casting them, and somehow making the constructors private, which would be annoying.
390 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
392 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
393 pub enum GenericDefId {
394 FunctionId(FunctionId),
397 TypeAliasId(TypeAliasId),
399 // enum variants cannot have generics themselves, but their parent enums
400 // can, and this makes some code easier to write
401 EnumVariantId(EnumVariantId),
402 // consts can have type parameters from their parents (i.e. associated consts of traits)
407 AdtId(StructId, EnumId, UnionId),
416 impl From<AssocItemId> for GenericDefId {
417 fn from(item: AssocItemId) -> Self {
419 AssocItemId::FunctionId(f) => f.into(),
420 AssocItemId::ConstId(c) => c.into(),
421 AssocItemId::TypeAliasId(t) => t.into(),
426 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
431 FunctionId(FunctionId),
432 EnumVariantId(EnumVariantId),
436 TypeAliasId(TypeAliasId),
437 MacroDefId(MacroDefId),
439 GenericParamId(GenericParamId),
445 AdtId(StructId, EnumId, UnionId),
458 impl From<AssocContainerId> for AttrDefId {
459 fn from(acid: AssocContainerId) -> Self {
461 AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
462 AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
463 AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
468 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
470 EnumVariantId(EnumVariantId),
474 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
477 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
479 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
480 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
481 VariantId::EnumVariantId(it) => {
482 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
487 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
489 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
490 VariantId::StructId(it) => it.lookup(db).id.file_id(),
491 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
495 pub fn adt_id(self) -> AdtId {
497 VariantId::EnumVariantId(it) => it.parent.into(),
498 VariantId::StructId(it) => it.into(),
499 VariantId::UnionId(it) => it.into(),
506 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
511 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
514 pub trait HasModule {
515 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
518 impl HasModule for AssocContainerId {
519 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
521 AssocContainerId::ModuleId(it) => it,
522 AssocContainerId::ImplId(it) => it.lookup(db).container,
523 AssocContainerId::TraitId(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
600 impl HasModule for StaticLoc {
601 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
607 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
609 /// Returns `None` if `self` refers to a primitive type.
610 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
612 ModuleDefId::ModuleId(id) => *id,
613 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
614 ModuleDefId::AdtId(id) => id.module(db),
615 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
616 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
617 ModuleDefId::StaticId(id) => id.lookup(db).container,
618 ModuleDefId::TraitId(id) => id.lookup(db).container,
619 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
620 ModuleDefId::BuiltinType(_) => return None,
626 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
628 AttrDefId::ModuleId(it) => it.krate,
629 AttrDefId::FieldId(it) => it.parent.module(db).krate,
630 AttrDefId::AdtId(it) => it.module(db).krate,
631 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
632 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
633 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
634 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
635 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
636 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
637 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
638 AttrDefId::GenericParamId(it) => {
640 GenericParamId::TypeParamId(it) => it.parent,
641 GenericParamId::LifetimeParamId(it) => it.parent,
642 GenericParamId::ConstParamId(it) => it.parent,
647 // FIXME: `MacroDefId` should store the defining module, then this can implement
649 AttrDefId::MacroDefId(it) => it.krate,
654 /// A helper trait for converting to MacroCallId
655 pub trait AsMacroCall {
658 db: &dyn db::DefDatabase,
660 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
661 ) -> Option<MacroCallId> {
662 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
665 fn as_call_id_with_errors(
667 db: &dyn db::DefDatabase,
669 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
670 error_sink: &mut dyn FnMut(mbe::ExpandError),
671 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
674 impl AsMacroCall for InFile<&ast::MacroCall> {
675 fn as_call_id_with_errors(
677 db: &dyn db::DefDatabase,
679 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
680 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
681 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
682 let fragment = hir_expand::to_fragment_kind(self.value);
683 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
684 let h = Hygiene::new(db.upcast(), self.file_id);
685 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
687 let path = match error_sink
688 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
692 return Ok(Err(error));
696 macro_call_as_call_id(
697 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
707 /// Helper wrapper for `AstId` with `ModPath`
708 #[derive(Clone, Debug, Eq, PartialEq)]
709 struct AstIdWithPath<T: ast::AstNode> {
714 impl<T: ast::AstNode> AstIdWithPath<T> {
715 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
716 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
721 pub struct UnresolvedMacro {
725 fn macro_call_as_call_id(
726 call: &AstIdWithPath<ast::MacroCall>,
727 fragment: FragmentKind,
728 db: &dyn db::DefDatabase,
730 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
731 error_sink: &mut dyn FnMut(mbe::ExpandError),
732 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
733 let def: MacroDefId =
734 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
736 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
737 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
738 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
745 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
748 .map(MacroCallId::from)
750 Ok(def.as_lazy_macro(
753 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
759 fn derive_macro_as_call_id(
760 item_attr: &AstIdWithPath<ast::Item>,
762 db: &dyn db::DefDatabase,
764 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
765 ) -> Result<MacroCallId, UnresolvedMacro> {
766 let def: MacroDefId = resolver(item_attr.path.clone())
767 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
768 let last_segment = item_attr
772 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
773 let res = def.as_lazy_macro(
776 MacroCallKind::Derive {
777 ast_id: item_attr.ast_id,
778 derive_name: last_segment.to_string(),
779 derive_attr_index: derive_attr.ast_index,
785 fn attr_macro_as_call_id(
786 item_attr: &AstIdWithPath<ast::Item>,
788 db: &dyn db::DefDatabase,
790 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
791 ) -> Result<MacroCallId, UnresolvedMacro> {
792 let def: MacroDefId = resolver(item_attr.path.clone())
793 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
794 let last_segment = item_attr
798 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
799 let mut arg = match ¯o_attr.input {
800 Some(input) => match &**input {
801 attr::AttrInput::Literal(_) => tt::Subtree::default(),
802 attr::AttrInput::TokenTree(tt) => tt.clone(),
804 None => tt::Subtree::default(),
806 // The parentheses are always disposed here.
807 arg.delimiter = None;
809 let res = def.as_lazy_macro(
812 MacroCallKind::Attr {
813 ast_id: item_attr.ast_id,
814 attr_name: last_segment.to_string(),
816 invoc_attr_index: macro_attr.id.ast_index,