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, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
72 use crate::builtin_type::BuiltinType;
74 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
79 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
83 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
84 /// `DefMap` of `krate`.
85 block: Option<BlockId>,
86 /// The module's ID in its originating `DefMap`.
87 pub local_id: LocalModuleId,
91 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
94 db.block_def_map(block).unwrap_or_else(|| {
95 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
96 // so the `DefMap` here must exist.
97 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
100 None => db.crate_def_map(self.krate),
104 pub fn krate(&self) -> CrateId {
108 pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
109 self.def_map(db).containing_module(self.local_id)
113 /// An ID of a module, **local** to a specific crate
114 pub type LocalModuleId = Idx<nameres::ModuleData>;
117 pub struct ItemLoc<N: ItemTreeNode> {
118 pub container: ModuleId,
119 pub id: ItemTreeId<N>,
122 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
123 fn clone(&self) -> Self {
124 Self { container: self.container, id: self.id }
128 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
130 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
131 fn eq(&self, other: &Self) -> bool {
132 self.container == other.container && self.id == other.id
136 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
138 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
139 fn hash<H: Hasher>(&self, state: &mut H) {
140 self.container.hash(state);
146 pub struct AssocItemLoc<N: ItemTreeNode> {
147 pub container: AssocContainerId,
148 pub id: ItemTreeId<N>,
151 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
152 fn clone(&self) -> Self {
153 Self { container: self.container, id: self.id }
157 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
159 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
160 fn eq(&self, other: &Self) -> bool {
161 self.container == other.container && self.id == other.id
165 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
167 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
168 fn hash<H: Hasher>(&self, state: &mut H) {
169 self.container.hash(state);
174 macro_rules! impl_intern {
175 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
176 impl_intern_key!($id);
178 impl Intern for $loc {
180 fn intern(self, db: &dyn db::DefDatabase) -> $id {
185 impl Lookup for $id {
187 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
194 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
195 pub struct FunctionId(salsa::InternId);
196 type FunctionLoc = AssocItemLoc<Function>;
197 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
199 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
200 pub struct StructId(salsa::InternId);
201 type StructLoc = ItemLoc<Struct>;
202 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
204 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
205 pub struct UnionId(salsa::InternId);
206 pub type UnionLoc = ItemLoc<Union>;
207 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
209 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
210 pub struct EnumId(salsa::InternId);
211 pub type EnumLoc = ItemLoc<Enum>;
212 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
214 // FIXME: rename to `VariantId`, only enums can ave variants
215 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
216 pub struct EnumVariantId {
218 pub local_id: LocalEnumVariantId,
221 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
223 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
225 pub parent: VariantId,
226 pub local_id: LocalFieldId,
229 pub type LocalFieldId = Idx<adt::FieldData>;
231 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232 pub struct ConstId(salsa::InternId);
233 type ConstLoc = AssocItemLoc<Const>;
234 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
236 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
237 pub struct StaticId(salsa::InternId);
238 pub type StaticLoc = ItemLoc<Static>;
239 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
241 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
242 pub struct TraitId(salsa::InternId);
243 pub type TraitLoc = ItemLoc<Trait>;
244 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
246 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
247 pub struct TypeAliasId(salsa::InternId);
248 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
249 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
251 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
252 pub struct ImplId(salsa::InternId);
253 type ImplLoc = ItemLoc<Impl>;
254 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
256 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
257 pub struct BlockId(salsa::InternId);
258 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
259 pub struct BlockLoc {
260 ast_id: AstId<ast::BlockExpr>,
261 /// The containing module.
264 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
266 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
267 pub struct TypeParamId {
268 pub parent: GenericDefId,
269 pub local_id: LocalTypeParamId,
272 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
274 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
275 pub struct LifetimeParamId {
276 pub parent: GenericDefId,
277 pub local_id: LocalLifetimeParamId,
279 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
281 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
282 pub struct ConstParamId {
283 pub parent: GenericDefId,
284 pub local_id: LocalConstParamId,
286 pub type LocalConstParamId = Idx<generics::ConstParamData>;
288 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
289 pub enum AssocContainerId {
294 impl_from!(ModuleId for AssocContainerId);
297 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
303 impl_from!(StructId, UnionId, EnumId for AdtId);
306 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
307 pub enum GenericParamId {
308 TypeParamId(TypeParamId),
309 LifetimeParamId(LifetimeParamId),
310 ConstParamId(ConstParamId),
312 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
314 /// The defs which can be visible in the module.
315 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
316 pub enum ModuleDefId {
318 FunctionId(FunctionId),
320 // Can't be directly declared, but can be imported.
321 EnumVariantId(EnumVariantId),
325 TypeAliasId(TypeAliasId),
326 BuiltinType(BuiltinType),
331 AdtId(StructId, EnumId, UnionId),
341 /// The defs which have a body.
342 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
343 pub enum DefWithBodyId {
344 FunctionId(FunctionId),
349 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
352 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
354 DefWithBodyId::FunctionId(f) => Some(f.into()),
355 DefWithBodyId::StaticId(_) => None,
356 DefWithBodyId::ConstId(c) => Some(c.into()),
361 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
362 pub enum AssocItemId {
363 FunctionId(FunctionId),
365 TypeAliasId(TypeAliasId),
367 // FIXME: not every function, ... is actually an assoc item. maybe we should make
368 // sure that you can only turn actual assoc items into AssocItemIds. This would
369 // require not implementing From, and instead having some checked way of
370 // casting them, and somehow making the constructors private, which would be annoying.
371 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
373 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
374 pub enum GenericDefId {
375 FunctionId(FunctionId),
378 TypeAliasId(TypeAliasId),
380 // enum variants cannot have generics themselves, but their parent enums
381 // can, and this makes some code easier to write
382 EnumVariantId(EnumVariantId),
383 // consts can have type parameters from their parents (i.e. associated consts of traits)
388 AdtId(StructId, EnumId, UnionId),
397 impl From<AssocItemId> for GenericDefId {
398 fn from(item: AssocItemId) -> Self {
400 AssocItemId::FunctionId(f) => f.into(),
401 AssocItemId::ConstId(c) => c.into(),
402 AssocItemId::TypeAliasId(t) => t.into(),
407 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
412 FunctionId(FunctionId),
413 EnumVariantId(EnumVariantId),
417 TypeAliasId(TypeAliasId),
418 MacroDefId(MacroDefId),
420 GenericParamId(GenericParamId),
426 AdtId(StructId, EnumId, UnionId),
439 impl From<AssocContainerId> for AttrDefId {
440 fn from(acid: AssocContainerId) -> Self {
442 AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
443 AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
444 AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
449 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
451 EnumVariantId(EnumVariantId),
455 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
458 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
460 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
461 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
462 VariantId::EnumVariantId(it) => {
463 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
468 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
470 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
471 VariantId::StructId(it) => it.lookup(db).id.file_id(),
472 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
479 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
484 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
487 pub trait HasModule {
488 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
491 impl HasModule for AssocContainerId {
492 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
494 AssocContainerId::ModuleId(it) => it,
495 AssocContainerId::ImplId(it) => it.lookup(db).container,
496 AssocContainerId::TraitId(it) => it.lookup(db).container,
501 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
502 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
503 self.container.module(db)
507 impl HasModule for AdtId {
508 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
510 AdtId::StructId(it) => it.lookup(db).container,
511 AdtId::UnionId(it) => it.lookup(db).container,
512 AdtId::EnumId(it) => it.lookup(db).container,
517 impl HasModule for VariantId {
518 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
520 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
521 VariantId::StructId(it) => it.lookup(db).container,
522 VariantId::UnionId(it) => it.lookup(db).container,
527 impl HasModule for DefWithBodyId {
528 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
530 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
531 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
532 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
538 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
540 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
541 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
542 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
547 impl HasModule for GenericDefId {
548 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
550 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
551 GenericDefId::AdtId(it) => it.module(db),
552 GenericDefId::TraitId(it) => it.lookup(db).container,
553 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
554 GenericDefId::ImplId(it) => it.lookup(db).container,
555 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
556 GenericDefId::ConstId(it) => it.lookup(db).module(db),
561 impl HasModule for StaticLoc {
562 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
568 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
570 /// Returns `None` if `self` refers to a primitive type.
571 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
573 ModuleDefId::ModuleId(id) => *id,
574 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
575 ModuleDefId::AdtId(id) => id.module(db),
576 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
577 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
578 ModuleDefId::StaticId(id) => id.lookup(db).container,
579 ModuleDefId::TraitId(id) => id.lookup(db).container,
580 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
581 ModuleDefId::BuiltinType(_) => return None,
587 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
589 AttrDefId::ModuleId(it) => it.krate,
590 AttrDefId::FieldId(it) => it.parent.module(db).krate,
591 AttrDefId::AdtId(it) => it.module(db).krate,
592 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
593 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
594 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
595 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
596 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
597 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
598 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
599 AttrDefId::GenericParamId(it) => {
601 GenericParamId::TypeParamId(it) => it.parent,
602 GenericParamId::LifetimeParamId(it) => it.parent,
603 GenericParamId::ConstParamId(it) => it.parent,
608 // FIXME: `MacroDefId` should store the defining module, then this can implement
610 AttrDefId::MacroDefId(it) => it.krate,
615 /// A helper trait for converting to MacroCallId
616 pub trait AsMacroCall {
619 db: &dyn db::DefDatabase,
621 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
622 ) -> Option<MacroCallId> {
623 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
626 fn as_call_id_with_errors(
628 db: &dyn db::DefDatabase,
630 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
631 error_sink: &mut dyn FnMut(mbe::ExpandError),
632 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
635 impl AsMacroCall for InFile<&ast::MacroCall> {
636 fn as_call_id_with_errors(
638 db: &dyn db::DefDatabase,
640 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
641 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
642 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
643 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
644 let h = Hygiene::new(db.upcast(), self.file_id);
645 let path = self.value.path().and_then(|path| path::ModPath::from_src(path, &h));
647 let path = match error_sink
648 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
652 return Ok(Err(error));
656 macro_call_as_call_id(
657 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
666 /// Helper wrapper for `AstId` with `ModPath`
667 #[derive(Clone, Debug, Eq, PartialEq)]
668 struct AstIdWithPath<T: ast::AstNode> {
673 impl<T: ast::AstNode> AstIdWithPath<T> {
674 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
675 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
680 pub struct UnresolvedMacro {
684 fn macro_call_as_call_id(
685 call: &AstIdWithPath<ast::MacroCall>,
686 db: &dyn db::DefDatabase,
688 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
689 error_sink: &mut dyn FnMut(mbe::ExpandError),
690 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
691 let def: MacroDefId =
692 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
694 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
695 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
696 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
703 &|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?),
706 .map(MacroCallId::from)
709 .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id })
715 fn derive_macro_as_call_id(
716 item_attr: &AstIdWithPath<ast::Item>,
718 db: &dyn db::DefDatabase,
720 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
721 ) -> Result<MacroCallId, UnresolvedMacro> {
722 let def: MacroDefId = resolver(item_attr.path.clone())
723 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
724 let last_segment = item_attr
728 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
733 MacroCallKind::Derive {
734 ast_id: item_attr.ast_id,
735 derive_name: last_segment.to_string(),