Skip to content

Commit ec55777

Browse files
authored
Merge pull request #21094 from Zalathar/dump
Print more macro information in `DefMap` dumps
2 parents 3d827b0 + 421bc6e commit ec55777

File tree

8 files changed

+757
-668
lines changed

8 files changed

+757
-668
lines changed

src/tools/rust-analyzer/crates/hir-def/src/expr_store/tests/body/block.rs

Lines changed: 126 additions & 118 deletions
Large diffs are not rendered by default.

src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Describes items defined or visible (ie, imported) in a certain scope.
22
//! This is shared between modules and blocks.
33
4-
use std::sync::LazyLock;
4+
use std::{fmt, sync::LazyLock};
55

66
use base_db::Crate;
7-
use hir_expand::{AstId, MacroCallId, attrs::AttrId, db::ExpandDatabase, name::Name};
7+
use hir_expand::{AstId, MacroCallId, attrs::AttrId, name::Name};
88
use indexmap::map::Entry;
99
use itertools::Itertools;
1010
use la_arena::Idx;
@@ -19,6 +19,7 @@ use crate::{
1919
AdtId, BuiltinType, ConstId, ExternBlockId, ExternCrateId, FxIndexMap, HasModule, ImplId,
2020
LocalModuleId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId,
2121
db::DefDatabase,
22+
nameres::MacroSubNs,
2223
per_ns::{Item, MacrosItem, PerNs, TypesItem, ValuesItem},
2324
visibility::Visibility,
2425
};
@@ -735,40 +736,47 @@ impl ItemScope {
735736
}
736737
}
737738

738-
pub(crate) fn dump(&self, db: &dyn ExpandDatabase, buf: &mut String) {
739+
pub(crate) fn dump(&self, db: &dyn DefDatabase, buf: &mut String) {
739740
let mut entries: Vec<_> = self.resolutions().collect();
740741
entries.sort_by_key(|(name, _)| name.clone());
741742

743+
let print_macro_sub_ns =
744+
|buf: &mut String, macro_id: MacroId| match MacroSubNs::from_id(db, macro_id) {
745+
MacroSubNs::Bang => buf.push('!'),
746+
MacroSubNs::Attr => buf.push('#'),
747+
};
748+
742749
for (name, def) in entries {
743-
format_to!(
744-
buf,
745-
"{}:",
746-
name.map_or("_".to_owned(), |name| name.display(db, Edition::LATEST).to_string())
747-
);
750+
let display_name: &dyn fmt::Display = match &name {
751+
Some(name) => &name.display(db, Edition::LATEST),
752+
None => &"_",
753+
};
754+
format_to!(buf, "- {display_name} :");
748755

749756
if let Some(Item { import, .. }) = def.types {
750-
buf.push_str(" t");
757+
buf.push_str(" type");
751758
match import {
752-
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
753-
Some(ImportOrExternCrate::Glob(_)) => buf.push('g'),
754-
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
759+
Some(ImportOrExternCrate::Import(_)) => buf.push_str(" (import)"),
760+
Some(ImportOrExternCrate::Glob(_)) => buf.push_str(" (glob)"),
761+
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push_str(" (extern)"),
755762
None => (),
756763
}
757764
}
758765
if let Some(Item { import, .. }) = def.values {
759-
buf.push_str(" v");
766+
buf.push_str(" value");
760767
match import {
761-
Some(ImportOrGlob::Import(_)) => buf.push('i'),
762-
Some(ImportOrGlob::Glob(_)) => buf.push('g'),
768+
Some(ImportOrGlob::Import(_)) => buf.push_str(" (import)"),
769+
Some(ImportOrGlob::Glob(_)) => buf.push_str(" (glob)"),
763770
None => (),
764771
}
765772
}
766-
if let Some(Item { import, .. }) = def.macros {
767-
buf.push_str(" m");
773+
if let Some(Item { def: macro_id, import, .. }) = def.macros {
774+
buf.push_str(" macro");
775+
print_macro_sub_ns(buf, macro_id);
768776
match import {
769-
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
770-
Some(ImportOrExternCrate::Glob(_)) => buf.push('g'),
771-
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
777+
Some(ImportOrExternCrate::Import(_)) => buf.push_str(" (import)"),
778+
Some(ImportOrExternCrate::Glob(_)) => buf.push_str(" (glob)"),
779+
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push_str(" (extern)"),
772780
None => (),
773781
}
774782
}
@@ -778,6 +786,21 @@ impl ItemScope {
778786

779787
buf.push('\n');
780788
}
789+
790+
// Also dump legacy-textual-scope macros visible at the _end_ of the scope.
791+
//
792+
// For tests involving a cursor position, this might include macros that
793+
// are _not_ visible at the cursor position.
794+
let mut legacy_macros = self.legacy_macros().collect::<Vec<_>>();
795+
legacy_macros.sort_by(|(a, _), (b, _)| Ord::cmp(a, b));
796+
for (name, macros) in legacy_macros {
797+
format_to!(buf, "- (legacy) {} :", name.display(db, Edition::LATEST));
798+
for &macro_id in macros {
799+
buf.push_str(" macro");
800+
print_macro_sub_ns(buf, macro_id);
801+
}
802+
buf.push('\n');
803+
}
781804
}
782805

783806
pub(crate) fn shrink_to_fit(&mut self) {

src/tools/rust-analyzer/crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl DefMap {
602602
let mut arc;
603603
let mut current_map = self;
604604
while let Some(block) = current_map.block {
605-
go(&mut buf, db, current_map, "block scope", Self::ROOT);
605+
go(&mut buf, db, current_map, "(block scope)", Self::ROOT);
606606
buf.push('\n');
607607
arc = block.parent.def_map(db, self.krate);
608608
current_map = arc;
@@ -814,7 +814,7 @@ pub enum MacroSubNs {
814814
}
815815

816816
impl MacroSubNs {
817-
fn from_id(db: &dyn DefDatabase, macro_id: MacroId) -> Self {
817+
pub(crate) fn from_id(db: &dyn DefDatabase, macro_id: MacroId) -> Self {
818818
let expander = match macro_id {
819819
MacroId::Macro2Id(it) => it.lookup(db).expander,
820820
MacroId::MacroRulesId(it) => it.lookup(db).expander,

0 commit comments

Comments
 (0)