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