]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/item_tree.rs
Remove previously added parameter names from the function data
[rust.git] / crates / hir_def / src / item_tree.rs
1 //! A simplified AST that only contains items.
2
3 mod lower;
4 #[cfg(test)]
5 mod tests;
6
7 use std::{
8     any::type_name,
9     fmt::{self, Debug},
10     hash::{Hash, Hasher},
11     marker::PhantomData,
12     ops::{Index, Range},
13     sync::Arc,
14 };
15
16 use arena::{Arena, Idx, RawId};
17 use ast::{AstNode, AttrsOwner, NameOwner, StructKind};
18 use either::Either;
19 use hir_expand::{
20     ast_id_map::FileAstId,
21     hygiene::Hygiene,
22     name::{name, AsName, Name},
23     HirFileId, InFile,
24 };
25 use rustc_hash::FxHashMap;
26 use smallvec::SmallVec;
27 use syntax::{ast, match_ast};
28 use test_utils::mark;
29
30 use crate::{
31     attr::Attrs,
32     db::DefDatabase,
33     generics::GenericParams,
34     path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
35     type_ref::{Mutability, TypeBound, TypeRef},
36     visibility::RawVisibility,
37 };
38
39 #[derive(Copy, Clone, Eq, PartialEq)]
40 pub struct RawVisibilityId(u32);
41
42 impl RawVisibilityId {
43     pub const PUB: Self = RawVisibilityId(u32::max_value());
44     pub const PRIV: Self = RawVisibilityId(u32::max_value() - 1);
45     pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 2);
46 }
47
48 impl fmt::Debug for RawVisibilityId {
49     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50         let mut f = f.debug_tuple("RawVisibilityId");
51         match *self {
52             Self::PUB => f.field(&"pub"),
53             Self::PRIV => f.field(&"pub(self)"),
54             Self::PUB_CRATE => f.field(&"pub(crate)"),
55             _ => f.field(&self.0),
56         };
57         f.finish()
58     }
59 }
60
61 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
62 pub struct GenericParamsId(u32);
63
64 impl GenericParamsId {
65     pub const EMPTY: Self = GenericParamsId(u32::max_value());
66 }
67
68 /// The item tree of a source file.
69 #[derive(Debug, Eq, PartialEq)]
70 pub struct ItemTree {
71     top_level: SmallVec<[ModItem; 1]>,
72     attrs: FxHashMap<AttrOwner, Attrs>,
73     inner_items: FxHashMap<FileAstId<ast::Item>, SmallVec<[ModItem; 1]>>,
74
75     data: Option<Box<ItemTreeData>>,
76 }
77
78 impl ItemTree {
79     pub fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
80         let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id));
81         let syntax = if let Some(node) = db.parse_or_expand(file_id) {
82             node
83         } else {
84             return Arc::new(Self::empty());
85         };
86
87         let hygiene = Hygiene::new(db.upcast(), file_id);
88         let ctx = lower::Ctx::new(db, hygiene.clone(), file_id);
89         let mut top_attrs = None;
90         let mut item_tree = match_ast! {
91             match syntax {
92                 ast::SourceFile(file) => {
93                     top_attrs = Some(Attrs::new(&file, &hygiene));
94                     ctx.lower_module_items(&file)
95                 },
96                 ast::MacroItems(items) => {
97                     ctx.lower_module_items(&items)
98                 },
99                 // Macros can expand to expressions. We return an empty item tree in this case, but
100                 // still need to collect inner items.
101                 ast::Expr(e) => {
102                     ctx.lower_inner_items(e.syntax())
103                 },
104                 _ => {
105                     panic!("cannot create item tree from {:?}", syntax);
106                 },
107             }
108         };
109
110         if let Some(attrs) = top_attrs {
111             item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
112         }
113         item_tree.shrink_to_fit();
114         Arc::new(item_tree)
115     }
116
117     fn empty() -> Self {
118         Self {
119             top_level: Default::default(),
120             attrs: Default::default(),
121             inner_items: Default::default(),
122             data: Default::default(),
123         }
124     }
125
126     fn shrink_to_fit(&mut self) {
127         if let Some(data) = &mut self.data {
128             let ItemTreeData {
129                 imports,
130                 extern_crates,
131                 functions,
132                 structs,
133                 fields,
134                 unions,
135                 enums,
136                 variants,
137                 consts,
138                 statics,
139                 traits,
140                 impls,
141                 type_aliases,
142                 mods,
143                 macro_calls,
144                 exprs,
145                 vis,
146                 generics,
147             } = &mut **data;
148
149             imports.shrink_to_fit();
150             extern_crates.shrink_to_fit();
151             functions.shrink_to_fit();
152             structs.shrink_to_fit();
153             fields.shrink_to_fit();
154             unions.shrink_to_fit();
155             enums.shrink_to_fit();
156             variants.shrink_to_fit();
157             consts.shrink_to_fit();
158             statics.shrink_to_fit();
159             traits.shrink_to_fit();
160             impls.shrink_to_fit();
161             type_aliases.shrink_to_fit();
162             mods.shrink_to_fit();
163             macro_calls.shrink_to_fit();
164             exprs.shrink_to_fit();
165
166             vis.arena.shrink_to_fit();
167             generics.arena.shrink_to_fit();
168         }
169     }
170
171     /// Returns an iterator over all items located at the top level of the `HirFileId` this
172     /// `ItemTree` was created from.
173     pub fn top_level_items(&self) -> &[ModItem] {
174         &self.top_level
175     }
176
177     /// Returns the inner attributes of the source file.
178     pub fn top_level_attrs(&self) -> &Attrs {
179         self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&Attrs::EMPTY)
180     }
181
182     pub fn attrs(&self, of: AttrOwner) -> &Attrs {
183         self.attrs.get(&of).unwrap_or(&Attrs::EMPTY)
184     }
185
186     /// Returns the lowered inner items that `ast` corresponds to.
187     ///
188     /// Most AST items are lowered to a single `ModItem`, but some (eg. `use` items) may be lowered
189     /// to multiple items in the `ItemTree`.
190     pub fn inner_items(&self, ast: FileAstId<ast::Item>) -> &[ModItem] {
191         &self.inner_items[&ast]
192     }
193
194     pub fn all_inner_items(&self) -> impl Iterator<Item = ModItem> + '_ {
195         self.inner_items.values().flatten().copied()
196     }
197
198     pub fn source<S: ItemTreeNode>(&self, db: &dyn DefDatabase, of: ItemTreeId<S>) -> S::Source {
199         // This unwrap cannot fail, since it has either succeeded above, or resulted in an empty
200         // ItemTree (in which case there is no valid `FileItemTreeId` to call this method with).
201         let root =
202             db.parse_or_expand(of.file_id).expect("parse_or_expand failed on constructed ItemTree");
203
204         let id = self[of.value].ast_id();
205         let map = db.ast_id_map(of.file_id);
206         let ptr = map.get(id);
207         ptr.to_node(&root)
208     }
209
210     fn data(&self) -> &ItemTreeData {
211         self.data.as_ref().expect("attempted to access data of empty ItemTree")
212     }
213
214     fn data_mut(&mut self) -> &mut ItemTreeData {
215         self.data.get_or_insert_with(Box::default)
216     }
217 }
218
219 #[derive(Default, Debug, Eq, PartialEq)]
220 struct ItemVisibilities {
221     arena: Arena<RawVisibility>,
222 }
223
224 impl ItemVisibilities {
225     fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
226         match &vis {
227             RawVisibility::Public => RawVisibilityId::PUB,
228             RawVisibility::Module(path) if path.segments.is_empty() => match &path.kind {
229                 PathKind::Super(0) => RawVisibilityId::PRIV,
230                 PathKind::Crate => RawVisibilityId::PUB_CRATE,
231                 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
232             },
233             _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
234         }
235     }
236 }
237
238 static VIS_PUB: RawVisibility = RawVisibility::Public;
239 static VIS_PRIV: RawVisibility =
240     RawVisibility::Module(ModPath { kind: PathKind::Super(0), segments: Vec::new() });
241 static VIS_PUB_CRATE: RawVisibility =
242     RawVisibility::Module(ModPath { kind: PathKind::Crate, segments: Vec::new() });
243
244 #[derive(Default, Debug, Eq, PartialEq)]
245 struct GenericParamsStorage {
246     arena: Arena<GenericParams>,
247 }
248
249 impl GenericParamsStorage {
250     fn alloc(&mut self, params: GenericParams) -> GenericParamsId {
251         if params.types.is_empty() && params.where_predicates.is_empty() {
252             return GenericParamsId::EMPTY;
253         }
254
255         GenericParamsId(self.arena.alloc(params).into_raw().into())
256     }
257 }
258
259 static EMPTY_GENERICS: GenericParams =
260     GenericParams { types: Arena::new(), where_predicates: Vec::new() };
261
262 #[derive(Default, Debug, Eq, PartialEq)]
263 struct ItemTreeData {
264     imports: Arena<Import>,
265     extern_crates: Arena<ExternCrate>,
266     functions: Arena<Function>,
267     structs: Arena<Struct>,
268     fields: Arena<Field>,
269     unions: Arena<Union>,
270     enums: Arena<Enum>,
271     variants: Arena<Variant>,
272     consts: Arena<Const>,
273     statics: Arena<Static>,
274     traits: Arena<Trait>,
275     impls: Arena<Impl>,
276     type_aliases: Arena<TypeAlias>,
277     mods: Arena<Mod>,
278     macro_calls: Arena<MacroCall>,
279     exprs: Arena<Expr>,
280
281     vis: ItemVisibilities,
282     generics: GenericParamsStorage,
283 }
284
285 #[derive(Debug, Eq, PartialEq, Hash)]
286 pub enum AttrOwner {
287     /// Attributes on an item.
288     ModItem(ModItem),
289     /// Inner attributes of the source file.
290     TopLevel,
291
292     Variant(Idx<Variant>),
293     Field(Idx<Field>),
294 }
295
296 macro_rules! from_attrs {
297     ( $( $var:ident($t:ty) ),+ ) => {
298         $(
299             impl From<$t> for AttrOwner {
300                 fn from(t: $t) -> AttrOwner {
301                     AttrOwner::$var(t)
302                 }
303             }
304         )+
305     };
306 }
307
308 from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>));
309
310 /// Trait implemented by all item nodes in the item tree.
311 pub trait ItemTreeNode: Clone {
312     type Source: AstNode + Into<ast::Item>;
313
314     fn ast_id(&self) -> FileAstId<Self::Source>;
315
316     /// Looks up an instance of `Self` in an item tree.
317     fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
318
319     /// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
320     fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
321
322     /// Upcasts a `FileItemTreeId` to a generic `ModItem`.
323     fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
324 }
325
326 pub struct FileItemTreeId<N: ItemTreeNode> {
327     index: Idx<N>,
328     _p: PhantomData<N>,
329 }
330
331 impl<N: ItemTreeNode> Clone for FileItemTreeId<N> {
332     fn clone(&self) -> Self {
333         Self { index: self.index, _p: PhantomData }
334     }
335 }
336 impl<N: ItemTreeNode> Copy for FileItemTreeId<N> {}
337
338 impl<N: ItemTreeNode> PartialEq for FileItemTreeId<N> {
339     fn eq(&self, other: &FileItemTreeId<N>) -> bool {
340         self.index == other.index
341     }
342 }
343 impl<N: ItemTreeNode> Eq for FileItemTreeId<N> {}
344
345 impl<N: ItemTreeNode> Hash for FileItemTreeId<N> {
346     fn hash<H: Hasher>(&self, state: &mut H) {
347         self.index.hash(state)
348     }
349 }
350
351 impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
352     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353         self.index.fmt(f)
354     }
355 }
356
357 pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
358
359 macro_rules! mod_items {
360     ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
361         #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
362         pub enum ModItem {
363             $(
364                 $typ(FileItemTreeId<$typ>),
365             )+
366         }
367
368         $(
369             impl From<FileItemTreeId<$typ>> for ModItem {
370                 fn from(id: FileItemTreeId<$typ>) -> ModItem {
371                     ModItem::$typ(id)
372                 }
373             }
374         )+
375
376         $(
377             impl ItemTreeNode for $typ {
378                 type Source = $ast;
379
380                 fn ast_id(&self) -> FileAstId<Self::Source> {
381                     self.ast_id
382                 }
383
384                 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
385                     &tree.data().$fld[index]
386                 }
387
388                 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
389                     if let ModItem::$typ(id) = mod_item {
390                         Some(id)
391                     } else {
392                         None
393                     }
394                 }
395
396                 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
397                     ModItem::$typ(id)
398                 }
399             }
400
401             impl Index<Idx<$typ>> for ItemTree {
402                 type Output = $typ;
403
404                 fn index(&self, index: Idx<$typ>) -> &Self::Output {
405                     &self.data().$fld[index]
406                 }
407             }
408         )+
409     };
410 }
411
412 mod_items! {
413     Import in imports -> ast::Use,
414     ExternCrate in extern_crates -> ast::ExternCrate,
415     Function in functions -> ast::Fn,
416     Struct in structs -> ast::Struct,
417     Union in unions -> ast::Union,
418     Enum in enums -> ast::Enum,
419     Const in consts -> ast::Const,
420     Static in statics -> ast::Static,
421     Trait in traits -> ast::Trait,
422     Impl in impls -> ast::Impl,
423     TypeAlias in type_aliases -> ast::TypeAlias,
424     Mod in mods -> ast::Module,
425     MacroCall in macro_calls -> ast::MacroCall,
426 }
427
428 macro_rules! impl_index {
429     ( $($fld:ident: $t:ty),+ $(,)? ) => {
430         $(
431             impl Index<Idx<$t>> for ItemTree {
432                 type Output = $t;
433
434                 fn index(&self, index: Idx<$t>) -> &Self::Output {
435                     &self.data().$fld[index]
436                 }
437             }
438         )+
439     };
440 }
441
442 impl_index!(fields: Field, variants: Variant, exprs: Expr);
443
444 impl Index<RawVisibilityId> for ItemTree {
445     type Output = RawVisibility;
446     fn index(&self, index: RawVisibilityId) -> &Self::Output {
447         match index {
448             RawVisibilityId::PRIV => &VIS_PRIV,
449             RawVisibilityId::PUB => &VIS_PUB,
450             RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
451             _ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
452         }
453     }
454 }
455
456 impl Index<GenericParamsId> for ItemTree {
457     type Output = GenericParams;
458
459     fn index(&self, index: GenericParamsId) -> &Self::Output {
460         match index {
461             GenericParamsId::EMPTY => &EMPTY_GENERICS,
462             _ => &self.data().generics.arena[Idx::from_raw(index.0.into())],
463         }
464     }
465 }
466
467 impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
468     type Output = N;
469     fn index(&self, id: FileItemTreeId<N>) -> &N {
470         N::lookup(self, id.index)
471     }
472 }
473
474 /// A desugared `use` import.
475 #[derive(Debug, Clone, Eq, PartialEq)]
476 pub struct Import {
477     pub path: ModPath,
478     pub alias: Option<ImportAlias>,
479     pub visibility: RawVisibilityId,
480     pub is_glob: bool,
481     pub is_prelude: bool,
482     /// AST ID of the `use` or `extern crate` item this import was derived from. Note that many
483     /// `Import`s can map to the same `use` item.
484     pub ast_id: FileAstId<ast::Use>,
485     /// Index of this `Import` when the containing `Use` is visited via `ModPath::expand_use_item`.
486     ///
487     /// This can be used to get the `UseTree` this `Import` corresponds to and allows emitting
488     /// precise diagnostics.
489     pub index: usize,
490 }
491
492 #[derive(Debug, Clone, Eq, PartialEq)]
493 pub struct ExternCrate {
494     pub name: Name,
495     pub alias: Option<ImportAlias>,
496     pub visibility: RawVisibilityId,
497     /// Whether this is a `#[macro_use] extern crate ...`.
498     pub is_macro_use: bool,
499     pub ast_id: FileAstId<ast::ExternCrate>,
500 }
501
502 #[derive(Debug, Clone, Eq, PartialEq)]
503 pub struct Function {
504     pub name: Name,
505     pub visibility: RawVisibilityId,
506     pub generic_params: GenericParamsId,
507     pub has_self_param: bool,
508     pub has_body: bool,
509     pub is_unsafe: bool,
510     pub params: Box<[TypeRef]>,
511     pub is_varargs: bool,
512     pub ret_type: TypeRef,
513     pub ast_id: FileAstId<ast::Fn>,
514 }
515
516 #[derive(Debug, Clone, Eq, PartialEq)]
517 pub struct Struct {
518     pub name: Name,
519     pub visibility: RawVisibilityId,
520     pub generic_params: GenericParamsId,
521     pub fields: Fields,
522     pub ast_id: FileAstId<ast::Struct>,
523     pub kind: StructDefKind,
524 }
525
526 #[derive(Debug, Clone, Eq, PartialEq)]
527 pub enum StructDefKind {
528     /// `struct S { ... }` - type namespace only.
529     Record,
530     /// `struct S(...);`
531     Tuple,
532     /// `struct S;`
533     Unit,
534 }
535
536 #[derive(Debug, Clone, Eq, PartialEq)]
537 pub struct Union {
538     pub name: Name,
539     pub visibility: RawVisibilityId,
540     pub generic_params: GenericParamsId,
541     pub fields: Fields,
542     pub ast_id: FileAstId<ast::Union>,
543 }
544
545 #[derive(Debug, Clone, Eq, PartialEq)]
546 pub struct Enum {
547     pub name: Name,
548     pub visibility: RawVisibilityId,
549     pub generic_params: GenericParamsId,
550     pub variants: IdRange<Variant>,
551     pub ast_id: FileAstId<ast::Enum>,
552 }
553
554 #[derive(Debug, Clone, Eq, PartialEq)]
555 pub struct Const {
556     /// const _: () = ();
557     pub name: Option<Name>,
558     pub visibility: RawVisibilityId,
559     pub type_ref: TypeRef,
560     pub ast_id: FileAstId<ast::Const>,
561 }
562
563 #[derive(Debug, Clone, Eq, PartialEq)]
564 pub struct Static {
565     pub name: Name,
566     pub visibility: RawVisibilityId,
567     pub mutable: bool,
568     pub type_ref: TypeRef,
569     pub ast_id: FileAstId<ast::Static>,
570 }
571
572 #[derive(Debug, Clone, Eq, PartialEq)]
573 pub struct Trait {
574     pub name: Name,
575     pub visibility: RawVisibilityId,
576     pub generic_params: GenericParamsId,
577     pub auto: bool,
578     pub items: Box<[AssocItem]>,
579     pub ast_id: FileAstId<ast::Trait>,
580 }
581
582 #[derive(Debug, Clone, Eq, PartialEq)]
583 pub struct Impl {
584     pub generic_params: GenericParamsId,
585     pub target_trait: Option<TypeRef>,
586     pub target_type: TypeRef,
587     pub is_negative: bool,
588     pub items: Box<[AssocItem]>,
589     pub ast_id: FileAstId<ast::Impl>,
590 }
591
592 #[derive(Debug, Clone, PartialEq, Eq)]
593 pub struct TypeAlias {
594     pub name: Name,
595     pub visibility: RawVisibilityId,
596     /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
597     pub bounds: Box<[TypeBound]>,
598     pub generic_params: GenericParamsId,
599     pub type_ref: Option<TypeRef>,
600     pub is_extern: bool,
601     pub ast_id: FileAstId<ast::TypeAlias>,
602 }
603
604 #[derive(Debug, Clone, Eq, PartialEq)]
605 pub struct Mod {
606     pub name: Name,
607     pub visibility: RawVisibilityId,
608     pub kind: ModKind,
609     pub ast_id: FileAstId<ast::Module>,
610 }
611
612 #[derive(Debug, Clone, Eq, PartialEq)]
613 pub enum ModKind {
614     /// `mod m { ... }`
615     Inline { items: Box<[ModItem]> },
616
617     /// `mod m;`
618     Outline {},
619 }
620
621 #[derive(Debug, Clone, Eq, PartialEq)]
622 pub struct MacroCall {
623     /// For `macro_rules!` declarations, this is the name of the declared macro.
624     pub name: Option<Name>,
625     /// Path to the called macro.
626     pub path: ModPath,
627     /// Has `#[macro_export]`.
628     pub is_export: bool,
629     /// Has `#[macro_export(local_inner_macros)]`.
630     pub is_local_inner: bool,
631     /// Has `#[rustc_builtin_macro]`.
632     pub is_builtin: bool,
633     pub ast_id: FileAstId<ast::MacroCall>,
634 }
635
636 // NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
637 // lengths, but we don't do much with them yet.
638 #[derive(Debug, Clone, Eq, PartialEq)]
639 pub struct Expr;
640
641 macro_rules! impl_froms {
642     ($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
643         $(
644             impl From<$t> for $e {
645                 fn from(it: $t) -> $e {
646                     $e::$v(it)
647                 }
648             }
649         )*
650     }
651 }
652
653 impl ModItem {
654     pub fn as_assoc_item(&self) -> Option<AssocItem> {
655         match self {
656             ModItem::Import(_)
657             | ModItem::ExternCrate(_)
658             | ModItem::Struct(_)
659             | ModItem::Union(_)
660             | ModItem::Enum(_)
661             | ModItem::Static(_)
662             | ModItem::Trait(_)
663             | ModItem::Impl(_)
664             | ModItem::Mod(_) => None,
665             ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
666             ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
667             ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
668             ModItem::Function(func) => Some(AssocItem::Function(*func)),
669         }
670     }
671
672     pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
673         N::id_from_mod_item(self)
674     }
675 }
676
677 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
678 pub enum AssocItem {
679     Function(FileItemTreeId<Function>),
680     TypeAlias(FileItemTreeId<TypeAlias>),
681     Const(FileItemTreeId<Const>),
682     MacroCall(FileItemTreeId<MacroCall>),
683 }
684
685 impl_froms!(AssocItem {
686     Function(FileItemTreeId<Function>),
687     TypeAlias(FileItemTreeId<TypeAlias>),
688     Const(FileItemTreeId<Const>),
689     MacroCall(FileItemTreeId<MacroCall>),
690 });
691
692 impl From<AssocItem> for ModItem {
693     fn from(item: AssocItem) -> Self {
694         match item {
695             AssocItem::Function(it) => it.into(),
696             AssocItem::TypeAlias(it) => it.into(),
697             AssocItem::Const(it) => it.into(),
698             AssocItem::MacroCall(it) => it.into(),
699         }
700     }
701 }
702
703 #[derive(Debug, Eq, PartialEq)]
704 pub struct Variant {
705     pub name: Name,
706     pub fields: Fields,
707 }
708
709 pub struct IdRange<T> {
710     range: Range<u32>,
711     _p: PhantomData<T>,
712 }
713
714 impl<T> IdRange<T> {
715     fn new(range: Range<Idx<T>>) -> Self {
716         Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
717     }
718 }
719
720 impl<T> Iterator for IdRange<T> {
721     type Item = Idx<T>;
722     fn next(&mut self) -> Option<Self::Item> {
723         self.range.next().map(|raw| Idx::from_raw(raw.into()))
724     }
725 }
726
727 impl<T> fmt::Debug for IdRange<T> {
728     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
729         f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
730     }
731 }
732
733 impl<T> Clone for IdRange<T> {
734     fn clone(&self) -> Self {
735         Self { range: self.range.clone(), _p: PhantomData }
736     }
737 }
738
739 impl<T> PartialEq for IdRange<T> {
740     fn eq(&self, other: &Self) -> bool {
741         self.range == other.range
742     }
743 }
744
745 impl<T> Eq for IdRange<T> {}
746
747 #[derive(Debug, Clone, PartialEq, Eq)]
748 pub enum Fields {
749     Record(IdRange<Field>),
750     Tuple(IdRange<Field>),
751     Unit,
752 }
753
754 /// A single field of an enum variant or struct
755 #[derive(Debug, Clone, PartialEq, Eq)]
756 pub struct Field {
757     pub name: Name,
758     pub type_ref: TypeRef,
759     pub visibility: RawVisibilityId,
760 }