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, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
72 use crate::attr::AttrId;
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(),
489 pub fn adt_id(self) -> AdtId {
491 VariantId::EnumVariantId(it) => it.parent.into(),
492 VariantId::StructId(it) => it.into(),
493 VariantId::UnionId(it) => it.into(),
500 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
505 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
508 pub trait HasModule {
509 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
512 impl HasModule for AssocContainerId {
513 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
515 AssocContainerId::ModuleId(it) => it,
516 AssocContainerId::ImplId(it) => it.lookup(db).container,
517 AssocContainerId::TraitId(it) => it.lookup(db).container,
522 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
523 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
524 self.container.module(db)
528 impl HasModule for AdtId {
529 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
531 AdtId::StructId(it) => it.lookup(db).container,
532 AdtId::UnionId(it) => it.lookup(db).container,
533 AdtId::EnumId(it) => it.lookup(db).container,
538 impl HasModule for VariantId {
539 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
541 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
542 VariantId::StructId(it) => it.lookup(db).container,
543 VariantId::UnionId(it) => it.lookup(db).container,
548 impl HasModule for DefWithBodyId {
549 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
551 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
552 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
553 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
559 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
561 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
562 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
563 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
568 impl HasModule for GenericDefId {
569 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
571 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
572 GenericDefId::AdtId(it) => it.module(db),
573 GenericDefId::TraitId(it) => it.lookup(db).container,
574 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
575 GenericDefId::ImplId(it) => it.lookup(db).container,
576 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
577 GenericDefId::ConstId(it) => it.lookup(db).module(db),
582 impl HasModule for StaticLoc {
583 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
589 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
591 /// Returns `None` if `self` refers to a primitive type.
592 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
594 ModuleDefId::ModuleId(id) => *id,
595 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
596 ModuleDefId::AdtId(id) => id.module(db),
597 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
598 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
599 ModuleDefId::StaticId(id) => id.lookup(db).container,
600 ModuleDefId::TraitId(id) => id.lookup(db).container,
601 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
602 ModuleDefId::BuiltinType(_) => return None,
608 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
610 AttrDefId::ModuleId(it) => it.krate,
611 AttrDefId::FieldId(it) => it.parent.module(db).krate,
612 AttrDefId::AdtId(it) => it.module(db).krate,
613 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
614 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
615 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
616 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
617 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
618 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
619 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
620 AttrDefId::GenericParamId(it) => {
622 GenericParamId::TypeParamId(it) => it.parent,
623 GenericParamId::LifetimeParamId(it) => it.parent,
624 GenericParamId::ConstParamId(it) => it.parent,
629 // FIXME: `MacroDefId` should store the defining module, then this can implement
631 AttrDefId::MacroDefId(it) => it.krate,
636 /// A helper trait for converting to MacroCallId
637 pub trait AsMacroCall {
640 db: &dyn db::DefDatabase,
642 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
643 ) -> Option<MacroCallId> {
644 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
647 fn as_call_id_with_errors(
649 db: &dyn db::DefDatabase,
651 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
652 error_sink: &mut dyn FnMut(mbe::ExpandError),
653 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
656 impl AsMacroCall for InFile<&ast::MacroCall> {
657 fn as_call_id_with_errors(
659 db: &dyn db::DefDatabase,
661 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
662 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
663 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
664 let fragment = hir_expand::to_fragment_kind(self.value);
665 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
666 let h = Hygiene::new(db.upcast(), self.file_id);
667 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
669 let path = match error_sink
670 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
674 return Ok(Err(error));
678 macro_call_as_call_id(
679 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
689 /// Helper wrapper for `AstId` with `ModPath`
690 #[derive(Clone, Debug, Eq, PartialEq)]
691 struct AstIdWithPath<T: ast::AstNode> {
696 impl<T: ast::AstNode> AstIdWithPath<T> {
697 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
698 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
703 pub struct UnresolvedMacro {
707 fn macro_call_as_call_id(
708 call: &AstIdWithPath<ast::MacroCall>,
709 fragment: FragmentKind,
710 db: &dyn db::DefDatabase,
712 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
713 error_sink: &mut dyn FnMut(mbe::ExpandError),
714 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
715 let def: MacroDefId =
716 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
718 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
719 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
720 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
727 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
730 .map(MacroCallId::from)
736 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
743 fn derive_macro_as_call_id(
744 item_attr: &AstIdWithPath<ast::Item>,
746 db: &dyn db::DefDatabase,
748 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
749 ) -> Result<MacroCallId, UnresolvedMacro> {
750 let def: MacroDefId = resolver(item_attr.path.clone())
751 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
752 let last_segment = item_attr
756 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
761 MacroCallKind::Derive {
762 ast_id: item_attr.ast_id,
763 derive_name: last_segment.to_string(),
764 derive_attr_index: derive_attr.ast_index,