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, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
77 builtin_type::BuiltinType,
79 Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
84 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
88 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
89 /// `DefMap` of `krate`.
90 block: Option<BlockId>,
91 /// The module's ID in its originating `DefMap`.
92 pub local_id: LocalModuleId,
96 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
99 db.block_def_map(block).unwrap_or_else(|| {
100 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
101 // so the `DefMap` here must exist.
102 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
105 None => db.crate_def_map(self.krate),
109 pub fn krate(&self) -> CrateId {
113 pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
114 self.def_map(db).containing_module(self.local_id)
117 pub fn containing_block(&self) -> Option<BlockId> {
122 /// An ID of a module, **local** to a specific crate
123 pub type LocalModuleId = Idx<nameres::ModuleData>;
126 pub struct ItemLoc<N: ItemTreeNode> {
127 pub container: ModuleId,
128 pub id: ItemTreeId<N>,
131 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
132 fn clone(&self) -> Self {
133 Self { container: self.container, id: self.id }
137 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
139 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
140 fn eq(&self, other: &Self) -> bool {
141 self.container == other.container && self.id == other.id
145 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
147 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
148 fn hash<H: Hasher>(&self, state: &mut H) {
149 self.container.hash(state);
155 pub struct AssocItemLoc<N: ItemTreeNode> {
156 pub container: AssocContainerId,
157 pub id: ItemTreeId<N>,
160 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
161 fn clone(&self) -> Self {
162 Self { container: self.container, id: self.id }
166 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
168 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
169 fn eq(&self, other: &Self) -> bool {
170 self.container == other.container && self.id == other.id
174 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
176 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
177 fn hash<H: Hasher>(&self, state: &mut H) {
178 self.container.hash(state);
183 macro_rules! impl_intern {
184 ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
185 impl_intern_key!($id);
187 impl Intern for $loc {
189 fn intern(self, db: &dyn db::DefDatabase) -> $id {
194 impl Lookup for $id {
196 fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
203 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
204 pub struct FunctionId(salsa::InternId);
205 type FunctionLoc = AssocItemLoc<Function>;
206 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
208 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
209 pub struct StructId(salsa::InternId);
210 type StructLoc = ItemLoc<Struct>;
211 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
213 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
214 pub struct UnionId(salsa::InternId);
215 pub type UnionLoc = ItemLoc<Union>;
216 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
218 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
219 pub struct EnumId(salsa::InternId);
220 pub type EnumLoc = ItemLoc<Enum>;
221 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
223 // FIXME: rename to `VariantId`, only enums can ave variants
224 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
225 pub struct EnumVariantId {
227 pub local_id: LocalEnumVariantId,
230 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
232 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
234 pub parent: VariantId,
235 pub local_id: LocalFieldId,
238 pub type LocalFieldId = Idx<adt::FieldData>;
240 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
241 pub struct ConstId(salsa::InternId);
242 type ConstLoc = AssocItemLoc<Const>;
243 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
245 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
246 pub struct StaticId(salsa::InternId);
247 pub type StaticLoc = ItemLoc<Static>;
248 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
250 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
251 pub struct TraitId(salsa::InternId);
252 pub type TraitLoc = ItemLoc<Trait>;
253 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
255 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
256 pub struct TypeAliasId(salsa::InternId);
257 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
258 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
260 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
261 pub struct ImplId(salsa::InternId);
262 type ImplLoc = ItemLoc<Impl>;
263 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
265 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
266 pub struct BlockId(salsa::InternId);
267 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
268 pub struct BlockLoc {
269 ast_id: AstId<ast::BlockExpr>,
270 /// The containing module.
273 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
275 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
276 pub struct TypeParamId {
277 pub parent: GenericDefId,
278 pub local_id: LocalTypeParamId,
281 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
283 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
284 pub struct LifetimeParamId {
285 pub parent: GenericDefId,
286 pub local_id: LocalLifetimeParamId,
288 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
290 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
291 pub struct ConstParamId {
292 pub parent: GenericDefId,
293 pub local_id: LocalConstParamId,
295 pub type LocalConstParamId = Idx<generics::ConstParamData>;
297 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298 pub enum AssocContainerId {
303 impl_from!(ModuleId for AssocContainerId);
306 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
312 impl_from!(StructId, UnionId, EnumId for AdtId);
315 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
316 pub enum GenericParamId {
317 TypeParamId(TypeParamId),
318 LifetimeParamId(LifetimeParamId),
319 ConstParamId(ConstParamId),
321 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
323 /// The defs which can be visible in the module.
324 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
325 pub enum ModuleDefId {
327 FunctionId(FunctionId),
329 // Can't be directly declared, but can be imported.
330 EnumVariantId(EnumVariantId),
334 TypeAliasId(TypeAliasId),
335 BuiltinType(BuiltinType),
340 AdtId(StructId, EnumId, UnionId),
350 /// The defs which have a body.
351 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
352 pub enum DefWithBodyId {
353 FunctionId(FunctionId),
358 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
361 pub fn as_generic_def_id(self) -> Option<GenericDefId> {
363 DefWithBodyId::FunctionId(f) => Some(f.into()),
364 DefWithBodyId::StaticId(_) => None,
365 DefWithBodyId::ConstId(c) => Some(c.into()),
370 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
371 pub enum AssocItemId {
372 FunctionId(FunctionId),
374 TypeAliasId(TypeAliasId),
376 // FIXME: not every function, ... is actually an assoc item. maybe we should make
377 // sure that you can only turn actual assoc items into AssocItemIds. This would
378 // require not implementing From, and instead having some checked way of
379 // casting them, and somehow making the constructors private, which would be annoying.
380 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
382 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
383 pub enum GenericDefId {
384 FunctionId(FunctionId),
387 TypeAliasId(TypeAliasId),
389 // enum variants cannot have generics themselves, but their parent enums
390 // can, and this makes some code easier to write
391 EnumVariantId(EnumVariantId),
392 // consts can have type parameters from their parents (i.e. associated consts of traits)
397 AdtId(StructId, EnumId, UnionId),
406 impl From<AssocItemId> for GenericDefId {
407 fn from(item: AssocItemId) -> Self {
409 AssocItemId::FunctionId(f) => f.into(),
410 AssocItemId::ConstId(c) => c.into(),
411 AssocItemId::TypeAliasId(t) => t.into(),
416 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
421 FunctionId(FunctionId),
422 EnumVariantId(EnumVariantId),
426 TypeAliasId(TypeAliasId),
427 MacroDefId(MacroDefId),
429 GenericParamId(GenericParamId),
435 AdtId(StructId, EnumId, UnionId),
448 impl From<AssocContainerId> for AttrDefId {
449 fn from(acid: AssocContainerId) -> Self {
451 AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
452 AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
453 AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
458 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
460 EnumVariantId(EnumVariantId),
464 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
467 pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
469 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
470 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
471 VariantId::EnumVariantId(it) => {
472 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
477 pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
479 VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
480 VariantId::StructId(it) => it.lookup(db).id.file_id(),
481 VariantId::UnionId(it) => it.lookup(db).id.file_id(),
485 pub fn adt_id(self) -> AdtId {
487 VariantId::EnumVariantId(it) => it.parent.into(),
488 VariantId::StructId(it) => it.into(),
489 VariantId::UnionId(it) => it.into(),
496 fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
501 fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
504 pub trait HasModule {
505 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
508 impl HasModule for AssocContainerId {
509 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
511 AssocContainerId::ModuleId(it) => it,
512 AssocContainerId::ImplId(it) => it.lookup(db).container,
513 AssocContainerId::TraitId(it) => it.lookup(db).container,
518 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
519 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
520 self.container.module(db)
524 impl HasModule for AdtId {
525 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
527 AdtId::StructId(it) => it.lookup(db).container,
528 AdtId::UnionId(it) => it.lookup(db).container,
529 AdtId::EnumId(it) => it.lookup(db).container,
534 impl HasModule for VariantId {
535 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
537 VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
538 VariantId::StructId(it) => it.lookup(db).container,
539 VariantId::UnionId(it) => it.lookup(db).container,
544 impl HasModule for DefWithBodyId {
545 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
547 DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
548 DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
549 DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
555 pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
557 DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
558 DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
559 DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
564 impl HasModule for GenericDefId {
565 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
567 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
568 GenericDefId::AdtId(it) => it.module(db),
569 GenericDefId::TraitId(it) => it.lookup(db).container,
570 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
571 GenericDefId::ImplId(it) => it.lookup(db).container,
572 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
573 GenericDefId::ConstId(it) => it.lookup(db).module(db),
578 impl HasModule for TypeAliasId {
579 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
580 self.lookup(db).module(db)
584 impl HasModule for TraitId {
585 fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
586 self.lookup(db).container
590 impl HasModule for StaticLoc {
591 fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
597 /// Returns the module containing `self` (or `self`, if `self` is itself a module).
599 /// Returns `None` if `self` refers to a primitive type.
600 pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
602 ModuleDefId::ModuleId(id) => *id,
603 ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
604 ModuleDefId::AdtId(id) => id.module(db),
605 ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
606 ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
607 ModuleDefId::StaticId(id) => id.lookup(db).container,
608 ModuleDefId::TraitId(id) => id.lookup(db).container,
609 ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
610 ModuleDefId::BuiltinType(_) => return None,
616 pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
618 AttrDefId::ModuleId(it) => it.krate,
619 AttrDefId::FieldId(it) => it.parent.module(db).krate,
620 AttrDefId::AdtId(it) => it.module(db).krate,
621 AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
622 AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
623 AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
624 AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
625 AttrDefId::TraitId(it) => it.lookup(db).container.krate,
626 AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
627 AttrDefId::ImplId(it) => it.lookup(db).container.krate,
628 AttrDefId::GenericParamId(it) => {
630 GenericParamId::TypeParamId(it) => it.parent,
631 GenericParamId::LifetimeParamId(it) => it.parent,
632 GenericParamId::ConstParamId(it) => it.parent,
637 // FIXME: `MacroDefId` should store the defining module, then this can implement
639 AttrDefId::MacroDefId(it) => it.krate,
644 /// A helper trait for converting to MacroCallId
645 pub trait AsMacroCall {
648 db: &dyn db::DefDatabase,
650 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
651 ) -> Option<MacroCallId> {
652 self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
655 fn as_call_id_with_errors(
657 db: &dyn db::DefDatabase,
659 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
660 error_sink: &mut dyn FnMut(mbe::ExpandError),
661 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
664 impl AsMacroCall for InFile<&ast::MacroCall> {
665 fn as_call_id_with_errors(
667 db: &dyn db::DefDatabase,
669 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
670 mut error_sink: &mut dyn FnMut(mbe::ExpandError),
671 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
672 let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
673 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
674 let h = Hygiene::new(db.upcast(), self.file_id);
675 let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
677 let path = match error_sink
678 .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
682 return Ok(Err(error));
686 macro_call_as_call_id(
687 &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
697 /// Helper wrapper for `AstId` with `ModPath`
698 #[derive(Clone, Debug, Eq, PartialEq)]
699 struct AstIdWithPath<T: ast::AstNode> {
704 impl<T: ast::AstNode> AstIdWithPath<T> {
705 fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
706 AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
711 pub struct UnresolvedMacro {
715 fn macro_call_as_call_id(
716 call: &AstIdWithPath<ast::MacroCall>,
718 db: &dyn db::DefDatabase,
720 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
721 error_sink: &mut dyn FnMut(mbe::ExpandError),
722 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
723 let def: MacroDefId =
724 resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
726 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
727 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
728 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
735 &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
739 Ok(def.as_lazy_macro(
742 MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
748 fn derive_macro_as_call_id(
749 item_attr: &AstIdWithPath<ast::Item>,
751 db: &dyn db::DefDatabase,
753 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
754 ) -> Result<MacroCallId, UnresolvedMacro> {
755 let def: MacroDefId = resolver(item_attr.path.clone())
756 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
757 let last_segment = item_attr
761 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
762 let res = def.as_lazy_macro(
765 MacroCallKind::Derive {
766 ast_id: item_attr.ast_id,
767 derive_name: last_segment.to_string().into_boxed_str(),
768 derive_attr_index: derive_attr.ast_index,
774 fn attr_macro_as_call_id(
775 item_attr: &AstIdWithPath<ast::Item>,
777 db: &dyn db::DefDatabase,
779 resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
780 ) -> Result<MacroCallId, UnresolvedMacro> {
781 let def: MacroDefId = resolver(item_attr.path.clone())
782 .filter(MacroDefId::is_attribute)
783 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
784 let last_segment = item_attr
788 .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
789 let mut arg = match ¯o_attr.input {
790 Some(input) => match &**input {
791 attr::AttrInput::Literal(_) => Default::default(),
792 attr::AttrInput::TokenTree(tt, map) => (tt.clone(), map.clone()),
794 None => Default::default(),
796 // The parentheses are always disposed here.
797 arg.0.delimiter = None;
799 let res = def.as_lazy_macro(
802 MacroCallKind::Attr {
803 ast_id: item_attr.ast_id,
804 attr_name: last_segment.to_string().into_boxed_str(),
806 invoc_attr_index: macro_attr.id.ast_index,