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