]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/item_tree.rs
Merge #6016
[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 path: ModPath,
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 is_unsafe: bool,
509     pub params: Box<[TypeRef]>,
510     pub is_varargs: bool,
511     pub ret_type: TypeRef,
512     pub ast_id: FileAstId<ast::Fn>,
513 }
514
515 #[derive(Debug, Clone, Eq, PartialEq)]
516 pub struct Struct {
517     pub name: Name,
518     pub visibility: RawVisibilityId,
519     pub generic_params: GenericParamsId,
520     pub fields: Fields,
521     pub ast_id: FileAstId<ast::Struct>,
522     pub kind: StructDefKind,
523 }
524
525 #[derive(Debug, Clone, Eq, PartialEq)]
526 pub enum StructDefKind {
527     /// `struct S { ... }` - type namespace only.
528     Record,
529     /// `struct S(...);`
530     Tuple,
531     /// `struct S;`
532     Unit,
533 }
534
535 #[derive(Debug, Clone, Eq, PartialEq)]
536 pub struct Union {
537     pub name: Name,
538     pub visibility: RawVisibilityId,
539     pub generic_params: GenericParamsId,
540     pub fields: Fields,
541     pub ast_id: FileAstId<ast::Union>,
542 }
543
544 #[derive(Debug, Clone, Eq, PartialEq)]
545 pub struct Enum {
546     pub name: Name,
547     pub visibility: RawVisibilityId,
548     pub generic_params: GenericParamsId,
549     pub variants: IdRange<Variant>,
550     pub ast_id: FileAstId<ast::Enum>,
551 }
552
553 #[derive(Debug, Clone, Eq, PartialEq)]
554 pub struct Const {
555     /// const _: () = ();
556     pub name: Option<Name>,
557     pub visibility: RawVisibilityId,
558     pub type_ref: TypeRef,
559     pub ast_id: FileAstId<ast::Const>,
560 }
561
562 #[derive(Debug, Clone, Eq, PartialEq)]
563 pub struct Static {
564     pub name: Name,
565     pub visibility: RawVisibilityId,
566     pub mutable: bool,
567     pub type_ref: TypeRef,
568     pub ast_id: FileAstId<ast::Static>,
569 }
570
571 #[derive(Debug, Clone, Eq, PartialEq)]
572 pub struct Trait {
573     pub name: Name,
574     pub visibility: RawVisibilityId,
575     pub generic_params: GenericParamsId,
576     pub auto: bool,
577     pub items: Box<[AssocItem]>,
578     pub ast_id: FileAstId<ast::Trait>,
579 }
580
581 #[derive(Debug, Clone, Eq, PartialEq)]
582 pub struct Impl {
583     pub generic_params: GenericParamsId,
584     pub target_trait: Option<TypeRef>,
585     pub target_type: TypeRef,
586     pub is_negative: bool,
587     pub items: Box<[AssocItem]>,
588     pub ast_id: FileAstId<ast::Impl>,
589 }
590
591 #[derive(Debug, Clone, PartialEq, Eq)]
592 pub struct TypeAlias {
593     pub name: Name,
594     pub visibility: RawVisibilityId,
595     /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
596     pub bounds: Box<[TypeBound]>,
597     pub generic_params: GenericParamsId,
598     pub type_ref: Option<TypeRef>,
599     pub is_extern: bool,
600     pub ast_id: FileAstId<ast::TypeAlias>,
601 }
602
603 #[derive(Debug, Clone, Eq, PartialEq)]
604 pub struct Mod {
605     pub name: Name,
606     pub visibility: RawVisibilityId,
607     pub kind: ModKind,
608     pub ast_id: FileAstId<ast::Module>,
609 }
610
611 #[derive(Debug, Clone, Eq, PartialEq)]
612 pub enum ModKind {
613     /// `mod m { ... }`
614     Inline { items: Box<[ModItem]> },
615
616     /// `mod m;`
617     Outline {},
618 }
619
620 #[derive(Debug, Clone, Eq, PartialEq)]
621 pub struct MacroCall {
622     /// For `macro_rules!` declarations, this is the name of the declared macro.
623     pub name: Option<Name>,
624     /// Path to the called macro.
625     pub path: ModPath,
626     /// Has `#[macro_export]`.
627     pub is_export: bool,
628     /// Has `#[macro_export(local_inner_macros)]`.
629     pub is_local_inner: bool,
630     /// Has `#[rustc_builtin_macro]`.
631     pub is_builtin: bool,
632     pub ast_id: FileAstId<ast::MacroCall>,
633 }
634
635 // NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
636 // lengths, but we don't do much with them yet.
637 #[derive(Debug, Clone, Eq, PartialEq)]
638 pub struct Expr;
639
640 macro_rules! impl_froms {
641     ($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
642         $(
643             impl From<$t> for $e {
644                 fn from(it: $t) -> $e {
645                     $e::$v(it)
646                 }
647             }
648         )*
649     }
650 }
651
652 impl ModItem {
653     pub fn as_assoc_item(&self) -> Option<AssocItem> {
654         match self {
655             ModItem::Import(_)
656             | ModItem::ExternCrate(_)
657             | ModItem::Struct(_)
658             | ModItem::Union(_)
659             | ModItem::Enum(_)
660             | ModItem::Static(_)
661             | ModItem::Trait(_)
662             | ModItem::Impl(_)
663             | ModItem::Mod(_) => None,
664             ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
665             ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
666             ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
667             ModItem::Function(func) => Some(AssocItem::Function(*func)),
668         }
669     }
670
671     pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
672         N::id_from_mod_item(self)
673     }
674 }
675
676 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
677 pub enum AssocItem {
678     Function(FileItemTreeId<Function>),
679     TypeAlias(FileItemTreeId<TypeAlias>),
680     Const(FileItemTreeId<Const>),
681     MacroCall(FileItemTreeId<MacroCall>),
682 }
683
684 impl_froms!(AssocItem {
685     Function(FileItemTreeId<Function>),
686     TypeAlias(FileItemTreeId<TypeAlias>),
687     Const(FileItemTreeId<Const>),
688     MacroCall(FileItemTreeId<MacroCall>),
689 });
690
691 impl From<AssocItem> for ModItem {
692     fn from(item: AssocItem) -> Self {
693         match item {
694             AssocItem::Function(it) => it.into(),
695             AssocItem::TypeAlias(it) => it.into(),
696             AssocItem::Const(it) => it.into(),
697             AssocItem::MacroCall(it) => it.into(),
698         }
699     }
700 }
701
702 #[derive(Debug, Eq, PartialEq)]
703 pub struct Variant {
704     pub name: Name,
705     pub fields: Fields,
706 }
707
708 pub struct IdRange<T> {
709     range: Range<u32>,
710     _p: PhantomData<T>,
711 }
712
713 impl<T> IdRange<T> {
714     fn new(range: Range<Idx<T>>) -> Self {
715         Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
716     }
717 }
718
719 impl<T> Iterator for IdRange<T> {
720     type Item = Idx<T>;
721     fn next(&mut self) -> Option<Self::Item> {
722         self.range.next().map(|raw| Idx::from_raw(raw.into()))
723     }
724 }
725
726 impl<T> fmt::Debug for IdRange<T> {
727     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
728         f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
729     }
730 }
731
732 impl<T> Clone for IdRange<T> {
733     fn clone(&self) -> Self {
734         Self { range: self.range.clone(), _p: PhantomData }
735     }
736 }
737
738 impl<T> PartialEq for IdRange<T> {
739     fn eq(&self, other: &Self) -> bool {
740         self.range == other.range
741     }
742 }
743
744 impl<T> Eq for IdRange<T> {}
745
746 #[derive(Debug, Clone, PartialEq, Eq)]
747 pub enum Fields {
748     Record(IdRange<Field>),
749     Tuple(IdRange<Field>),
750     Unit,
751 }
752
753 /// A single field of an enum variant or struct
754 #[derive(Debug, Clone, PartialEq, Eq)]
755 pub struct Field {
756     pub name: Name,
757     pub type_ref: TypeRef,
758     pub visibility: RawVisibilityId,
759 }