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, ExpandTo, 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> {
120 /// An ID of a module, **local** to a specific crate
121 pub type LocalModuleId = Idx<nameres::ModuleData>;
124 pub struct ItemLoc<N: ItemTreeNode> {
125 pub container: ModuleId,
126 pub id: ItemTreeId<N>,
129 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
130 fn clone(&self) -> Self {
131 Self { container: self.container, id: self.id }
135 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
137 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
138 fn eq(&self, other: &Self) -> bool {
139 self.container == other.container && self.id == other.id
143 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
145 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
146 fn hash<H: Hasher>(&self, state: &mut H) {
147 self.container.hash(state);
153 pub struct AssocItemLoc<N: ItemTreeNode> {
154 pub container: AssocContainerId,
155 pub id: ItemTreeId<N>,
158 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
159 fn clone(&self) -> Self {
160 Self { container: self.container, id: self.id }
164 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
166 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
167 fn eq(&self, other: &Self) -> bool {
168 self.container == other.container && self.id == other.id
172 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
174 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
175 fn hash<H: Hasher>(&self, state: &mut H) {
176 self.container.hash(state);
181 macro_rules! impl_intern {
182 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
183 impl_intern_key!($id);
185 impl Intern for $loc {
187 fn intern(self, db: &dyn db::DefDatabase) -> $id {
192 impl Lookup for $id {
194 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
201 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
202 pub struct FunctionId(salsa::InternId);
203 type FunctionLoc = AssocItemLoc<Function>;
204 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
206 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
207 pub struct StructId(salsa::InternId);
208 type StructLoc = ItemLoc<Struct>;
209 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
211 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
212 pub struct UnionId(salsa::InternId);
213 pub type UnionLoc = ItemLoc<Union>;
214 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
216 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
217 pub struct EnumId(salsa::InternId);
218 pub type EnumLoc = ItemLoc<Enum>;
219 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
221 // FIXME: rename to `VariantId`, only enums can ave variants
222 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
223 pub struct EnumVariantId {
225 pub local_id: LocalEnumVariantId,
228 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
230 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232 pub parent: VariantId,
233 pub local_id: LocalFieldId,
236 pub type LocalFieldId = Idx<adt::FieldData>;
238 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
239 pub struct ConstId(salsa::InternId);
240 type ConstLoc = AssocItemLoc<Const>;
241 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
243 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
244 pub struct StaticId(salsa::InternId);
245 pub type StaticLoc = ItemLoc<Static>;
246 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
248 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
249 pub struct TraitId(salsa::InternId);
250 pub type TraitLoc = ItemLoc<Trait>;
251 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
253 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254 pub struct TypeAliasId(salsa::InternId);
255 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
256 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
258 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
259 pub struct ImplId(salsa::InternId);
260 type ImplLoc = ItemLoc<Impl>;
261 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
263 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
264 pub struct BlockId(salsa::InternId);
265 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
266 pub struct BlockLoc {
267 ast_id: AstId<ast::BlockExpr>,
268 /// The containing module.
271 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
273 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
274 pub struct TypeParamId {
275 pub parent: GenericDefId,
276 pub local_id: LocalTypeParamId,
279 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
281 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
282 pub struct LifetimeParamId {
283 pub parent: GenericDefId,
284 pub local_id: LocalLifetimeParamId,
286 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
288 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
289 pub struct ConstParamId {
290 pub parent: GenericDefId,
291 pub local_id: LocalConstParamId,
293 pub type LocalConstParamId = Idx<generics::ConstParamData>;
295 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
296 pub enum AssocContainerId {
301 impl_from!(ModuleId for AssocContainerId);
304 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
310 impl_from!(StructId, UnionId, EnumId for AdtId);
313 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
314 pub enum GenericParamId {
315 TypeParamId(TypeParamId),
316 LifetimeParamId(LifetimeParamId),
317 ConstParamId(ConstParamId),
319 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
321 /// The defs which can be visible in the module.
322 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
323 pub enum ModuleDefId {
325 FunctionId(FunctionId),
327 // Can't be directly declared, but can be imported.
328 EnumVariantId(EnumVariantId),
332 TypeAliasId(TypeAliasId),
333 BuiltinType(BuiltinType),
338 AdtId(StructId, EnumId, UnionId),
348 /// The defs which have a body.
349 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
350 pub enum DefWithBodyId {
351 FunctionId(FunctionId),
356 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
359 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
361 DefWithBodyId::FunctionId(f) => Some(f.into()),
362 DefWithBodyId::StaticId(_) => None,
363 DefWithBodyId::ConstId(c) => Some(c.into()),
368 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
369 pub enum AssocItemId {
370 FunctionId(FunctionId),
372 TypeAliasId(TypeAliasId),
374 // FIXME: not every function, ... is actually an assoc item. maybe we should make
375 // sure that you can only turn actual assoc items into AssocItemIds. This would
376 // require not implementing From, and instead having some checked way of
377 // casting them, and somehow making the constructors private, which would be annoying.
378 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
380 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
381 pub enum GenericDefId {
382 FunctionId(FunctionId),
385 TypeAliasId(TypeAliasId),
387 // enum variants cannot have generics themselves, but their parent enums
388 // can, and this makes some code easier to write
389 EnumVariantId(EnumVariantId),
390 // consts can have type parameters from their parents (i.e. associated consts of traits)
395 AdtId(StructId, EnumId, UnionId),
404 impl From<AssocItemId> for GenericDefId {
405 fn from(item: AssocItemId) -> Self {
407 AssocItemId::FunctionId(f) => f.into(),
408 AssocItemId::ConstId(c) => c.into(),
409 AssocItemId::TypeAliasId(t) => t.into(),
414 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
419 FunctionId(FunctionId),
420 EnumVariantId(EnumVariantId),
424 TypeAliasId(TypeAliasId),
425 MacroDefId(MacroDefId),
427 GenericParamId(GenericParamId),
433 AdtId(StructId, EnumId, UnionId),
446 impl From<AssocContainerId> for AttrDefId {
447 fn from(acid: AssocContainerId) -> Self {
449 AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
450 AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
451 AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
456 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
458 EnumVariantId(EnumVariantId),
462 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
465 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
467 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
468 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
469 VariantId::EnumVariantId(it) => {
470 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
475 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
477 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
478 VariantId::StructId(it) => it.lookup(db).id.file_id(),
479 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
483 pub fn adt_id(self) -> AdtId {
485 VariantId::EnumVariantId(it) => it.parent.into(),
486 VariantId::StructId(it) => it.into(),
487 VariantId::UnionId(it) => it.into(),
494 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
499 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
502 pub trait HasModule {
503 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
506 impl HasModule for AssocContainerId {
507 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
509 AssocContainerId::ModuleId(it) => it,
510 AssocContainerId::ImplId(it) => it.lookup(db).container,
511 AssocContainerId::TraitId(it) => it.lookup(db).container,
516 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
517 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
518 self.container.module(db)
522 impl HasModule for AdtId {
523 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
525 AdtId::StructId(it) => it.lookup(db).container,
526 AdtId::UnionId(it) => it.lookup(db).container,
527 AdtId::EnumId(it) => it.lookup(db).container,
532 impl HasModule for VariantId {
533 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
535 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
536 VariantId::StructId(it) => it.lookup(db).container,
537 VariantId::UnionId(it) => it.lookup(db).container,
542 impl HasModule for DefWithBodyId {
543 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
545 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
546 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
547 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
553 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
555 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
556 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
557 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
562 impl HasModule for GenericDefId {
563 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
565 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
566 GenericDefId::AdtId(it) => it.module(db),
567 GenericDefId::TraitId(it) => it.lookup(db).container,
568 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
569 GenericDefId::ImplId(it) => it.lookup(db).container,
570 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
571 GenericDefId::ConstId(it) => it.lookup(db).module(db),
576 impl HasModule for TypeAliasId {
577 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
578 self.lookup(db).module(db)
582 impl HasModule for TraitId {
583 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
584 self.lookup(db).container
588 impl HasModule for StaticLoc {
589 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
595 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
597 /// Returns `None` if `self` refers to a primitive type.
598 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
600 ModuleDefId::ModuleId(id) => *id,
601 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
602 ModuleDefId::AdtId(id) => id.module(db),
603 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
604 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
605 ModuleDefId::StaticId(id) => id.lookup(db).container,
606 ModuleDefId::TraitId(id) => id.lookup(db).container,
607 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
608 ModuleDefId::BuiltinType(_) => return None,
614 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
616 AttrDefId::ModuleId(it) => it.krate,
617 AttrDefId::FieldId(it) => it.parent.module(db).krate,
618 AttrDefId::AdtId(it) => it.module(db).krate,
619 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
620 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
621 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
622 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
623 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
624 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
625 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
626 AttrDefId::GenericParamId(it) => {
628 GenericParamId::TypeParamId(it) => it.parent,
629 GenericParamId::LifetimeParamId(it) => it.parent,
630 GenericParamId::ConstParamId(it) => it.parent,
635 // FIXME: `MacroDefId` should store the defining module, then this can implement
637 AttrDefId::MacroDefId(it) => it.krate,
642 /// A helper trait for converting to MacroCallId
643 pub trait AsMacroCall {
646 db: &dyn db::DefDatabase,
648 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
649 ) -> Option<MacroCallId> {
650 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
653 fn as_call_id_with_errors(
655 db: &dyn db::DefDatabase,
657 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
658 error_sink: &mut dyn FnMut(mbe::ExpandError),
659 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
662 impl AsMacroCall for InFile<&ast::MacroCall> {
663 fn as_call_id_with_errors(
665 db: &dyn db::DefDatabase,
667 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
668 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
669 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
670 let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
671 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
672 let h = Hygiene::new(db.upcast(), self.file_id);
673 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
675 let path = match error_sink
676 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
680 return Ok(Err(error));
684 macro_call_as_call_id(
685 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
695 /// Helper wrapper for `AstId` with `ModPath`
696 #[derive(Clone, Debug, Eq, PartialEq)]
697 struct AstIdWithPath<T: ast::AstNode> {
702 impl<T: ast::AstNode> AstIdWithPath<T> {
703 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
704 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
709 pub struct UnresolvedMacro {
713 fn macro_call_as_call_id(
714 call: &AstIdWithPath<ast::MacroCall>,
716 db: &dyn db::DefDatabase,
718 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
719 error_sink: &mut dyn FnMut(mbe::ExpandError),
720 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
721 let def: MacroDefId =
722 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
724 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
725 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
726 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
733 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
737 Ok(def.as_lazy_macro(
740 MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
746 fn derive_macro_as_call_id(
747 item_attr: &AstIdWithPath<ast::Item>,
749 db: &dyn db::DefDatabase,
751 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
752 ) -> Result<MacroCallId, UnresolvedMacro> {
753 let def: MacroDefId = resolver(item_attr.path.clone())
754 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
755 let last_segment = item_attr
759 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
760 let res = def.as_lazy_macro(
763 MacroCallKind::Derive {
764 ast_id: item_attr.ast_id,
765 derive_name: last_segment.to_string(),
766 derive_attr_index: derive_attr.ast_index,
772 fn attr_macro_as_call_id(
773 item_attr: &AstIdWithPath<ast::Item>,
775 db: &dyn db::DefDatabase,
777 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
778 ) -> Result<MacroCallId, UnresolvedMacro> {
779 let def: MacroDefId = resolver(item_attr.path.clone())
780 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
781 let last_segment = item_attr
785 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
786 let mut arg = match ¯o_attr.input {
787 Some(input) => match &**input {
788 attr::AttrInput::Literal(_) => Default::default(),
789 attr::AttrInput::TokenTree(tt, map) => (tt.clone(), map.clone()),
791 None => Default::default(),
793 // The parentheses are always disposed here.
794 arg.0.delimiter = None;
796 let res = def.as_lazy_macro(
799 MacroCallKind::Attr {
800 ast_id: item_attr.ast_id,
801 attr_name: last_segment.to_string(),
803 invoc_attr_index: macro_attr.id.ast_index,