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