]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/lib.rs
Merge #8853
[rust.git] / crates / hir_def / src / lib.rs
1 //! `hir_def` crate contains everything between macro expansion and type
2 //! inference.
3 //!
4 //! It defines various items (structs, enums, traits) which comprises Rust code,
5 //! as well as an algorithm for resolving paths to such entities.
6 //!
7 //! Note that `hir_def` is a work in progress, so not all of the above is
8 //! actually true.
9
10 #[allow(unused)]
11 macro_rules! eprintln {
12     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
13 }
14
15 pub mod db;
16
17 pub mod attr;
18 pub mod path;
19 pub mod type_ref;
20 pub mod builtin_type;
21 pub mod builtin_attr;
22 pub mod diagnostics;
23 pub mod per_ns;
24 pub mod item_scope;
25
26 pub mod dyn_map;
27 pub mod keys;
28
29 pub mod item_tree;
30 pub mod intern;
31
32 pub mod adt;
33 pub mod data;
34 pub mod generics;
35 pub mod lang_item;
36
37 pub mod expr;
38 pub mod body;
39 pub mod resolver;
40
41 mod trace;
42 pub mod nameres;
43
44 pub mod src;
45 pub mod child_by_source;
46
47 pub mod visibility;
48 pub mod find_path;
49 pub mod import_map;
50
51 #[cfg(test)]
52 mod test_db;
53
54 use std::{
55     hash::{Hash, Hasher},
56     sync::Arc,
57 };
58
59 use adt::VariantData;
60 use base_db::{impl_intern_key, salsa, CrateId};
61 use hir_expand::{
62     ast_id_map::FileAstId,
63     eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
64     hygiene::Hygiene,
65     AstId, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
66 };
67 use la_arena::Idx;
68 use nameres::DefMap;
69 use path::ModPath;
70 use syntax::ast;
71
72 use crate::attr::AttrId;
73 use crate::builtin_type::BuiltinType;
74 use item_tree::{
75     Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
76     TypeAlias, Union,
77 };
78 use stdx::impl_from;
79
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81 pub struct ModuleId {
82     krate: CrateId,
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,
89 }
90
91 impl ModuleId {
92     pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
93         match self.block {
94             Some(block) => {
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);
99                 })
100             }
101             None => db.crate_def_map(self.krate),
102         }
103     }
104
105     pub fn krate(&self) -> CrateId {
106         self.krate
107     }
108
109     pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
110         self.def_map(db).containing_module(self.local_id)
111     }
112
113     /// Returns `true` if this module represents a block expression.
114     ///
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() {
119             return false;
120         }
121
122         self.def_map(db)[self.local_id].parent.is_none()
123     }
124 }
125
126 /// An ID of a module, **local** to a specific crate
127 pub type LocalModuleId = Idx<nameres::ModuleData>;
128
129 #[derive(Debug)]
130 pub struct ItemLoc<N: ItemTreeNode> {
131     pub container: ModuleId,
132     pub id: ItemTreeId<N>,
133 }
134
135 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
136     fn clone(&self) -> Self {
137         Self { container: self.container, id: self.id }
138     }
139 }
140
141 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
142
143 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
144     fn eq(&self, other: &Self) -> bool {
145         self.container == other.container && self.id == other.id
146     }
147 }
148
149 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
150
151 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
152     fn hash<H: Hasher>(&self, state: &mut H) {
153         self.container.hash(state);
154         self.id.hash(state);
155     }
156 }
157
158 #[derive(Debug)]
159 pub struct AssocItemLoc<N: ItemTreeNode> {
160     pub container: AssocContainerId,
161     pub id: ItemTreeId<N>,
162 }
163
164 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
165     fn clone(&self) -> Self {
166         Self { container: self.container, id: self.id }
167     }
168 }
169
170 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
171
172 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
173     fn eq(&self, other: &Self) -> bool {
174         self.container == other.container && self.id == other.id
175     }
176 }
177
178 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
179
180 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
181     fn hash<H: Hasher>(&self, state: &mut H) {
182         self.container.hash(state);
183         self.id.hash(state);
184     }
185 }
186
187 macro_rules! impl_intern {
188     ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
189         impl_intern_key!($id);
190
191         impl Intern for $loc {
192             type ID = $id;
193             fn intern(self, db: &dyn db::DefDatabase) -> $id {
194                 db.$intern(self)
195             }
196         }
197
198         impl Lookup for $id {
199             type Data = $loc;
200             fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
201                 db.$lookup(*self)
202             }
203         }
204     };
205 }
206
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);
211
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);
216
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);
221
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);
226
227 // FIXME: rename to `VariantId`, only enums can ave variants
228 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
229 pub struct EnumVariantId {
230     pub parent: EnumId,
231     pub local_id: LocalEnumVariantId,
232 }
233
234 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
235
236 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
237 pub struct FieldId {
238     pub parent: VariantId,
239     pub local_id: LocalFieldId,
240 }
241
242 pub type LocalFieldId = Idx<adt::FieldData>;
243
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);
248
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);
253
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);
258
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);
263
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);
268
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.
275     module: ModuleId,
276 }
277 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
278
279 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
280 pub struct TypeParamId {
281     pub parent: GenericDefId,
282     pub local_id: LocalTypeParamId,
283 }
284
285 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
286
287 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
288 pub struct LifetimeParamId {
289     pub parent: GenericDefId,
290     pub local_id: LocalLifetimeParamId,
291 }
292 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
293
294 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
295 pub struct ConstParamId {
296     pub parent: GenericDefId,
297     pub local_id: LocalConstParamId,
298 }
299 pub type LocalConstParamId = Idx<generics::ConstParamData>;
300
301 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
302 pub enum AssocContainerId {
303     ModuleId(ModuleId),
304     ImplId(ImplId),
305     TraitId(TraitId),
306 }
307 impl_from!(ModuleId for AssocContainerId);
308
309 /// A Data Type
310 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
311 pub enum AdtId {
312     StructId(StructId),
313     UnionId(UnionId),
314     EnumId(EnumId),
315 }
316 impl_from!(StructId, UnionId, EnumId for AdtId);
317
318 /// A generic param
319 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
320 pub enum GenericParamId {
321     TypeParamId(TypeParamId),
322     LifetimeParamId(LifetimeParamId),
323     ConstParamId(ConstParamId),
324 }
325 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
326
327 /// The defs which can be visible in the module.
328 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
329 pub enum ModuleDefId {
330     ModuleId(ModuleId),
331     FunctionId(FunctionId),
332     AdtId(AdtId),
333     // Can't be directly declared, but can be imported.
334     EnumVariantId(EnumVariantId),
335     ConstId(ConstId),
336     StaticId(StaticId),
337     TraitId(TraitId),
338     TypeAliasId(TypeAliasId),
339     BuiltinType(BuiltinType),
340 }
341 impl_from!(
342     ModuleId,
343     FunctionId,
344     AdtId(StructId, EnumId, UnionId),
345     EnumVariantId,
346     ConstId,
347     StaticId,
348     TraitId,
349     TypeAliasId,
350     BuiltinType
351     for ModuleDefId
352 );
353
354 /// The defs which have a body.
355 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
356 pub enum DefWithBodyId {
357     FunctionId(FunctionId),
358     StaticId(StaticId),
359     ConstId(ConstId),
360 }
361
362 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
363
364 impl DefWithBodyId {
365     pub fn as_generic_def_id(self) -> Option<GenericDefId> {
366         match self {
367             DefWithBodyId::FunctionId(f) => Some(f.into()),
368             DefWithBodyId::StaticId(_) => None,
369             DefWithBodyId::ConstId(c) => Some(c.into()),
370         }
371     }
372 }
373
374 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
375 pub enum AssocItemId {
376     FunctionId(FunctionId),
377     ConstId(ConstId),
378     TypeAliasId(TypeAliasId),
379 }
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);
385
386 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
387 pub enum GenericDefId {
388     FunctionId(FunctionId),
389     AdtId(AdtId),
390     TraitId(TraitId),
391     TypeAliasId(TypeAliasId),
392     ImplId(ImplId),
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)
397     ConstId(ConstId),
398 }
399 impl_from!(
400     FunctionId,
401     AdtId(StructId, EnumId, UnionId),
402     TraitId,
403     TypeAliasId,
404     ImplId,
405     EnumVariantId,
406     ConstId
407     for GenericDefId
408 );
409
410 impl From<AssocItemId> for GenericDefId {
411     fn from(item: AssocItemId) -> Self {
412         match item {
413             AssocItemId::FunctionId(f) => f.into(),
414             AssocItemId::ConstId(c) => c.into(),
415             AssocItemId::TypeAliasId(t) => t.into(),
416         }
417     }
418 }
419
420 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
421 pub enum AttrDefId {
422     ModuleId(ModuleId),
423     FieldId(FieldId),
424     AdtId(AdtId),
425     FunctionId(FunctionId),
426     EnumVariantId(EnumVariantId),
427     StaticId(StaticId),
428     ConstId(ConstId),
429     TraitId(TraitId),
430     TypeAliasId(TypeAliasId),
431     MacroDefId(MacroDefId),
432     ImplId(ImplId),
433     GenericParamId(GenericParamId),
434 }
435
436 impl_from!(
437     ModuleId,
438     FieldId,
439     AdtId(StructId, EnumId, UnionId),
440     EnumVariantId,
441     StaticId,
442     ConstId,
443     FunctionId,
444     TraitId,
445     TypeAliasId,
446     MacroDefId,
447     ImplId,
448     GenericParamId
449     for AttrDefId
450 );
451
452 impl From<AssocContainerId> for AttrDefId {
453     fn from(acid: AssocContainerId) -> Self {
454         match acid {
455             AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
456             AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
457             AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
458         }
459     }
460 }
461
462 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
463 pub enum VariantId {
464     EnumVariantId(EnumVariantId),
465     StructId(StructId),
466     UnionId(UnionId),
467 }
468 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
469
470 impl VariantId {
471     pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
472         match self {
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()
477             }
478         }
479     }
480
481     pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
482         match self {
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(),
486         }
487     }
488 }
489
490 trait Intern {
491     type ID;
492     fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
493 }
494
495 pub trait Lookup {
496     type Data;
497     fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
498 }
499
500 pub trait HasModule {
501     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
502 }
503
504 impl HasModule for AssocContainerId {
505     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
506         match *self {
507             AssocContainerId::ModuleId(it) => it,
508             AssocContainerId::ImplId(it) => it.lookup(db).container,
509             AssocContainerId::TraitId(it) => it.lookup(db).container,
510         }
511     }
512 }
513
514 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
515     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
516         self.container.module(db)
517     }
518 }
519
520 impl HasModule for AdtId {
521     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
522         match self {
523             AdtId::StructId(it) => it.lookup(db).container,
524             AdtId::UnionId(it) => it.lookup(db).container,
525             AdtId::EnumId(it) => it.lookup(db).container,
526         }
527     }
528 }
529
530 impl HasModule for VariantId {
531     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
532         match self {
533             VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
534             VariantId::StructId(it) => it.lookup(db).container,
535             VariantId::UnionId(it) => it.lookup(db).container,
536         }
537     }
538 }
539
540 impl HasModule for DefWithBodyId {
541     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
542         match self {
543             DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
544             DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
545             DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
546         }
547     }
548 }
549
550 impl DefWithBodyId {
551     pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
552         match self {
553             DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
554             DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
555             DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
556         }
557     }
558 }
559
560 impl HasModule for GenericDefId {
561     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
562         match self {
563             GenericDefId::FunctionId(it) => it.lookup(db).module(db),
564             GenericDefId::AdtId(it) => it.module(db),
565             GenericDefId::TraitId(it) => it.lookup(db).container,
566             GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
567             GenericDefId::ImplId(it) => it.lookup(db).container,
568             GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
569             GenericDefId::ConstId(it) => it.lookup(db).module(db),
570         }
571     }
572 }
573
574 impl HasModule for StaticLoc {
575     fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
576         self.container
577     }
578 }
579
580 impl ModuleDefId {
581     /// Returns the module containing `self` (or `self`, if `self` is itself a module).
582     ///
583     /// Returns `None` if `self` refers to a primitive type.
584     pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
585         Some(match self {
586             ModuleDefId::ModuleId(id) => *id,
587             ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
588             ModuleDefId::AdtId(id) => id.module(db),
589             ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
590             ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
591             ModuleDefId::StaticId(id) => id.lookup(db).container,
592             ModuleDefId::TraitId(id) => id.lookup(db).container,
593             ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
594             ModuleDefId::BuiltinType(_) => return None,
595         })
596     }
597 }
598
599 impl AttrDefId {
600     pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
601         match self {
602             AttrDefId::ModuleId(it) => it.krate,
603             AttrDefId::FieldId(it) => it.parent.module(db).krate,
604             AttrDefId::AdtId(it) => it.module(db).krate,
605             AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
606             AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
607             AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
608             AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
609             AttrDefId::TraitId(it) => it.lookup(db).container.krate,
610             AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
611             AttrDefId::ImplId(it) => it.lookup(db).container.krate,
612             AttrDefId::GenericParamId(it) => {
613                 match it {
614                     GenericParamId::TypeParamId(it) => it.parent,
615                     GenericParamId::LifetimeParamId(it) => it.parent,
616                     GenericParamId::ConstParamId(it) => it.parent,
617                 }
618                 .module(db)
619                 .krate
620             }
621             // FIXME: `MacroDefId` should store the defining module, then this can implement
622             // `HasModule`
623             AttrDefId::MacroDefId(it) => it.krate,
624         }
625     }
626 }
627
628 /// A helper trait for converting to MacroCallId
629 pub trait AsMacroCall {
630     fn as_call_id(
631         &self,
632         db: &dyn db::DefDatabase,
633         krate: CrateId,
634         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
635     ) -> Option<MacroCallId> {
636         self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
637     }
638
639     fn as_call_id_with_errors(
640         &self,
641         db: &dyn db::DefDatabase,
642         krate: CrateId,
643         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
644         error_sink: &mut dyn FnMut(mbe::ExpandError),
645     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
646 }
647
648 impl AsMacroCall for InFile<&ast::MacroCall> {
649     fn as_call_id_with_errors(
650         &self,
651         db: &dyn db::DefDatabase,
652         krate: CrateId,
653         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
654         mut error_sink: &mut dyn FnMut(mbe::ExpandError),
655     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
656         let fragment = hir_expand::to_fragment_kind(self.value);
657         let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
658         let h = Hygiene::new(db.upcast(), self.file_id);
659         let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
660
661         let path = match error_sink
662             .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
663         {
664             Ok(path) => path,
665             Err(error) => {
666                 return Ok(Err(error));
667             }
668         };
669
670         macro_call_as_call_id(
671             &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
672             fragment,
673             db,
674             krate,
675             resolver,
676             error_sink,
677         )
678     }
679 }
680
681 /// Helper wrapper for `AstId` with `ModPath`
682 #[derive(Clone, Debug, Eq, PartialEq)]
683 struct AstIdWithPath<T: ast::AstNode> {
684     ast_id: AstId<T>,
685     path: path::ModPath,
686 }
687
688 impl<T: ast::AstNode> AstIdWithPath<T> {
689     fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
690         AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
691     }
692 }
693
694 #[derive(Debug)]
695 pub struct UnresolvedMacro {
696     pub path: ModPath,
697 }
698
699 fn macro_call_as_call_id(
700     call: &AstIdWithPath<ast::MacroCall>,
701     fragment: FragmentKind,
702     db: &dyn db::DefDatabase,
703     krate: CrateId,
704     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
705     error_sink: &mut dyn FnMut(mbe::ExpandError),
706 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
707     let def: MacroDefId =
708         resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
709
710     let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
711         let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
712         let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
713
714         expand_eager_macro(
715             db.upcast(),
716             krate,
717             macro_call,
718             def,
719             &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
720             error_sink,
721         )
722         .map(MacroCallId::from)
723     } else {
724         Ok(def
725             .as_lazy_macro(
726                 db.upcast(),
727                 krate,
728                 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
729             )
730             .into())
731     };
732     Ok(res)
733 }
734
735 fn derive_macro_as_call_id(
736     item_attr: &AstIdWithPath<ast::Item>,
737     derive_attr: AttrId,
738     db: &dyn db::DefDatabase,
739     krate: CrateId,
740     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
741 ) -> Result<MacroCallId, UnresolvedMacro> {
742     let def: MacroDefId = resolver(item_attr.path.clone())
743         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
744     let last_segment = item_attr
745         .path
746         .segments()
747         .last()
748         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
749     let res = def
750         .as_lazy_macro(
751             db.upcast(),
752             krate,
753             MacroCallKind::Derive {
754                 ast_id: item_attr.ast_id,
755                 derive_name: last_segment.to_string(),
756                 derive_attr_index: derive_attr.ast_index,
757             },
758         )
759         .into();
760     Ok(res)
761 }