1 //! A simplified AST that only contains items.
3 //! This is the primary IR used throughout `hir_def`. It is the input to the name resolution
4 //! algorithm, as well as to the queries defined in `adt.rs`, `data.rs`, and most things in
7 //! `ItemTree`s are built per `HirFileId`, from the syntax tree of the parsed file. This means that
8 //! they are crate-independent: they don't know which `#[cfg]`s are active or which module they
9 //! belong to, since those concepts don't exist at this level (a single `ItemTree` might be part of
10 //! multiple crates, or might be included into the same crate twice via `#[path]`).
12 //! One important purpose of this layer is to provide an "invalidation barrier" for incremental
13 //! computations: when typing inside an item body, the `ItemTree` of the modified file is typically
14 //! unaffected, so we don't have to recompute name resolution results or item data (see `data.rs`).
16 //! The `ItemTree` for the currently open file can be displayed by using the VS Code command
17 //! "Rust Analyzer: Debug ItemTree".
19 //! Compared to rustc's architecture, `ItemTree` has properties from both rustc's AST and HIR: many
20 //! syntax-level Rust features are already desugared to simpler forms in the `ItemTree`, but name
21 //! resolution has not yet been performed. `ItemTree`s are per-file, while rustc's AST and HIR are
22 //! per-crate, because we are interested in incrementally computing it.
24 //! The representation of items in the `ItemTree` should generally mirror the surface syntax: it is
25 //! usually a bad idea to desugar a syntax-level construct to something that is structurally
26 //! different here. Name resolution needs to be able to process attributes and expand macros
27 //! (including attribute macros), and having a 1-to-1 mapping between syntax and the `ItemTree`
28 //! avoids introducing subtle bugs.
30 //! In general, any item in the `ItemTree` stores its `AstId`, which allows mapping it back to its
47 use ast::{AstNode, HasName, StructKind};
51 ast_id_map::FileAstId,
53 name::{name, AsName, Name},
54 ExpandTo, HirFileId, InFile,
56 use la_arena::{Arena, Idx, RawIdx};
58 use rustc_hash::FxHashMap;
59 use smallvec::SmallVec;
60 use syntax::{ast, match_ast, SyntaxKind};
63 attr::{Attrs, RawAttrs},
65 generics::GenericParams,
67 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
68 type_ref::{Mutability, TraitRef, TypeBound, TypeRef},
69 visibility::RawVisibility,
73 #[derive(Copy, Clone, Eq, PartialEq)]
74 pub struct RawVisibilityId(u32);
76 impl RawVisibilityId {
77 pub const PUB: Self = RawVisibilityId(u32::max_value());
78 pub const PRIV: Self = RawVisibilityId(u32::max_value() - 1);
79 pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 2);
82 impl fmt::Debug for RawVisibilityId {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 let mut f = f.debug_tuple("RawVisibilityId");
86 Self::PUB => f.field(&"pub"),
87 Self::PRIV => f.field(&"pub(self)"),
88 Self::PUB_CRATE => f.field(&"pub(crate)"),
89 _ => f.field(&self.0),
95 /// The item tree of a source file.
96 #[derive(Debug, Default, Eq, PartialEq)]
100 top_level: SmallVec<[ModItem; 1]>,
101 attrs: FxHashMap<AttrOwner, RawAttrs>,
103 data: Option<Box<ItemTreeData>>,
107 pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
108 let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id));
109 let syntax = if let Some(node) = db.parse_or_expand(file_id) {
110 if node.kind() == SyntaxKind::ERROR {
111 // FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
112 return Default::default();
116 return Default::default();
119 let hygiene = Hygiene::new(db.upcast(), file_id);
120 let ctx = lower::Ctx::new(db, hygiene.clone(), file_id);
121 let mut top_attrs = None;
122 let mut item_tree = match_ast! {
124 ast::SourceFile(file) => {
125 top_attrs = Some(RawAttrs::new(db, &file, &hygiene));
126 ctx.lower_module_items(&file)
128 ast::MacroItems(items) => {
129 ctx.lower_module_items(&items)
131 ast::MacroStmts(stmts) => {
132 // The produced statements can include items, which should be added as top-level
134 ctx.lower_macro_stmts(stmts)
137 // FIXME: This occurs because macros in pattern position are treated as inner
138 // items and expanded during block DefMap computation
139 return Default::default();
142 // Types can contain inner items. We return an empty item tree in this case, but
143 // still need to collect inner items.
144 ctx.lower_inner_items(ty.syntax())
147 // Macros can expand to expressions. We return an empty item tree in this case, but
148 // still need to collect inner items.
149 ctx.lower_inner_items(e.syntax())
152 panic!("cannot create item tree from {:?} {}", syntax, syntax);
157 if let Some(attrs) = top_attrs {
158 item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
160 item_tree.shrink_to_fit();
164 fn shrink_to_fit(&mut self) {
165 if let Some(data) = &mut self.data {
190 imports.shrink_to_fit();
191 extern_crates.shrink_to_fit();
192 extern_blocks.shrink_to_fit();
193 functions.shrink_to_fit();
194 params.shrink_to_fit();
195 structs.shrink_to_fit();
196 fields.shrink_to_fit();
197 unions.shrink_to_fit();
198 enums.shrink_to_fit();
199 variants.shrink_to_fit();
200 consts.shrink_to_fit();
201 statics.shrink_to_fit();
202 traits.shrink_to_fit();
203 impls.shrink_to_fit();
204 type_aliases.shrink_to_fit();
205 mods.shrink_to_fit();
206 macro_calls.shrink_to_fit();
207 macro_rules.shrink_to_fit();
208 macro_defs.shrink_to_fit();
210 vis.arena.shrink_to_fit();
212 inner_items.shrink_to_fit();
216 /// Returns an iterator over all items located at the top level of the `HirFileId` this
217 /// `ItemTree` was created from.
218 pub fn top_level_items(&self) -> &[ModItem] {
222 /// Returns the inner attributes of the source file.
223 pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
224 self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone().filter(db, krate)
227 pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
228 self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
231 pub fn attrs(&self, db: &dyn DefDatabase, krate: CrateId, of: AttrOwner) -> Attrs {
232 self.raw_attrs(of).clone().filter(db, krate)
235 pub fn inner_items_of_block(&self, block: FileAstId<ast::BlockExpr>) -> &[ModItem] {
237 Some(data) => data.inner_items.get(&block).map(|it| &**it).unwrap_or(&[]),
242 pub fn pretty_print(&self) -> String {
243 pretty::print_item_tree(self)
246 fn data(&self) -> &ItemTreeData {
247 self.data.as_ref().expect("attempted to access data of empty ItemTree")
250 fn data_mut(&mut self) -> &mut ItemTreeData {
251 self.data.get_or_insert_with(Box::default)
255 #[derive(Default, Debug, Eq, PartialEq)]
256 struct ItemVisibilities {
257 arena: Arena<RawVisibility>,
260 impl ItemVisibilities {
261 fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
263 RawVisibility::Public => RawVisibilityId::PUB,
264 RawVisibility::Module(path) if path.segments().is_empty() => match &path.kind {
265 PathKind::Super(0) => RawVisibilityId::PRIV,
266 PathKind::Crate => RawVisibilityId::PUB_CRATE,
267 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
269 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
274 static VIS_PUB: RawVisibility = RawVisibility::Public;
275 static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)));
276 static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate));
278 #[derive(Default, Debug, Eq, PartialEq)]
279 struct ItemTreeData {
280 imports: Arena<Import>,
281 extern_crates: Arena<ExternCrate>,
282 extern_blocks: Arena<ExternBlock>,
283 functions: Arena<Function>,
284 params: Arena<Param>,
285 structs: Arena<Struct>,
286 fields: Arena<Field>,
287 unions: Arena<Union>,
289 variants: Arena<Variant>,
290 consts: Arena<Const>,
291 statics: Arena<Static>,
292 traits: Arena<Trait>,
294 type_aliases: Arena<TypeAlias>,
296 macro_calls: Arena<MacroCall>,
297 macro_rules: Arena<MacroRules>,
298 macro_defs: Arena<MacroDef>,
300 vis: ItemVisibilities,
302 inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>,
305 #[derive(Debug, Eq, PartialEq, Hash)]
307 /// Attributes on an item.
309 /// Inner attributes of the source file.
312 Variant(Idx<Variant>),
317 macro_rules! from_attrs {
318 ( $( $var:ident($t:ty) ),+ ) => {
320 impl From<$t> for AttrOwner {
321 fn from(t: $t) -> AttrOwner {
329 from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>), Param(Idx<Param>));
331 /// Trait implemented by all item nodes in the item tree.
332 pub trait ItemTreeNode: Clone {
333 type Source: AstNode + Into<ast::Item>;
335 fn ast_id(&self) -> FileAstId<Self::Source>;
337 /// Looks up an instance of `Self` in an item tree.
338 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
340 /// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
341 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
343 /// Upcasts a `FileItemTreeId` to a generic `ModItem`.
344 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
347 pub struct FileItemTreeId<N: ItemTreeNode> {
352 impl<N: ItemTreeNode> Clone for FileItemTreeId<N> {
353 fn clone(&self) -> Self {
354 Self { index: self.index, _p: PhantomData }
357 impl<N: ItemTreeNode> Copy for FileItemTreeId<N> {}
359 impl<N: ItemTreeNode> PartialEq for FileItemTreeId<N> {
360 fn eq(&self, other: &FileItemTreeId<N>) -> bool {
361 self.index == other.index
364 impl<N: ItemTreeNode> Eq for FileItemTreeId<N> {}
366 impl<N: ItemTreeNode> Hash for FileItemTreeId<N> {
367 fn hash<H: Hasher>(&self, state: &mut H) {
368 self.index.hash(state)
372 impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
373 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
378 /// Identifies a particular [`ItemTree`].
379 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
382 block: Option<BlockId>,
386 pub(crate) fn new(file: HirFileId, block: Option<BlockId>) -> Self {
390 pub(crate) fn item_tree(&self, db: &dyn DefDatabase) -> Arc<ItemTree> {
392 Some(_) => unreachable!("per-block ItemTrees are not yet implemented"),
393 None => db.file_item_tree(self.file),
397 pub(crate) fn file_id(self) -> HirFileId {
403 pub struct ItemTreeId<N: ItemTreeNode> {
405 pub value: FileItemTreeId<N>,
408 impl<N: ItemTreeNode> ItemTreeId<N> {
409 pub fn new(tree: TreeId, idx: FileItemTreeId<N>) -> Self {
410 Self { tree, value: idx }
413 pub fn file_id(self) -> HirFileId {
417 pub fn tree_id(self) -> TreeId {
421 pub fn item_tree(self, db: &dyn DefDatabase) -> Arc<ItemTree> {
422 self.tree.item_tree(db)
426 impl<N: ItemTreeNode> Copy for ItemTreeId<N> {}
427 impl<N: ItemTreeNode> Clone for ItemTreeId<N> {
428 fn clone(&self) -> Self {
433 impl<N: ItemTreeNode> PartialEq for ItemTreeId<N> {
434 fn eq(&self, other: &Self) -> bool {
435 self.tree == other.tree && self.value == other.value
439 impl<N: ItemTreeNode> Eq for ItemTreeId<N> {}
441 impl<N: ItemTreeNode> Hash for ItemTreeId<N> {
442 fn hash<H: Hasher>(&self, state: &mut H) {
443 self.tree.hash(state);
444 self.value.hash(state);
448 macro_rules! mod_items {
449 ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
450 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
453 $typ(FileItemTreeId<$typ>),
458 impl From<FileItemTreeId<$typ>> for ModItem {
459 fn from(id: FileItemTreeId<$typ>) -> ModItem {
466 impl ItemTreeNode for $typ {
469 fn ast_id(&self) -> FileAstId<Self::Source> {
473 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
474 &tree.data().$fld[index]
477 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
479 ModItem::$typ(id) => Some(id),
484 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
489 impl Index<Idx<$typ>> for ItemTree {
492 fn index(&self, index: Idx<$typ>) -> &Self::Output {
493 &self.data().$fld[index]
501 Import in imports -> ast::Use,
502 ExternCrate in extern_crates -> ast::ExternCrate,
503 ExternBlock in extern_blocks -> ast::ExternBlock,
504 Function in functions -> ast::Fn,
505 Struct in structs -> ast::Struct,
506 Union in unions -> ast::Union,
507 Enum in enums -> ast::Enum,
508 Const in consts -> ast::Const,
509 Static in statics -> ast::Static,
510 Trait in traits -> ast::Trait,
511 Impl in impls -> ast::Impl,
512 TypeAlias in type_aliases -> ast::TypeAlias,
513 Mod in mods -> ast::Module,
514 MacroCall in macro_calls -> ast::MacroCall,
515 MacroRules in macro_rules -> ast::MacroRules,
516 MacroDef in macro_defs -> ast::MacroDef,
519 macro_rules! impl_index {
520 ( $($fld:ident: $t:ty),+ $(,)? ) => {
522 impl Index<Idx<$t>> for ItemTree {
525 fn index(&self, index: Idx<$t>) -> &Self::Output {
526 &self.data().$fld[index]
533 impl_index!(fields: Field, variants: Variant, params: Param);
535 impl Index<RawVisibilityId> for ItemTree {
536 type Output = RawVisibility;
537 fn index(&self, index: RawVisibilityId) -> &Self::Output {
539 RawVisibilityId::PRIV => &VIS_PRIV,
540 RawVisibilityId::PUB => &VIS_PUB,
541 RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
542 _ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
547 impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
549 fn index(&self, id: FileItemTreeId<N>) -> &N {
550 N::lookup(self, id.index)
554 #[derive(Debug, Clone, Eq, PartialEq)]
556 pub visibility: RawVisibilityId,
557 pub ast_id: FileAstId<ast::Use>,
558 pub use_tree: UseTree,
561 #[derive(Debug, Clone, Eq, PartialEq)]
563 pub index: Idx<ast::UseTree>,
567 #[derive(Debug, Clone, Eq, PartialEq)]
568 pub enum UseTreeKind {
570 /// use path::to::Item;
571 /// use path::to::Item as Renamed;
572 /// use path::to::Trait as _;
574 Single { path: Interned<ModPath>, alias: Option<ImportAlias> },
577 /// use *; // (invalid, but can occur in nested tree)
580 Glob { path: Option<Interned<ModPath>> },
583 /// use prefix::{self, Item, ...};
585 Prefixed { prefix: Option<Interned<ModPath>>, list: Box<[UseTree]> },
588 #[derive(Debug, Clone, Eq, PartialEq)]
589 pub struct ExternCrate {
591 pub alias: Option<ImportAlias>,
592 pub visibility: RawVisibilityId,
593 pub ast_id: FileAstId<ast::ExternCrate>,
596 #[derive(Debug, Clone, Eq, PartialEq)]
597 pub struct ExternBlock {
598 pub abi: Option<Interned<str>>,
599 pub ast_id: FileAstId<ast::ExternBlock>,
600 pub children: Box<[ModItem]>,
603 #[derive(Debug, Clone, Eq, PartialEq)]
604 pub struct Function {
606 pub visibility: RawVisibilityId,
607 pub explicit_generic_params: Interned<GenericParams>,
608 pub abi: Option<Interned<str>>,
609 pub params: IdRange<Param>,
610 pub ret_type: Interned<TypeRef>,
611 pub async_ret_type: Option<Interned<TypeRef>>,
612 pub ast_id: FileAstId<ast::Fn>,
613 pub(crate) flags: FnFlags,
616 #[derive(Debug, Clone, Eq, PartialEq)]
618 Normal(Interned<TypeRef>),
622 #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
623 pub(crate) struct FnFlags {
627 pub(crate) const HAS_SELF_PARAM: u8 = 1 << 0;
628 pub(crate) const HAS_BODY: u8 = 1 << 1;
629 pub(crate) const IS_DEFAULT: u8 = 1 << 2;
630 pub(crate) const IS_CONST: u8 = 1 << 3;
631 pub(crate) const IS_ASYNC: u8 = 1 << 4;
632 pub(crate) const IS_UNSAFE: u8 = 1 << 5;
633 /// Whether the function is located in an `extern` block (*not* whether it is an
634 /// `extern "abi" fn`).
635 pub(crate) const IS_IN_EXTERN_BLOCK: u8 = 1 << 6;
636 pub(crate) const IS_VARARGS: u8 = 1 << 7;
639 #[derive(Debug, Clone, Eq, PartialEq)]
642 pub visibility: RawVisibilityId,
643 pub generic_params: Interned<GenericParams>,
645 pub ast_id: FileAstId<ast::Struct>,
648 #[derive(Debug, Clone, Eq, PartialEq)]
651 pub visibility: RawVisibilityId,
652 pub generic_params: Interned<GenericParams>,
654 pub ast_id: FileAstId<ast::Union>,
657 #[derive(Debug, Clone, Eq, PartialEq)]
660 pub visibility: RawVisibilityId,
661 pub generic_params: Interned<GenericParams>,
662 pub variants: IdRange<Variant>,
663 pub ast_id: FileAstId<ast::Enum>,
666 #[derive(Debug, Clone, Eq, PartialEq)]
668 /// const _: () = ();
669 pub name: Option<Name>,
670 pub visibility: RawVisibilityId,
671 pub type_ref: Interned<TypeRef>,
672 pub ast_id: FileAstId<ast::Const>,
675 #[derive(Debug, Clone, Eq, PartialEq)]
678 pub visibility: RawVisibilityId,
680 /// Whether the static is in an `extern` block.
682 pub type_ref: Interned<TypeRef>,
683 pub ast_id: FileAstId<ast::Static>,
686 #[derive(Debug, Clone, Eq, PartialEq)]
689 pub visibility: RawVisibilityId,
690 pub generic_params: Interned<GenericParams>,
693 pub items: Box<[AssocItem]>,
694 pub ast_id: FileAstId<ast::Trait>,
697 #[derive(Debug, Clone, Eq, PartialEq)]
699 pub generic_params: Interned<GenericParams>,
700 pub target_trait: Option<Interned<TraitRef>>,
701 pub self_ty: Interned<TypeRef>,
702 pub is_negative: bool,
703 pub items: Box<[AssocItem]>,
704 pub ast_id: FileAstId<ast::Impl>,
707 #[derive(Debug, Clone, PartialEq, Eq)]
708 pub struct TypeAlias {
710 pub visibility: RawVisibilityId,
711 /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
712 pub bounds: Box<[Interned<TypeBound>]>,
713 pub generic_params: Interned<GenericParams>,
714 pub type_ref: Option<Interned<TypeRef>>,
716 pub ast_id: FileAstId<ast::TypeAlias>,
719 #[derive(Debug, Clone, Eq, PartialEq)]
722 pub visibility: RawVisibilityId,
724 pub ast_id: FileAstId<ast::Module>,
727 #[derive(Debug, Clone, Eq, PartialEq)]
730 Inline { items: Box<[ModItem]> },
736 #[derive(Debug, Clone, Eq, PartialEq)]
737 pub struct MacroCall {
738 /// Path to the called macro.
739 pub path: Interned<ModPath>,
740 pub ast_id: FileAstId<ast::MacroCall>,
741 pub expand_to: ExpandTo,
744 #[derive(Debug, Clone, Eq, PartialEq)]
745 pub struct MacroRules {
746 /// The name of the declared macro.
748 pub ast_id: FileAstId<ast::MacroRules>,
751 /// "Macros 2.0" macro definition.
752 #[derive(Debug, Clone, Eq, PartialEq)]
753 pub struct MacroDef {
755 pub visibility: RawVisibilityId,
756 pub ast_id: FileAstId<ast::MacroDef>,
760 /// Maps a `UseTree` contained in this import back to its AST node.
761 pub fn use_tree_to_ast(
763 db: &dyn DefDatabase,
765 index: Idx<ast::UseTree>,
767 // Re-lower the AST item and get the source map.
768 // Note: The AST unwraps are fine, since if they fail we should have never obtained `index`.
769 let ast = InFile::new(file_id, self.ast_id).to_node(db.upcast());
770 let ast_use_tree = ast.use_tree().expect("missing `use_tree`");
771 let hygiene = Hygiene::new(db.upcast(), file_id);
772 let (_, source_map) =
773 lower::lower_use_tree(db, &hygiene, ast_use_tree).expect("failed to lower use tree");
774 source_map[index].clone()
778 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
779 pub enum ImportKind {
780 /// The `ModPath` is imported normally.
782 /// This is a glob-import of all names in the `ModPath`.
784 /// This is a `some::path::self` import, which imports `some::path` only in type namespace.
789 /// Expands the `UseTree` into individually imported `ModPath`s.
792 mut cb: impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
794 self.expand_impl(None, &mut cb)
799 prefix: Option<ModPath>,
800 cb: &mut dyn FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
803 prefix: Option<ModPath>,
805 ) -> Option<(ModPath, ImportKind)> {
806 match (prefix, &path.kind) {
807 (None, _) => Some((path.clone(), ImportKind::Plain)),
808 (Some(mut prefix), PathKind::Plain) => {
809 for segment in path.segments() {
810 prefix.push_segment(segment.clone());
812 Some((prefix, ImportKind::Plain))
814 (Some(prefix), PathKind::Super(0)) => {
815 // `some::path::self` == `some::path`
816 if path.segments().is_empty() {
817 Some((prefix, ImportKind::TypeOnly))
822 (Some(_), _) => None,
827 UseTreeKind::Single { path, alias } => {
828 if let Some((path, kind)) = concat_mod_paths(prefix, path) {
829 cb(self.index, path, kind, alias.clone());
832 UseTreeKind::Glob { path: Some(path) } => {
833 if let Some((path, _)) = concat_mod_paths(prefix, path) {
834 cb(self.index, path, ImportKind::Glob, None);
837 UseTreeKind::Glob { path: None } => {
838 if let Some(prefix) = prefix {
839 cb(self.index, prefix, ImportKind::Glob, None);
842 UseTreeKind::Prefixed { prefix: additional_prefix, list } => {
843 let prefix = match additional_prefix {
844 Some(path) => match concat_mod_paths(prefix, path) {
845 Some((path, ImportKind::Plain)) => Some(path),
850 for tree in &**list {
851 tree.expand_impl(prefix.clone(), cb);
858 macro_rules! impl_froms {
859 ($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
861 impl From<$t> for $e {
862 fn from(it: $t) -> $e {
871 pub fn as_assoc_item(&self) -> Option<AssocItem> {
874 | ModItem::ExternCrate(_)
875 | ModItem::ExternBlock(_)
883 | ModItem::MacroRules(_)
884 | ModItem::MacroDef(_) => None,
885 ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
886 ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
887 ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
888 ModItem::Function(func) => Some(AssocItem::Function(*func)),
892 pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
893 N::id_from_mod_item(self)
896 pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
898 ModItem::Import(it) => tree[it.index].ast_id().upcast(),
899 ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
900 ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
901 ModItem::Function(it) => tree[it.index].ast_id().upcast(),
902 ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
903 ModItem::Union(it) => tree[it.index].ast_id().upcast(),
904 ModItem::Enum(it) => tree[it.index].ast_id().upcast(),
905 ModItem::Const(it) => tree[it.index].ast_id().upcast(),
906 ModItem::Static(it) => tree[it.index].ast_id().upcast(),
907 ModItem::Trait(it) => tree[it.index].ast_id().upcast(),
908 ModItem::Impl(it) => tree[it.index].ast_id().upcast(),
909 ModItem::TypeAlias(it) => tree[it.index].ast_id().upcast(),
910 ModItem::Mod(it) => tree[it.index].ast_id().upcast(),
911 ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(),
912 ModItem::MacroRules(it) => tree[it.index].ast_id().upcast(),
913 ModItem::MacroDef(it) => tree[it.index].ast_id().upcast(),
918 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
920 Function(FileItemTreeId<Function>),
921 TypeAlias(FileItemTreeId<TypeAlias>),
922 Const(FileItemTreeId<Const>),
923 MacroCall(FileItemTreeId<MacroCall>),
926 impl_froms!(AssocItem {
927 Function(FileItemTreeId<Function>),
928 TypeAlias(FileItemTreeId<TypeAlias>),
929 Const(FileItemTreeId<Const>),
930 MacroCall(FileItemTreeId<MacroCall>),
933 impl From<AssocItem> for ModItem {
934 fn from(item: AssocItem) -> Self {
936 AssocItem::Function(it) => it.into(),
937 AssocItem::TypeAlias(it) => it.into(),
938 AssocItem::Const(it) => it.into(),
939 AssocItem::MacroCall(it) => it.into(),
944 #[derive(Debug, Eq, PartialEq)]
950 /// A range of densely allocated ItemTree IDs.
951 pub struct IdRange<T> {
957 fn new(range: Range<Idx<T>>) -> Self {
958 Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
961 fn is_empty(&self) -> bool {
962 self.range.is_empty()
966 impl<T> Iterator for IdRange<T> {
968 fn next(&mut self) -> Option<Self::Item> {
969 self.range.next().map(|raw| Idx::from_raw(raw.into()))
973 impl<T> DoubleEndedIterator for IdRange<T> {
974 fn next_back(&mut self) -> Option<Self::Item> {
975 self.range.next_back().map(|raw| Idx::from_raw(raw.into()))
979 impl<T> fmt::Debug for IdRange<T> {
980 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
981 f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
985 impl<T> Clone for IdRange<T> {
986 fn clone(&self) -> Self {
987 Self { range: self.range.clone(), _p: PhantomData }
991 impl<T> PartialEq for IdRange<T> {
992 fn eq(&self, other: &Self) -> bool {
993 self.range == other.range
997 impl<T> Eq for IdRange<T> {}
999 #[derive(Debug, Clone, PartialEq, Eq)]
1001 Record(IdRange<Field>),
1002 Tuple(IdRange<Field>),
1006 /// A single field of an enum variant or struct
1007 #[derive(Debug, Clone, PartialEq, Eq)]
1010 pub type_ref: Interned<TypeRef>,
1011 pub visibility: RawVisibilityId,