]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/lib.rs
Merge #8582
[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, AttrId, 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::builtin_type::BuiltinType;
73 use item_tree::{
74     Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait,
75     TypeAlias, Union,
76 };
77 use stdx::impl_from;
78
79 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80 pub struct ModuleId {
81     krate: CrateId,
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,
88 }
89
90 impl ModuleId {
91     pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
92         match self.block {
93             Some(block) => {
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);
98                 })
99             }
100             None => db.crate_def_map(self.krate),
101         }
102     }
103
104     pub fn krate(&self) -> CrateId {
105         self.krate
106     }
107
108     pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
109         self.def_map(db).containing_module(self.local_id)
110     }
111
112     /// Returns `true` if this module represents a block expression.
113     ///
114     /// Returns `false` if this module is a submodule *inside* a block expression
115     /// (eg. `m` in `{ mod m {} }`).
116     pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool {
117         if self.block.is_none() {
118             return false;
119         }
120
121         self.def_map(db)[self.local_id].parent.is_none()
122     }
123 }
124
125 /// An ID of a module, **local** to a specific crate
126 pub type LocalModuleId = Idx<nameres::ModuleData>;
127
128 #[derive(Debug)]
129 pub struct ItemLoc<N: ItemTreeNode> {
130     pub container: ModuleId,
131     pub id: ItemTreeId<N>,
132 }
133
134 impl<N: ItemTreeNode> Clone for ItemLoc<N> {
135     fn clone(&self) -> Self {
136         Self { container: self.container, id: self.id }
137     }
138 }
139
140 impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
141
142 impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
143     fn eq(&self, other: &Self) -> bool {
144         self.container == other.container && self.id == other.id
145     }
146 }
147
148 impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
149
150 impl<N: ItemTreeNode> Hash for ItemLoc<N> {
151     fn hash<H: Hasher>(&self, state: &mut H) {
152         self.container.hash(state);
153         self.id.hash(state);
154     }
155 }
156
157 #[derive(Debug)]
158 pub struct AssocItemLoc<N: ItemTreeNode> {
159     pub container: AssocContainerId,
160     pub id: ItemTreeId<N>,
161 }
162
163 impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
164     fn clone(&self) -> Self {
165         Self { container: self.container, id: self.id }
166     }
167 }
168
169 impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
170
171 impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
172     fn eq(&self, other: &Self) -> bool {
173         self.container == other.container && self.id == other.id
174     }
175 }
176
177 impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
178
179 impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
180     fn hash<H: Hasher>(&self, state: &mut H) {
181         self.container.hash(state);
182         self.id.hash(state);
183     }
184 }
185
186 macro_rules! impl_intern {
187     ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
188         impl_intern_key!($id);
189
190         impl Intern for $loc {
191             type ID = $id;
192             fn intern(self, db: &dyn db::DefDatabase) -> $id {
193                 db.$intern(self)
194             }
195         }
196
197         impl Lookup for $id {
198             type Data = $loc;
199             fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
200                 db.$lookup(*self)
201             }
202         }
203     };
204 }
205
206 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
207 pub struct FunctionId(salsa::InternId);
208 type FunctionLoc = AssocItemLoc<Function>;
209 impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
210
211 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
212 pub struct StructId(salsa::InternId);
213 type StructLoc = ItemLoc<Struct>;
214 impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
215
216 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
217 pub struct UnionId(salsa::InternId);
218 pub type UnionLoc = ItemLoc<Union>;
219 impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
220
221 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
222 pub struct EnumId(salsa::InternId);
223 pub type EnumLoc = ItemLoc<Enum>;
224 impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
225
226 // FIXME: rename to `VariantId`, only enums can ave variants
227 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
228 pub struct EnumVariantId {
229     pub parent: EnumId,
230     pub local_id: LocalEnumVariantId,
231 }
232
233 pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
234
235 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
236 pub struct FieldId {
237     pub parent: VariantId,
238     pub local_id: LocalFieldId,
239 }
240
241 pub type LocalFieldId = Idx<adt::FieldData>;
242
243 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
244 pub struct ConstId(salsa::InternId);
245 type ConstLoc = AssocItemLoc<Const>;
246 impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
247
248 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
249 pub struct StaticId(salsa::InternId);
250 pub type StaticLoc = ItemLoc<Static>;
251 impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
252
253 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254 pub struct TraitId(salsa::InternId);
255 pub type TraitLoc = ItemLoc<Trait>;
256 impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
257
258 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
259 pub struct TypeAliasId(salsa::InternId);
260 type TypeAliasLoc = AssocItemLoc<TypeAlias>;
261 impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
262
263 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
264 pub struct ImplId(salsa::InternId);
265 type ImplLoc = ItemLoc<Impl>;
266 impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
267
268 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
269 pub struct BlockId(salsa::InternId);
270 #[derive(Debug, Hash, PartialEq, Eq, Clone)]
271 pub struct BlockLoc {
272     ast_id: AstId<ast::BlockExpr>,
273     /// The containing module.
274     module: ModuleId,
275 }
276 impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
277
278 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
279 pub struct TypeParamId {
280     pub parent: GenericDefId,
281     pub local_id: LocalTypeParamId,
282 }
283
284 pub type LocalTypeParamId = Idx<generics::TypeParamData>;
285
286 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
287 pub struct LifetimeParamId {
288     pub parent: GenericDefId,
289     pub local_id: LocalLifetimeParamId,
290 }
291 pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
292
293 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
294 pub struct ConstParamId {
295     pub parent: GenericDefId,
296     pub local_id: LocalConstParamId,
297 }
298 pub type LocalConstParamId = Idx<generics::ConstParamData>;
299
300 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
301 pub enum AssocContainerId {
302     ModuleId(ModuleId),
303     ImplId(ImplId),
304     TraitId(TraitId),
305 }
306 impl_from!(ModuleId for AssocContainerId);
307
308 /// A Data Type
309 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
310 pub enum AdtId {
311     StructId(StructId),
312     UnionId(UnionId),
313     EnumId(EnumId),
314 }
315 impl_from!(StructId, UnionId, EnumId for AdtId);
316
317 /// A generic param
318 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
319 pub enum GenericParamId {
320     TypeParamId(TypeParamId),
321     LifetimeParamId(LifetimeParamId),
322     ConstParamId(ConstParamId),
323 }
324 impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId);
325
326 /// The defs which can be visible in the module.
327 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
328 pub enum ModuleDefId {
329     ModuleId(ModuleId),
330     FunctionId(FunctionId),
331     AdtId(AdtId),
332     // Can't be directly declared, but can be imported.
333     EnumVariantId(EnumVariantId),
334     ConstId(ConstId),
335     StaticId(StaticId),
336     TraitId(TraitId),
337     TypeAliasId(TypeAliasId),
338     BuiltinType(BuiltinType),
339 }
340 impl_from!(
341     ModuleId,
342     FunctionId,
343     AdtId(StructId, EnumId, UnionId),
344     EnumVariantId,
345     ConstId,
346     StaticId,
347     TraitId,
348     TypeAliasId,
349     BuiltinType
350     for ModuleDefId
351 );
352
353 /// The defs which have a body.
354 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
355 pub enum DefWithBodyId {
356     FunctionId(FunctionId),
357     StaticId(StaticId),
358     ConstId(ConstId),
359 }
360
361 impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId);
362
363 impl DefWithBodyId {
364     pub fn as_generic_def_id(self) -> Option<GenericDefId> {
365         match self {
366             DefWithBodyId::FunctionId(f) => Some(f.into()),
367             DefWithBodyId::StaticId(_) => None,
368             DefWithBodyId::ConstId(c) => Some(c.into()),
369         }
370     }
371 }
372
373 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
374 pub enum AssocItemId {
375     FunctionId(FunctionId),
376     ConstId(ConstId),
377     TypeAliasId(TypeAliasId),
378 }
379 // FIXME: not every function, ... is actually an assoc item. maybe we should make
380 // sure that you can only turn actual assoc items into AssocItemIds. This would
381 // require not implementing From, and instead having some checked way of
382 // casting them, and somehow making the constructors private, which would be annoying.
383 impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId);
384
385 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
386 pub enum GenericDefId {
387     FunctionId(FunctionId),
388     AdtId(AdtId),
389     TraitId(TraitId),
390     TypeAliasId(TypeAliasId),
391     ImplId(ImplId),
392     // enum variants cannot have generics themselves, but their parent enums
393     // can, and this makes some code easier to write
394     EnumVariantId(EnumVariantId),
395     // consts can have type parameters from their parents (i.e. associated consts of traits)
396     ConstId(ConstId),
397 }
398 impl_from!(
399     FunctionId,
400     AdtId(StructId, EnumId, UnionId),
401     TraitId,
402     TypeAliasId,
403     ImplId,
404     EnumVariantId,
405     ConstId
406     for GenericDefId
407 );
408
409 impl From<AssocItemId> for GenericDefId {
410     fn from(item: AssocItemId) -> Self {
411         match item {
412             AssocItemId::FunctionId(f) => f.into(),
413             AssocItemId::ConstId(c) => c.into(),
414             AssocItemId::TypeAliasId(t) => t.into(),
415         }
416     }
417 }
418
419 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
420 pub enum AttrDefId {
421     ModuleId(ModuleId),
422     FieldId(FieldId),
423     AdtId(AdtId),
424     FunctionId(FunctionId),
425     EnumVariantId(EnumVariantId),
426     StaticId(StaticId),
427     ConstId(ConstId),
428     TraitId(TraitId),
429     TypeAliasId(TypeAliasId),
430     MacroDefId(MacroDefId),
431     ImplId(ImplId),
432     GenericParamId(GenericParamId),
433 }
434
435 impl_from!(
436     ModuleId,
437     FieldId,
438     AdtId(StructId, EnumId, UnionId),
439     EnumVariantId,
440     StaticId,
441     ConstId,
442     FunctionId,
443     TraitId,
444     TypeAliasId,
445     MacroDefId,
446     ImplId,
447     GenericParamId
448     for AttrDefId
449 );
450
451 impl From<AssocContainerId> for AttrDefId {
452     fn from(acid: AssocContainerId) -> Self {
453         match acid {
454             AssocContainerId::ModuleId(mid) => AttrDefId::ModuleId(mid),
455             AssocContainerId::ImplId(iid) => AttrDefId::ImplId(iid),
456             AssocContainerId::TraitId(tid) => AttrDefId::TraitId(tid),
457         }
458     }
459 }
460
461 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
462 pub enum VariantId {
463     EnumVariantId(EnumVariantId),
464     StructId(StructId),
465     UnionId(UnionId),
466 }
467 impl_from!(EnumVariantId, StructId, UnionId for VariantId);
468
469 impl VariantId {
470     pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
471         match self {
472             VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
473             VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
474             VariantId::EnumVariantId(it) => {
475                 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
476             }
477         }
478     }
479
480     pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
481         match self {
482             VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
483             VariantId::StructId(it) => it.lookup(db).id.file_id(),
484             VariantId::UnionId(it) => it.lookup(db).id.file_id(),
485         }
486     }
487 }
488
489 trait Intern {
490     type ID;
491     fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
492 }
493
494 pub trait Lookup {
495     type Data;
496     fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
497 }
498
499 pub trait HasModule {
500     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
501 }
502
503 impl HasModule for AssocContainerId {
504     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
505         match *self {
506             AssocContainerId::ModuleId(it) => it,
507             AssocContainerId::ImplId(it) => it.lookup(db).container,
508             AssocContainerId::TraitId(it) => it.lookup(db).container,
509         }
510     }
511 }
512
513 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
514     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
515         self.container.module(db)
516     }
517 }
518
519 impl HasModule for AdtId {
520     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
521         match self {
522             AdtId::StructId(it) => it.lookup(db).container,
523             AdtId::UnionId(it) => it.lookup(db).container,
524             AdtId::EnumId(it) => it.lookup(db).container,
525         }
526     }
527 }
528
529 impl HasModule for VariantId {
530     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
531         match self {
532             VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
533             VariantId::StructId(it) => it.lookup(db).container,
534             VariantId::UnionId(it) => it.lookup(db).container,
535         }
536     }
537 }
538
539 impl HasModule for DefWithBodyId {
540     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
541         match self {
542             DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
543             DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
544             DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
545         }
546     }
547 }
548
549 impl DefWithBodyId {
550     pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
551         match self {
552             DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
553             DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
554             DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
555         }
556     }
557 }
558
559 impl HasModule for GenericDefId {
560     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
561         match self {
562             GenericDefId::FunctionId(it) => it.lookup(db).module(db),
563             GenericDefId::AdtId(it) => it.module(db),
564             GenericDefId::TraitId(it) => it.lookup(db).container,
565             GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
566             GenericDefId::ImplId(it) => it.lookup(db).container,
567             GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
568             GenericDefId::ConstId(it) => it.lookup(db).module(db),
569         }
570     }
571 }
572
573 impl HasModule for StaticLoc {
574     fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
575         self.container
576     }
577 }
578
579 impl ModuleDefId {
580     /// Returns the module containing `self` (or `self`, if `self` is itself a module).
581     ///
582     /// Returns `None` if `self` refers to a primitive type.
583     pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
584         Some(match self {
585             ModuleDefId::ModuleId(id) => *id,
586             ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
587             ModuleDefId::AdtId(id) => id.module(db),
588             ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
589             ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
590             ModuleDefId::StaticId(id) => id.lookup(db).container,
591             ModuleDefId::TraitId(id) => id.lookup(db).container,
592             ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
593             ModuleDefId::BuiltinType(_) => return None,
594         })
595     }
596 }
597
598 impl AttrDefId {
599     pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
600         match self {
601             AttrDefId::ModuleId(it) => it.krate,
602             AttrDefId::FieldId(it) => it.parent.module(db).krate,
603             AttrDefId::AdtId(it) => it.module(db).krate,
604             AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
605             AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
606             AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
607             AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
608             AttrDefId::TraitId(it) => it.lookup(db).container.krate,
609             AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
610             AttrDefId::ImplId(it) => it.lookup(db).container.krate,
611             AttrDefId::GenericParamId(it) => {
612                 match it {
613                     GenericParamId::TypeParamId(it) => it.parent,
614                     GenericParamId::LifetimeParamId(it) => it.parent,
615                     GenericParamId::ConstParamId(it) => it.parent,
616                 }
617                 .module(db)
618                 .krate
619             }
620             // FIXME: `MacroDefId` should store the defining module, then this can implement
621             // `HasModule`
622             AttrDefId::MacroDefId(it) => it.krate,
623         }
624     }
625 }
626
627 /// A helper trait for converting to MacroCallId
628 pub trait AsMacroCall {
629     fn as_call_id(
630         &self,
631         db: &dyn db::DefDatabase,
632         krate: CrateId,
633         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
634     ) -> Option<MacroCallId> {
635         self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
636     }
637
638     fn as_call_id_with_errors(
639         &self,
640         db: &dyn db::DefDatabase,
641         krate: CrateId,
642         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
643         error_sink: &mut dyn FnMut(mbe::ExpandError),
644     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
645 }
646
647 impl AsMacroCall for InFile<&ast::MacroCall> {
648     fn as_call_id_with_errors(
649         &self,
650         db: &dyn db::DefDatabase,
651         krate: CrateId,
652         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
653         mut error_sink: &mut dyn FnMut(mbe::ExpandError),
654     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
655         let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
656         let h = Hygiene::new(db.upcast(), self.file_id);
657         let path = self.value.path().and_then(|path| path::ModPath::from_src(path, &h));
658
659         let path = match error_sink
660             .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
661         {
662             Ok(path) => path,
663             Err(error) => {
664                 return Ok(Err(error));
665             }
666         };
667
668         macro_call_as_call_id(
669             &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
670             db,
671             krate,
672             resolver,
673             error_sink,
674         )
675     }
676 }
677
678 /// Helper wrapper for `AstId` with `ModPath`
679 #[derive(Clone, Debug, Eq, PartialEq)]
680 struct AstIdWithPath<T: ast::AstNode> {
681     ast_id: AstId<T>,
682     path: path::ModPath,
683 }
684
685 impl<T: ast::AstNode> AstIdWithPath<T> {
686     fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
687         AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
688     }
689 }
690
691 #[derive(Debug)]
692 pub struct UnresolvedMacro {
693     pub path: ModPath,
694 }
695
696 fn macro_call_as_call_id(
697     call: &AstIdWithPath<ast::MacroCall>,
698     db: &dyn db::DefDatabase,
699     krate: CrateId,
700     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
701     error_sink: &mut dyn FnMut(mbe::ExpandError),
702 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
703     let def: MacroDefId =
704         resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
705
706     let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
707         let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
708         let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
709
710         expand_eager_macro(
711             db.upcast(),
712             krate,
713             macro_call,
714             def,
715             &|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?),
716             error_sink,
717         )
718         .map(MacroCallId::from)
719     } else {
720         Ok(def
721             .as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id })
722             .into())
723     };
724     Ok(res)
725 }
726
727 fn derive_macro_as_call_id(
728     item_attr: &AstIdWithPath<ast::Item>,
729     derive_attr: AttrId,
730     db: &dyn db::DefDatabase,
731     krate: CrateId,
732     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
733 ) -> Result<MacroCallId, UnresolvedMacro> {
734     let def: MacroDefId = resolver(item_attr.path.clone())
735         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
736     let last_segment = item_attr
737         .path
738         .segments()
739         .last()
740         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
741     let res = def
742         .as_lazy_macro(
743             db.upcast(),
744             krate,
745             MacroCallKind::Derive {
746                 ast_id: item_attr.ast_id,
747                 derive_name: last_segment.to_string(),
748                 derive_attr,
749             },
750         )
751         .into();
752     Ok(res)
753 }