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