]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/lib.rs
Merge #8942
[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     pub fn adt_id(self) -> AdtId {
490         match self {
491             VariantId::EnumVariantId(it) => it.parent.into(),
492             VariantId::StructId(it) => it.into(),
493             VariantId::UnionId(it) => it.into(),
494         }
495     }
496 }
497
498 trait Intern {
499     type ID;
500     fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
501 }
502
503 pub trait Lookup {
504     type Data;
505     fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
506 }
507
508 pub trait HasModule {
509     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
510 }
511
512 impl HasModule for AssocContainerId {
513     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
514         match *self {
515             AssocContainerId::ModuleId(it) => it,
516             AssocContainerId::ImplId(it) => it.lookup(db).container,
517             AssocContainerId::TraitId(it) => it.lookup(db).container,
518         }
519     }
520 }
521
522 impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
523     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
524         self.container.module(db)
525     }
526 }
527
528 impl HasModule for AdtId {
529     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
530         match self {
531             AdtId::StructId(it) => it.lookup(db).container,
532             AdtId::UnionId(it) => it.lookup(db).container,
533             AdtId::EnumId(it) => it.lookup(db).container,
534         }
535     }
536 }
537
538 impl HasModule for VariantId {
539     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
540         match self {
541             VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
542             VariantId::StructId(it) => it.lookup(db).container,
543             VariantId::UnionId(it) => it.lookup(db).container,
544         }
545     }
546 }
547
548 impl HasModule for DefWithBodyId {
549     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
550         match self {
551             DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
552             DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
553             DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
554         }
555     }
556 }
557
558 impl DefWithBodyId {
559     pub fn as_mod_item(self, db: &dyn db::DefDatabase) -> ModItem {
560         match self {
561             DefWithBodyId::FunctionId(it) => it.lookup(db).id.value.into(),
562             DefWithBodyId::StaticId(it) => it.lookup(db).id.value.into(),
563             DefWithBodyId::ConstId(it) => it.lookup(db).id.value.into(),
564         }
565     }
566 }
567
568 impl HasModule for GenericDefId {
569     fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
570         match self {
571             GenericDefId::FunctionId(it) => it.lookup(db).module(db),
572             GenericDefId::AdtId(it) => it.module(db),
573             GenericDefId::TraitId(it) => it.lookup(db).container,
574             GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
575             GenericDefId::ImplId(it) => it.lookup(db).container,
576             GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
577             GenericDefId::ConstId(it) => it.lookup(db).module(db),
578         }
579     }
580 }
581
582 impl HasModule for StaticLoc {
583     fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
584         self.container
585     }
586 }
587
588 impl ModuleDefId {
589     /// Returns the module containing `self` (or `self`, if `self` is itself a module).
590     ///
591     /// Returns `None` if `self` refers to a primitive type.
592     pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
593         Some(match self {
594             ModuleDefId::ModuleId(id) => *id,
595             ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
596             ModuleDefId::AdtId(id) => id.module(db),
597             ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
598             ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
599             ModuleDefId::StaticId(id) => id.lookup(db).container,
600             ModuleDefId::TraitId(id) => id.lookup(db).container,
601             ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
602             ModuleDefId::BuiltinType(_) => return None,
603         })
604     }
605 }
606
607 impl AttrDefId {
608     pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
609         match self {
610             AttrDefId::ModuleId(it) => it.krate,
611             AttrDefId::FieldId(it) => it.parent.module(db).krate,
612             AttrDefId::AdtId(it) => it.module(db).krate,
613             AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
614             AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
615             AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
616             AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
617             AttrDefId::TraitId(it) => it.lookup(db).container.krate,
618             AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
619             AttrDefId::ImplId(it) => it.lookup(db).container.krate,
620             AttrDefId::GenericParamId(it) => {
621                 match it {
622                     GenericParamId::TypeParamId(it) => it.parent,
623                     GenericParamId::LifetimeParamId(it) => it.parent,
624                     GenericParamId::ConstParamId(it) => it.parent,
625                 }
626                 .module(db)
627                 .krate
628             }
629             // FIXME: `MacroDefId` should store the defining module, then this can implement
630             // `HasModule`
631             AttrDefId::MacroDefId(it) => it.krate,
632         }
633     }
634 }
635
636 /// A helper trait for converting to MacroCallId
637 pub trait AsMacroCall {
638     fn as_call_id(
639         &self,
640         db: &dyn db::DefDatabase,
641         krate: CrateId,
642         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
643     ) -> Option<MacroCallId> {
644         self.as_call_id_with_errors(db, krate, resolver, &mut |_| ()).ok()?.ok()
645     }
646
647     fn as_call_id_with_errors(
648         &self,
649         db: &dyn db::DefDatabase,
650         krate: CrateId,
651         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
652         error_sink: &mut dyn FnMut(mbe::ExpandError),
653     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro>;
654 }
655
656 impl AsMacroCall for InFile<&ast::MacroCall> {
657     fn as_call_id_with_errors(
658         &self,
659         db: &dyn db::DefDatabase,
660         krate: CrateId,
661         resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
662         mut error_sink: &mut dyn FnMut(mbe::ExpandError),
663     ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
664         let fragment = hir_expand::to_fragment_kind(self.value);
665         let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
666         let h = Hygiene::new(db.upcast(), self.file_id);
667         let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
668
669         let path = match error_sink
670             .option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
671         {
672             Ok(path) => path,
673             Err(error) => {
674                 return Ok(Err(error));
675             }
676         };
677
678         macro_call_as_call_id(
679             &AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
680             fragment,
681             db,
682             krate,
683             resolver,
684             error_sink,
685         )
686     }
687 }
688
689 /// Helper wrapper for `AstId` with `ModPath`
690 #[derive(Clone, Debug, Eq, PartialEq)]
691 struct AstIdWithPath<T: ast::AstNode> {
692     ast_id: AstId<T>,
693     path: path::ModPath,
694 }
695
696 impl<T: ast::AstNode> AstIdWithPath<T> {
697     fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
698         AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
699     }
700 }
701
702 #[derive(Debug)]
703 pub struct UnresolvedMacro {
704     pub path: ModPath,
705 }
706
707 fn macro_call_as_call_id(
708     call: &AstIdWithPath<ast::MacroCall>,
709     fragment: FragmentKind,
710     db: &dyn db::DefDatabase,
711     krate: CrateId,
712     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
713     error_sink: &mut dyn FnMut(mbe::ExpandError),
714 ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
715     let def: MacroDefId =
716         resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
717
718     let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
719         let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
720         let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
721
722         expand_eager_macro(
723             db.upcast(),
724             krate,
725             macro_call,
726             def,
727             &|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
728             error_sink,
729         )
730         .map(MacroCallId::from)
731     } else {
732         Ok(def
733             .as_lazy_macro(
734                 db.upcast(),
735                 krate,
736                 MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
737             )
738             .into())
739     };
740     Ok(res)
741 }
742
743 fn derive_macro_as_call_id(
744     item_attr: &AstIdWithPath<ast::Item>,
745     derive_attr: AttrId,
746     db: &dyn db::DefDatabase,
747     krate: CrateId,
748     resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
749 ) -> Result<MacroCallId, UnresolvedMacro> {
750     let def: MacroDefId = resolver(item_attr.path.clone())
751         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
752     let last_segment = item_attr
753         .path
754         .segments()
755         .last()
756         .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
757     let res = def
758         .as_lazy_macro(
759             db.upcast(),
760             krate,
761             MacroCallKind::Derive {
762                 ast_id: item_attr.ast_id,
763                 derive_name: last_segment.to_string(),
764                 derive_attr_index: derive_attr.ast_index,
765             },
766         )
767         .into();
768     Ok(res)
769 }