Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 126 additions & 118 deletions crates/hir-def/src/expr_store/tests/body/block.rs

Large diffs are not rendered by default.

63 changes: 43 additions & 20 deletions crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Describes items defined or visible (ie, imported) in a certain scope.
//! This is shared between modules and blocks.

use std::sync::LazyLock;
use std::{fmt, sync::LazyLock};

use base_db::Crate;
use hir_expand::{AstId, MacroCallId, attrs::AttrId, db::ExpandDatabase, name::Name};
use hir_expand::{AstId, MacroCallId, attrs::AttrId, name::Name};
use indexmap::map::Entry;
use itertools::Itertools;
use la_arena::Idx;
Expand All @@ -19,6 +19,7 @@ use crate::{
AdtId, BuiltinType, ConstId, ExternBlockId, ExternCrateId, FxIndexMap, HasModule, ImplId,
LocalModuleId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId,
db::DefDatabase,
nameres::MacroSubNs,
per_ns::{Item, MacrosItem, PerNs, TypesItem, ValuesItem},
visibility::Visibility,
};
Expand Down Expand Up @@ -735,40 +736,47 @@ impl ItemScope {
}
}

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

let print_macro_sub_ns =
|buf: &mut String, macro_id: MacroId| match MacroSubNs::from_id(db, macro_id) {
MacroSubNs::Bang => buf.push('!'),
MacroSubNs::Attr => buf.push('#'),
};

for (name, def) in entries {
format_to!(
buf,
"{}:",
name.map_or("_".to_owned(), |name| name.display(db, Edition::LATEST).to_string())
);
let display_name: &dyn fmt::Display = match &name {
Some(name) => &name.display(db, Edition::LATEST),
None => &"_",
};
format_to!(buf, "- {display_name} :");

if let Some(Item { import, .. }) = def.types {
buf.push_str(" t");
buf.push_str(" type");
match import {
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
Some(ImportOrExternCrate::Glob(_)) => buf.push('g'),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
Some(ImportOrExternCrate::Import(_)) => buf.push_str(" (import)"),
Some(ImportOrExternCrate::Glob(_)) => buf.push_str(" (glob)"),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push_str(" (extern)"),
None => (),
}
}
if let Some(Item { import, .. }) = def.values {
buf.push_str(" v");
buf.push_str(" value");
match import {
Some(ImportOrGlob::Import(_)) => buf.push('i'),
Some(ImportOrGlob::Glob(_)) => buf.push('g'),
Some(ImportOrGlob::Import(_)) => buf.push_str(" (import)"),
Some(ImportOrGlob::Glob(_)) => buf.push_str(" (glob)"),
None => (),
}
}
if let Some(Item { import, .. }) = def.macros {
buf.push_str(" m");
if let Some(Item { def: macro_id, import, .. }) = def.macros {
buf.push_str(" macro");
print_macro_sub_ns(buf, macro_id);
match import {
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
Some(ImportOrExternCrate::Glob(_)) => buf.push('g'),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
Some(ImportOrExternCrate::Import(_)) => buf.push_str(" (import)"),
Some(ImportOrExternCrate::Glob(_)) => buf.push_str(" (glob)"),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push_str(" (extern)"),
None => (),
}
}
Expand All @@ -778,6 +786,21 @@ impl ItemScope {

buf.push('\n');
}

// Also dump legacy-textual-scope macros visible at the _end_ of the scope.
//
// For tests involving a cursor position, this might include macros that
// are _not_ visible at the cursor position.
let mut legacy_macros = self.legacy_macros().collect::<Vec<_>>();
legacy_macros.sort_by(|(a, _), (b, _)| Ord::cmp(a, b));
for (name, macros) in legacy_macros {
format_to!(buf, "- (legacy) {} :", name.display(db, Edition::LATEST));
for &macro_id in macros {
buf.push_str(" macro");
print_macro_sub_ns(buf, macro_id);
}
buf.push('\n');
}
}

pub(crate) fn shrink_to_fit(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ impl DefMap {
let mut arc;
let mut current_map = self;
while let Some(block) = current_map.block {
go(&mut buf, db, current_map, "block scope", Self::ROOT);
go(&mut buf, db, current_map, "(block scope)", Self::ROOT);
buf.push('\n');
arc = block.parent.def_map(db, self.krate);
current_map = arc;
Expand Down Expand Up @@ -814,7 +814,7 @@ pub enum MacroSubNs {
}

impl MacroSubNs {
fn from_id(db: &dyn DefDatabase, macro_id: MacroId) -> Self {
pub(crate) fn from_id(db: &dyn DefDatabase, macro_id: MacroId) -> Self {
let expander = match macro_id {
MacroId::Macro2Id(it) => it.lookup(db).expander,
MacroId::MacroRulesId(it) => it.lookup(db).expander,
Expand Down
Loading