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