From bf8342a6efbca2899661291d1217020e25ec7263 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 22 Nov 2025 12:27:33 +1100 Subject: [PATCH 1/3] Make `DefMap` dumps nicer to read --- .../src/expr_store/tests/body/block.rs | 236 ++++++------- crates/hir-def/src/item_scope.rs | 34 +- crates/hir-def/src/nameres.rs | 2 +- crates/hir-def/src/nameres/tests.rs | 246 ++++++------- crates/hir-def/src/nameres/tests/globs.rs | 214 ++++++------ crates/hir-def/src/nameres/tests/macros.rs | 328 +++++++++--------- .../src/nameres/tests/mod_resolution.rs | 262 +++++++------- .../hir-def/src/nameres/tests/primitives.rs | 6 +- 8 files changed, 664 insertions(+), 664 deletions(-) diff --git a/crates/hir-def/src/expr_store/tests/body/block.rs b/crates/hir-def/src/expr_store/tests/body/block.rs index c7707378a5b3..e36f6550d512 100644 --- a/crates/hir-def/src/expr_store/tests/body/block.rs +++ b/crates/hir-def/src/expr_store/tests/body/block.rs @@ -12,12 +12,12 @@ fn outer() { } "#, expect![[r#" - block scope - inner: v + (block scope) + - inner : value crate - inner: t - outer: v + - inner : type + - outer : value "#]], ); } @@ -37,16 +37,16 @@ fn outer() { } "#, expect![[r#" - block scope - CrateStruct: ti - PlainStruct: ti vi - SelfStruct: ti - Struct: v - SuperStruct: _ + (block scope) + - CrateStruct : type (import) + - PlainStruct : type (import) value (import) + - SelfStruct : type (import) + - Struct : value + - SuperStruct : _ crate - Struct: t - outer: v + - Struct : type + - outer : value "#]], ); } @@ -65,13 +65,13 @@ fn outer() { } "#, expect![[r#" - block scope - imported: ti vi - name: v + (block scope) + - imported : type (import) value (import) + - name : value crate - name: t - outer: v + - name : type + - outer : value "#]], ); } @@ -91,17 +91,17 @@ fn outer() { } "#, expect![[r#" - block scope - inner1: ti - inner2: v - outer: vi + (block scope) + - inner1 : type (import) + - inner2 : value + - outer : value (import) - block scope - inner: v - inner1: t + (block scope) + - inner : value + - inner1 : type crate - outer: v + - outer : value "#]], ); } @@ -120,15 +120,15 @@ mod module { struct Struct {} "#, expect![[r#" - block scope - Struct: ti + (block scope) + - Struct : type (import) crate - Struct: t - module: t + - Struct : type + - module : type crate::module - f: v + - f : value "#]], ); } @@ -152,24 +152,24 @@ fn outer() { } "#, expect![[r#" - block scope - ResolveMe: ti + (block scope) + - ResolveMe : type (import) - block scope - m2: t + (block scope) + - m2 : type - block scope::m2 - inner: v + (block scope)::m2 + - inner : value - block scope - m: t + (block scope) + - m : type - block scope::m - ResolveMe: t - middle: v + (block scope)::m + - ResolveMe : type + - middle : value crate - outer: v + - outer : value "#]], ); } @@ -213,21 +213,21 @@ fn f() { } "#, expect![[r#" - block scope - ResolveMe: ti + (block scope) + - ResolveMe : type (import) - block scope - h: v + (block scope) + - h : value - block scope - m: t + (block scope) + - m : type - block scope::m - ResolveMe: t - g: v + (block scope)::m + - ResolveMe : type + - g : value crate - f: v + - f : value "#]], ); } @@ -250,11 +250,11 @@ fn f() { } "#, expect![[r#" - block scope - Hit: t + (block scope) + - Hit : type crate - f: v + - f : value "#]], ); } @@ -285,15 +285,15 @@ pub mod cov_mark { } "#, expect![[r#" - block scope - Hit: t + (block scope) + - Hit : type - block scope - nested: v + (block scope) + - nested : value crate - cov_mark: ti - f: v + - cov_mark : type (import) + - f : value "#]], ); } @@ -318,16 +318,16 @@ fn main() { } "#, expect![[r#" - block scope - module: t + (block scope) + - module : type - block scope::module - BarWorks: t v - FooWorks: t v + (block scope)::module + - BarWorks : type value + - FooWorks : type value crate - foo: m - main: v + - foo : macro + - main : value "#]], ); } @@ -354,14 +354,14 @@ fn f() { } "#, expect![[r#" - block scope - Def: t + (block scope) + - Def : type crate - module: t + - module : type crate::module - f: v + - f : value "#]], ) } @@ -380,16 +380,16 @@ fn main() { } "#, expect![[r#" - block scope - Struct: t - module: t + (block scope) + - Struct : type + - module : type - block scope::module - Struct: _ + (block scope)::module + - Struct : _ - crate - main: v - "#]], + crate + - main : value + "#]], ); } @@ -408,16 +408,16 @@ mod m { } "#, expect![[r#" - block scope - _: t - Tr: t + (block scope) + - _ : type + - Tr : type - crate - m: t + crate + - m : type - crate::m - main: v - "#]], + crate::m + - main : value + "#]], ); } @@ -444,11 +444,11 @@ fn foo() { } "#, expect![[r#" - block scope - bar: v + (block scope) + - bar : value crate - foo: v + - foo : value "#]], ) } @@ -467,16 +467,16 @@ fn outer() { } "#, expect![[r#" - block scope - name: _ - tests: t + (block scope) + - name : _ + - tests : type - block scope::tests - name: _ - outer: vg + (block scope)::tests + - name : _ + - outer : value (glob) crate - outer: v + - outer : value "#]], ); } @@ -496,11 +496,11 @@ fn foo() { } "#, expect![[r#" - block scope - inner: v + (block scope) + - inner : value crate - foo: v + - foo : value "#]], ) } @@ -517,12 +517,12 @@ fn f() {$0 }; "#, expect![[r#" - block scope - BAR: v - FOO: v + (block scope) + - BAR : value + - FOO : value crate - f: v + - f : value "#]], ) } @@ -543,14 +543,14 @@ fn main() { pub struct S; "#, expect![[r#" - block scope - f: t + (block scope) + - f : type - block scope::f - S: ti vi + (block scope)::f + - S : type (import) value (import) crate - main: v + - main : value "#]], ) } @@ -573,18 +573,18 @@ fn main() { pub const S; "#, expect![[r#" - block scope - S: ti vi - inner: v + (block scope) + - S : type (import) value (import) + - inner : value - block scope - core: t + (block scope) + - core : type - block scope::core - S: t v + (block scope)::core + - S : type value crate - main: v + - main : value "#]], ) } diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs index 77ed664f4443..01adef5bccdb 100644 --- a/crates/hir-def/src/item_scope.rs +++ b/crates/hir-def/src/item_scope.rs @@ -1,7 +1,7 @@ //! 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}; @@ -740,35 +740,35 @@ impl ItemScope { entries.sort_by_key(|(name, _)| name.clone()); 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"); + buf.push_str(" macro"); 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 => (), } } diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs index 7d5e627964eb..50958efa455f 100644 --- a/crates/hir-def/src/nameres.rs +++ b/crates/hir-def/src/nameres.rs @@ -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; diff --git a/crates/hir-def/src/nameres/tests.rs b/crates/hir-def/src/nameres/tests.rs index 4a7974c4fa15..68f47f50bfcf 100644 --- a/crates/hir-def/src/nameres/tests.rs +++ b/crates/hir-def/src/nameres/tests.rs @@ -61,22 +61,22 @@ extern { "#, expect![[r#" crate - E: _ - S: t v - V: _ - foo: t + - E : _ + - S : type value + - V : _ + - foo : type crate::foo - bar: t - f: v + - bar : type + - f : value crate::foo::bar - Baz: t v - E: t - EXT: v - Ext: t - U: t - ext: v + - Baz : type value + - E : type + - EXT : value + - Ext : type + - U : type + - ext : value "#]], ); } @@ -97,19 +97,19 @@ mod a { "#, expect![[r#" crate - a: t + - a : type crate::a - A: v - b: t + - A : value + - b : type crate::a::b - B: v - c: t + - B : value + - c : type crate::a::b::c - A: vg - b: tg + - A : value (glob) + - b : type (glob) "#]], ); } @@ -125,10 +125,10 @@ mod m { "#, expect![[r#" crate - m: t + - m : type crate::m - z: t v + - z : type value crate::m::z "#]], @@ -151,8 +151,8 @@ use crate; "#, expect![[r#" crate - S: t v - foo: t + - S : type value + - foo : type crate::foo "#]], @@ -172,11 +172,11 @@ pub struct Baz; "#, expect![[r#" crate - Foo: ti vi - foo: t + - Foo : type (import) value (import) + - foo : type crate::foo - Baz: t v + - Baz : type value "#]], ); } @@ -198,16 +198,16 @@ pub enum Quux {}; "#, expect![[r#" crate - Baz: ti vi - Quux: ti - foo: t + - Baz : type (import) value (import) + - Quux : type (import) + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v - Quux: t + - Baz : type value + - Quux : type "#]], ); } @@ -229,15 +229,15 @@ pub struct Baz; "#, expect![[r#" crate - Baz: ti vi - foo: t + - Baz : type (import) value (import) + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -261,8 +261,8 @@ pub enum Foo { Bar, Baz } "#, expect![[r#" crate - Bar: tg vg - Baz: tg vg + - Bar : type (glob) value (glob) + - Baz : type (glob) value (glob) "#]], ); } @@ -277,8 +277,8 @@ use self::E::V; "#, expect![[r#" crate - E: t - V: ti vi + - E : type + - V : type (import) value (import) "#]], ); } @@ -303,15 +303,15 @@ pub struct FromLib; "#, expect![[r#" crate - bar: t - foo: t + - bar : type + - foo : type crate::bar - Bar: t v + - Bar : type value crate::foo - Bar: _ - FromLib: ti vi + - Bar : _ + - FromLib : type (import) value (import) "#]], ); } @@ -332,14 +332,14 @@ pub struct Baz; "#, expect![[r#" crate - Baz: ti - foo: t + - Baz : type (import) + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -356,7 +356,7 @@ pub struct Baz; "#, expect![[r#" crate - Baz: ti vi + - Baz : type (import) value (import) "#]], ); } @@ -378,14 +378,14 @@ pub struct Arc; "#, expect![[r#" crate - alloc: t - alloc_crate: te - sync: t + - alloc : type + - alloc_crate : type (extern) + - sync : type crate::alloc crate::sync - Arc: ti vi + - Arc : type (import) value (import) "#]], ); } @@ -426,12 +426,12 @@ pub struct NotExported; "#, expect![[r#" crate - Exported: tg vg - PublicItem: tg vg - allowed_reexport: tg - exported: tg - not_allowed_reexport1: _ - not_allowed_reexport2: _ + - Exported : type (glob) value (glob) + - PublicItem : type (glob) value (glob) + - allowed_reexport : type (glob) + - exported : type (glob) + - not_allowed_reexport1 : _ + - not_allowed_reexport2 : _ "#]], ); } @@ -453,14 +453,14 @@ pub struct Arc; "#, expect![[r#" crate - alloc: t - alloc_crate: te - sync: t + - alloc : type + - alloc_crate : type (extern) + - sync : type crate::alloc crate::sync - Arc: ti vi + - Arc : type (import) value (import) "#]], ); } @@ -475,7 +475,7 @@ extern crate self as bla; "#, expect![[r#" crate - bla: te + - bla : type (extern) "#]], ); } @@ -496,7 +496,7 @@ pub struct Baz; "#, expect![[r#" crate - Baz: ti vi + - Baz : type (import) value (import) "#]], ); } @@ -514,8 +514,8 @@ pub struct Bar; "#, expect![[r#" crate - Bar: ti vi - foo: v + - Bar : type (import) value (import) + - foo : value "#]], ); } @@ -542,7 +542,7 @@ fn no_std_prelude() { "#, expect![[r#" crate - Rust: ti vi + - Rust : type (import) value (import) "#]], ); } @@ -566,7 +566,7 @@ fn edition_specific_preludes() { "#, expect![[r#" crate - Rust2018: ti vi + - Rust2018 : type (import) value (import) "#]], ); check( @@ -583,7 +583,7 @@ fn edition_specific_preludes() { "#, expect![[r#" crate - Rust2021: ti vi + - Rust2021 : type (import) value (import) "#]], ); } @@ -612,8 +612,8 @@ pub mod prelude { "#, expect![[r#" crate - Bar: ti vi - Foo: ti vi + - Bar : type (import) value (import) + - Foo : type (import) value (import) "#]], ); } @@ -639,9 +639,9 @@ pub mod prelude { "#, expect![[r#" crate - Bar: ti vi - Baz: _ - Foo: _ + - Bar : type (import) value (import) + - Baz : _ + - Foo : _ "#]], ); } @@ -667,9 +667,9 @@ pub mod prelude { "#, expect![[r#" crate - Bar: _ - Baz: ti vi - Foo: ti vi + - Bar : _ + - Baz : type (import) value (import) + - Foo : type (import) value (import) "#]], ); } @@ -692,15 +692,15 @@ mod b { "#, expect![[r#" crate - T: ti vi - a: t - b: t + - T : type (import) value (import) + - a : type + - b : type crate::a - T: t vg + - T : type value (glob) crate::b - T: v + - T : value "#]], ); } @@ -720,13 +720,13 @@ mod tr { "#, expect![[r#" crate - _: t - _: t - tr: t + - _ : type + - _ : type + - tr : type crate::tr - Tr: t - Tr2: t + - Tr : type + - Tr2 : type "#]], ); } @@ -748,17 +748,17 @@ use crate::reex::*; "#, expect![[r#" crate - _: t - reex: t - tr: t + - _ : type + - reex : type + - tr : type crate::reex - _: t - _: t + - _ : type + - _ : type crate::tr - PrivTr: t - PubTr: t + - PrivTr : type + - PubTr : type "#]], ); } @@ -781,7 +781,7 @@ mod tr { "#, expect![[r#" crate - _: t + - _ : type "#]], ); } @@ -800,12 +800,12 @@ use crate::m::{Struct as _, Enum as _, CONST as _}; "#, expect![[r#" crate - m: t + - m : type crate::m - CONST: v - Enum: t - Struct: t v + - CONST : value + - Enum : type + - Struct : type value "#]], ); } @@ -825,12 +825,12 @@ mod tr { "#, expect![[r#" crate - _: t - Tr: t v - tr: t + - _ : type + - Tr : type value + - tr : type crate::tr - Tr: t + - Tr : type "#]], ); } @@ -864,9 +864,9 @@ fn bar() {} "#, expect![[r#" crate - bar: v - baz: vi - foo: ti + - bar : value + - baz : value (import) + - foo : type (import) "#]], ); } @@ -885,11 +885,11 @@ use self::m::S::{self}; "#, expect![[r#" crate - S: ti - m: t + - S : type (import) + - m : type crate::m - S: t v m + - S : type value macro "#]], ); } @@ -909,8 +909,8 @@ pub const settings: () = (); "#, expect![[r#" crate - Settings: ti vi - settings: vi + - Settings : type (import) value (import) + - settings : value (import) "#]], ) } @@ -926,7 +926,7 @@ pub struct Struct; "#, expect![[r#" crate - Struct: _ + - Struct : _ "#]], ); check( @@ -939,8 +939,8 @@ pub struct Struct; "#, expect![[r#" crate - Struct: ti vi - dep: te + - Struct : type (import) value (import) + - dep : type (extern) "#]], ); } @@ -964,18 +964,18 @@ use some_module::unknown_func; "#, expect![[r#" crate - other_module: t - some_module: t - unknown_func: vi + - other_module : type + - some_module : type + - unknown_func : value (import) crate::other_module - some_submodule: t + - some_submodule : type crate::other_module::some_submodule - unknown_func: vi + - unknown_func : value (import) crate::some_module - unknown_func: v + - unknown_func : value "#]], ) } diff --git a/crates/hir-def/src/nameres/tests/globs.rs b/crates/hir-def/src/nameres/tests/globs.rs index ddb9d4a134d3..779f7769bbe9 100644 --- a/crates/hir-def/src/nameres/tests/globs.rs +++ b/crates/hir-def/src/nameres/tests/globs.rs @@ -18,18 +18,18 @@ pub struct Baz; "#, expect![[r#" crate - Baz: tg vg - Foo: tg vg - bar: tg - foo: t + - Baz : type (glob) value (glob) + - Foo : type (glob) value (glob) + - bar : type (glob) + - foo : type crate::foo - Baz: ti vi - Foo: t v - bar: t + - Baz : type (import) value (import) + - Foo : type value + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -53,20 +53,20 @@ pub use super::*; "#, expect![[r#" crate - Baz: tg vg - Foo: tg vg - bar: tg - foo: t + - Baz : type (glob) value (glob) + - Foo : type (glob) value (glob) + - bar : type (glob) + - foo : type crate::foo - Baz: tg vg - Foo: t v - bar: t + - Baz : type (glob) value (glob) + - Foo : type value + - bar : type crate::foo::bar - Baz: t v - Foo: tg vg - bar: tg + - Baz : type value + - Foo : type (glob) value (glob) + - bar : type (glob) "#]], ); } @@ -91,20 +91,20 @@ pub use super::*; ", expect![[r#" crate - Baz: tg vg - bar: tg - foo: t + - Baz : type (glob) value (glob) + - bar : type (glob) + - foo : type crate::foo - Baz: tg vg - PrivateStructFoo: t v - bar: t + - Baz : type (glob) value (glob) + - PrivateStructFoo : type value + - bar : type crate::foo::bar - Baz: t v - PrivateStructBar: t v - PrivateStructFoo: tg vg - bar: tg + - Baz : type value + - PrivateStructBar : type value + - PrivateStructFoo : type (glob) value (glob) + - bar : type (glob) "#]], ); } @@ -130,19 +130,19 @@ pub(crate) struct PubCrateStruct; ", expect![[r#" crate - Foo: tg - PubCrateStruct: tg vg - bar: tg - foo: t + - Foo : type (glob) + - PubCrateStruct : type (glob) value (glob) + - bar : type (glob) + - foo : type crate::foo - Foo: t v - bar: t + - Foo : type value + - bar : type crate::foo::bar - PrivateBar: t v - PrivateBaz: t v - PubCrateStruct: t v + - PrivateBar : type value + - PrivateBaz : type value + - PubCrateStruct : type value "#]], ); } @@ -160,7 +160,7 @@ pub struct Baz; "#, expect![[r#" crate - Baz: tg vg + - Baz : type (glob) value (glob) "#]], ); } @@ -178,7 +178,7 @@ struct Foo; "#, expect![[r#" crate - Baz: tg vg + - Baz : type (glob) value (glob) "#]], ); } @@ -193,9 +193,9 @@ use self::Foo::*; "#, expect![[r#" crate - Bar: tg vg - Baz: tg vg - Foo: t + - Bar : type (glob) value (glob) + - Baz : type (glob) value (glob) + - Foo : type "#]], ); } @@ -210,9 +210,9 @@ use self::Foo::{*}; "#, expect![[r#" crate - Bar: tg vg - Baz: tg vg - Foo: t + - Bar : type (glob) value (glob) + - Baz : type (glob) value (glob) + - Foo : type "#]], ); } @@ -237,22 +237,22 @@ pub mod baz { pub struct Bar; } "#, expect![[r#" crate - Bar: ti vi - bar: t - baz: ti - foo: t + - Bar : type (import) value (import) + - bar : type + - baz : type (import) + - foo : type crate::bar - baz: t + - baz : type crate::bar::baz - Bar: t v + - Bar : type value crate::foo - baz: t + - baz : type crate::foo::baz - Foo: t v + - Foo : type value "#]], ); } @@ -276,22 +276,22 @@ pub mod baz { pub struct Bar; } "#, expect![[r#" crate - Bar: ti vi - bar: t - baz: ti - foo: t + - Bar : type (import) value (import) + - bar : type + - baz : type (import) + - foo : type crate::bar - baz: t + - baz : type crate::bar::baz - Bar: t v + - Bar : type value crate::foo - baz: t + - baz : type crate::foo::baz - Foo: t v + - Foo : type value "#]], ); } @@ -311,29 +311,29 @@ mod d { "#, expect![[r#" crate - a: t - b: t - c: t - d: t + - a : type + - b : type + - c : type + - d : type crate::a - foo: t + - foo : type crate::a::foo - X: t v + - X : type value crate::b - foo: ti + - foo : type (import) crate::c - foo: t + - foo : type crate::c::foo - Y: t v + - Y : type value crate::d - Y: ti vi - foo: ti + - Y : type (import) value (import) + - foo : type (import) "#]], ); } @@ -355,15 +355,15 @@ use event::Event; "#, expect![[r#" crate - Event: ti - event: t + - Event : type (import) + - event : type crate::event - Event: t vg - serenity: t + - Event : type value (glob) + - serenity : type crate::event::serenity - Event: v + - Event : value "#]], ); } @@ -388,27 +388,27 @@ use reexport::*; "#, expect![[r#" crate - Trait: tg - defs: t - function: vg - makro: mg - reexport: t + - Trait : type (glob) + - defs : type + - function : value (glob) + - makro : macro (glob) + - reexport : type crate::defs - Trait: t - function: v - makro: m + - Trait : type + - function : value + - makro : macro crate::reexport - Trait: tg - function: vg - inner: t - makro: mg + - Trait : type (glob) + - function : value (glob) + - inner : type + - makro : macro (glob) crate::reexport::inner - Trait: ti - function: vi - makro: mi + - Trait : type (import) + - function : value (import) + - makro : macro (import) "#]], ); } @@ -435,19 +435,19 @@ mod glob_target { "#, expect![[r#" crate - glob_target: t - outer: t + - glob_target : type + - outer : type crate::glob_target - ShouldBePrivate: t v + - ShouldBePrivate : type value crate::outer - ShouldBePrivate: tg vg - inner_superglob: t + - ShouldBePrivate : type (glob) value (glob) + - inner_superglob : type crate::outer::inner_superglob - ShouldBePrivate: tg vg - inner_superglob: tg + - ShouldBePrivate : type (glob) value (glob) + - inner_superglob : type (glob) "#]], ); } @@ -473,20 +473,20 @@ use reexport_2::*; "#, expect![[r#" crate - Placeholder: tg vg - libs: t - reexport_1: tg - reexport_2: t + - Placeholder : type (glob) value (glob) + - libs : type + - reexport_1 : type (glob) + - reexport_2 : type crate::libs - Placeholder: t v + - Placeholder : type value crate::reexport_2 - Placeholder: tg vg - reexport_1: t + - Placeholder : type (glob) value (glob) + - reexport_1 : type crate::reexport_2::reexport_1 - Placeholder: tg vg + - Placeholder : type (glob) value (glob) "#]], ); } diff --git a/crates/hir-def/src/nameres/tests/macros.rs b/crates/hir-def/src/nameres/tests/macros.rs index 3cba88ec2f17..5b60031ae371 100644 --- a/crates/hir-def/src/nameres/tests/macros.rs +++ b/crates/hir-def/src/nameres/tests/macros.rs @@ -23,12 +23,12 @@ structs!(Bar, Baz); "#, expect![[r#" crate - Foo: t - nested: t + - Foo : type + - nested : type crate::nested - Bar: t - Baz: t + - Bar : type + - Baz : type "#]], ); } @@ -53,20 +53,20 @@ struct Y; "#, expect![[r#" crate - m: t - n1: t + - m : type + - n1 : type crate::m - n3: t + - n3 : type crate::m::n3 - Y: t v + - Y : type value crate::n1 - n2: t + - n2 : type crate::n1::n2 - X: t v + - X : type value "#]], ); } @@ -92,14 +92,14 @@ macro_rules! structs { "#, expect![[r#" crate - Bar: t - Foo: t - bar: t + - Bar : type + - Foo : type + - bar : type crate::bar - Bar: tg - Foo: tg - bar: tg + - Bar : type (glob) + - Foo : type (glob) + - bar : type (glob) "#]], ); } @@ -125,14 +125,14 @@ macro_rules! structs { "#, expect![[r#" crate - Bar: t - Foo: t - bar: t + - Bar : type + - Foo : type + - bar : type crate::bar - Bar: tg - Foo: tg - bar: tg + - Bar : type (glob) + - Foo : type (glob) + - bar : type (glob) "#]], ); } @@ -164,14 +164,14 @@ macro_rules! inner { "#, expect![[r#" crate - Bar: t - Foo: t - bar: t + - Bar : type + - Foo : type + - bar : type crate::bar - Bar: tg - Foo: tg - bar: tg + - Bar : type (glob) + - Foo : type (glob) + - bar : type (glob) "#]], ); } @@ -206,9 +206,9 @@ macro_rules! bar { "#, expect![[r#" crate - Foo: t - bar: mi - foo: mi + - Foo : type + - bar : macro (import) + - foo : macro (import) "#]], ); } @@ -252,13 +252,13 @@ mod priv_mod { "#, expect![[r#" crate - Bar: t v - Foo: t v - bar: t - foo: te + - Bar : type value + - Foo : type value + - bar : type + - foo : type (extern) crate::bar - Baz: t v + - Baz : type value "#]], ); } @@ -316,15 +316,15 @@ macro_rules! baz3 { () => { struct OkBaz3; } } "#, expect![[r#" crate - OkBar1: t v - OkBar2: t v - OkBar3: t v - OkBaz1: t v - OkBaz2: t v - OkBaz3: t v - all: te - empty: te - multiple: te + - OkBar1 : type value + - OkBar2 : type value + - OkBar3 : type value + - OkBaz1 : type value + - OkBaz2 : type value + - OkBaz3 : type value + - all : type (extern) + - empty : type (extern) + - multiple : type (extern) "#]], ); } @@ -370,13 +370,13 @@ macro_rules! structs_outside { "#, expect![[r#" crate - Bar: t v - Foo: t v - Out: t v - bar: t + - Bar : type value + - Foo : type value + - Out : type value + - bar : type crate::bar - Baz: t v + - Baz : type value "#]], ); } @@ -398,7 +398,7 @@ mod prelude { "#, expect![[r#" crate - prelude: t + - prelude : type crate::prelude "#]], @@ -419,7 +419,7 @@ macro_rules! m { "#, expect![[r#" crate - S: t v + - S : type value "#]], ); // FIXME: should not expand. legacy macro scoping is not implemented. @@ -499,36 +499,36 @@ macro_rules! baz { "#, expect![[r#" crate - NotFoundBefore: t v - Ok: t v - OkAfter: t v - OkShadowStop: t v - m1: t - m2: t - m3: t - m5: t - m7: t - ok_double_macro_use_shadow: v + - NotFoundBefore : type value + - Ok : type value + - OkAfter : type value + - OkShadowStop : type value + - m1 : type + - m2 : type + - m3 : type + - m5 : type + - m7 : type + - ok_double_macro_use_shadow : value crate::m1 crate::m2 crate::m3 - OkAfterInside: t v - OkMacroUse: t v - OkMacroUseInner: t v - m4: t - m5: t - ok_shadow: v + - OkAfterInside : type value + - OkMacroUse : type value + - OkMacroUseInner : type value + - m4 : type + - m5 : type + - ok_shadow : value crate::m3::m4 - ok_shadow_deep: v + - ok_shadow_deep : value crate::m3::m5 crate::m5 - m6: t + - m6 : type crate::m5::m6 @@ -555,9 +555,9 @@ fn baz() {} "#, expect![[r#" crate - bar: ti mi - baz: ti v mi - foo: t m + - bar : type (import) macro (import) + - baz : type (import) value macro (import) + - foo : type macro "#]], ); } @@ -585,9 +585,9 @@ mod m { "#, expect![[r#" crate - Alias: t v - Direct: t v - foo: te + - Alias : type value + - Direct : type value + - foo : type (extern) "#]], ); } @@ -623,19 +623,19 @@ mod m { "#, expect![[r#" crate - OkAliasCrate: t v - OkAliasPlain: t v - OkAliasSuper: t v - OkCrate: t v - OkPlain: t v - bar: m - m: t + - OkAliasCrate : type value + - OkAliasPlain : type value + - OkAliasSuper : type value + - OkCrate : type value + - OkPlain : type value + - bar : macro + - m : type crate::m - alias1: mi - alias2: mi - alias3: mi - not_found: _ + - alias1 : macro (import) + - alias2 : macro (import) + - alias3 : macro (import) + - not_found : _ "#]], ); } @@ -686,12 +686,12 @@ pub struct Baz; "#, expect![[r#" crate - Bar: ti vi - Baz: ti vi - Foo: t v - FooSelf: ti vi - foo: te - m: t + - Bar : type (import) value (import) + - Baz : type (import) value (import) + - Foo : type value + - FooSelf : type (import) value (import) + - foo : type (extern) + - m : type crate::m "#]], @@ -729,7 +729,7 @@ pub struct bar; "#, expect![[r#" crate - bar: ti vi + - bar : type (import) value (import) "#]], ); } @@ -794,7 +794,7 @@ pub trait Clone {} "#, expect![[r#" crate - Clone: tg mg + - Clone : type (glob) macro (glob) "#]], ); } @@ -842,11 +842,11 @@ fn unresolved_attributes_fall_back_track_per_file_moditems() { "#, expect![[r#" crate - Foo: t v - submod: t + - Foo : type value + - submod : type crate::submod - Bar: t v + - Bar : type value "#]], ); } @@ -863,9 +863,9 @@ extern "C" { } "#, expect![[r#" - crate - f: v - "#]], + crate + - f : value + "#]], ); } @@ -883,7 +883,7 @@ extern { "#, expect![[r#" crate - S: v + - S : value "#]], ); } @@ -909,8 +909,8 @@ fn derive() {} "#, expect![[r#" crate - S: t v - derive: m + - S : type value + - derive : macro "#]], ); } @@ -932,7 +932,7 @@ enum E { "#, expect![[r#" crate - E: t + - E : type "#]], ); } @@ -947,7 +947,7 @@ struct S; "#, expect![[r#" crate - S: t v + - S : type value "#]], ); } @@ -995,7 +995,7 @@ indirect_macro!(); "#, expect![[r#" crate - S: t + - S : type "#]], ); } @@ -1029,13 +1029,13 @@ pub fn derive_macro_2(_item: TokenStream) -> TokenStream { "#, expect![[r#" crate - AnotherTrait: m - DummyTrait: m - TokenStream: t v - attribute_macro: v m - derive_macro: v - derive_macro_2: v - function_like_macro: v m + - AnotherTrait : macro + - DummyTrait : macro + - TokenStream : type value + - attribute_macro : value macro + - derive_macro : value + - derive_macro_2 : value + - function_like_macro : value macro "#]], ); } @@ -1075,9 +1075,9 @@ macro_rules! mbe { "#, expect![[r#" crate - DummyTrait: mg - attribute_macro: mg - function_like_macro: mg + - DummyTrait : macro (glob) + - attribute_macro : macro (glob) + - function_like_macro : macro (glob) "#]], ); } @@ -1119,8 +1119,8 @@ structs!(Foo); "#, expect![[r#" crate - Foo: t - structs: m + - Foo : type + - structs : macro "#]], ); } @@ -1143,7 +1143,7 @@ pub mod prelude { "#, expect![[r#" crate - S: t v + - S : type value "#]], ) } @@ -1193,12 +1193,12 @@ struct A; struct B; "#, expect![[r#" - crate - A: t v - B: t v - inner_a: m - inner_b: m - "#]], + crate + - A : type value + - B : type value + - inner_a : macro + - inner_b : macro + "#]], ); } @@ -1227,8 +1227,8 @@ struct A; "#, expect![[r#" crate - A: t v - inner: m + - A : type value + - inner : macro "#]], ); // eager -> MBE -> $crate::mbe @@ -1256,8 +1256,8 @@ struct A; "#, expect![[r#" crate - A: t v - inner: m + - A : type value + - inner : macro "#]], ); } @@ -1292,20 +1292,20 @@ pub mod ip_address { "#, expect![[r#" crate - company_name: t + - company_name : type crate::company_name - network: t + - network : type crate::company_name::network - v1: t + - v1 : type crate::company_name::network::v1 - IpAddress: t - ip_address: t + - IpAddress : type + - ip_address : type crate::company_name::network::v1::ip_address - IpType: t + - IpType : type "#]], ); } @@ -1338,20 +1338,20 @@ pub mod ip_address { "#, expect![[r#" crate - company_name: t + - company_name : type crate::company_name - network: t + - network : type crate::company_name::network - v1: t + - v1 : type crate::company_name::network::v1 - IpAddress: t - ip_address: t + - IpAddress : type + - ip_address : type crate::company_name::network::v1::ip_address - IpType: t + - IpType : type "#]], ); } @@ -1392,30 +1392,30 @@ pub struct Url {} "#, expect![[r#" crate - nested: t + - nested : type crate::nested - company_name: t - different_company: t - util: t + - company_name : type + - different_company : type + - util : type crate::nested::company_name - network: t + - network : type crate::nested::company_name::network - v1: t + - v1 : type crate::nested::company_name::network::v1 - IpAddress: t + - IpAddress : type crate::nested::different_company - network: t + - network : type crate::nested::different_company::network - Url: t + - Url : type crate::nested::util - Helper: t + - Helper : type "#]], ); } @@ -1500,11 +1500,11 @@ pub mod prelude { "#, expect![[r#" crate - Ok: t v - bar: m - dep: te - foo: m - ok: v + - Ok : type value + - bar : macro + - dep : type (extern) + - foo : macro + - ok : value "#]], ); } @@ -1533,11 +1533,11 @@ macro_rules! mk_foo { "#, expect![[r#" crate - a: t - lib: te + - a : type + - lib : type (extern) crate::a - Ok: t v + - Ok : type value "#]], ); } @@ -1588,10 +1588,10 @@ pub mod prelude { "#, expect![[r#" crate - Ok: t v - bar: mi - foo: mi - ok: v + - Ok : type value + - bar : macro (import) + - foo : macro (import) + - ok : value "#]], ); } diff --git a/crates/hir-def/src/nameres/tests/mod_resolution.rs b/crates/hir-def/src/nameres/tests/mod_resolution.rs index 9c97e42f4fd3..63e746d0b412 100644 --- a/crates/hir-def/src/nameres/tests/mod_resolution.rs +++ b/crates/hir-def/src/nameres/tests/mod_resolution.rs @@ -18,8 +18,8 @@ pub struct Baz; ", expect![[r#" crate - Baz: _ - foo: t + - Baz : _ + - foo : type crate::foo "#]], @@ -41,13 +41,13 @@ struct X; "#, expect![[r#" crate - n1: t + - n1 : type crate::n1 - n2: t + - n2 : type crate::n1::n2 - X: t v + - X : type value "#]], ); } @@ -76,22 +76,22 @@ pub trait Iterator; "#, expect![[r#" crate - iter: t - prelude: t + - iter : type + - prelude : type crate::iter - Iterator: ti - traits: t + - Iterator : type (import) + - traits : type crate::iter::traits - Iterator: ti - iterator: t + - Iterator : type (import) + - iterator : type crate::iter::traits::iterator - Iterator: t + - Iterator : type crate::prelude - Iterator: ti + - Iterator : type (import) "#]], ); } @@ -109,11 +109,11 @@ pub struct Bar; "#, expect![[r#" crate - Bar: ti vi - foo: t + - Bar : type (import) value (import) + - foo : type crate::foo - Bar: t v + - Bar : type value "#]], ); } @@ -139,19 +139,19 @@ pub struct Baz; "#, expect![[r#" crate - Bar: ti vi - r#async: t + - Bar : type (import) value (import) + - r#async : type crate::r#async - Bar: t v - r#async: t - foo: t + - Bar : type value + - r#async : type + - foo : type crate::r#async::r#async - Baz: t v + - Baz : type value crate::r#async::foo - Foo: t v + - Foo : type value "#]], ); } @@ -176,19 +176,19 @@ pub struct Bar; "#, expect![[r#" crate - Bar: ti vi - Foo: ti vi - r#async: t + - Bar : type (import) value (import) + - Foo : type (import) value (import) + - r#async : type crate::r#async - a: t - r#async: t + - a : type + - r#async : type crate::r#async::a - Foo: t v + - Foo : type value crate::r#async::r#async - Bar: t v + - Bar : type value "#]], ); } @@ -207,11 +207,11 @@ pub struct Bar; "#, expect![[r#" crate - Bar: ti vi - foo: t + - Bar : type (import) value (import) + - foo : type crate::foo - Bar: t v + - Bar : type value "#]], ); } @@ -233,14 +233,14 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -262,14 +262,14 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -288,11 +288,11 @@ use super::Baz; "#, expect![[r#" crate - Baz: t v - foo: t + - Baz : type value + - foo : type crate::foo - Baz: ti vi + - Baz : type (import) value (import) "#]], ); } @@ -310,10 +310,10 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: t v + - Baz : type value "#]], ); } @@ -334,13 +334,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - foo_bar: t + - foo_bar : type crate::foo::foo_bar - Baz: t v + - Baz : type value "#]], ); } @@ -361,13 +361,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - foo_bar: t + - foo_bar : type crate::foo::foo_bar - Baz: t v + - Baz : type value "#]], ); } @@ -388,14 +388,14 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v -"#]], + - Baz : type value + "#]], ); } @@ -412,10 +412,10 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: t v + - Baz : type value "#]], ); } @@ -433,10 +433,10 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: t v + - Baz : type value "#]], ); } @@ -454,13 +454,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -477,13 +477,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -501,13 +501,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -528,13 +528,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -555,13 +555,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -579,7 +579,7 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo "#]], @@ -599,13 +599,13 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -626,14 +626,14 @@ pub struct Baz; "#, expect![[r#" crate - Baz: ti vi - foo: t + - Baz : type (import) value (import) + - foo : type crate::foo - bar: t + - bar : type crate::foo::bar - Baz: t v + - Baz : type value "#]], ); } @@ -657,17 +657,17 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - baz: t + - baz : type crate::foo::bar::baz - Baz: t v + - Baz : type value "#]], ); } @@ -691,17 +691,17 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - baz: t + - baz : type crate::foo::bar::baz - Baz: t v + - Baz : type value "#]], ); } @@ -725,17 +725,17 @@ pub struct Baz; "#, expect![[r#" crate - foo: t + - foo : type crate::foo - Baz: ti vi - bar: t + - Baz : type (import) value (import) + - bar : type crate::foo::bar - baz: t + - baz : type crate::foo::bar::baz - Baz: t v + - Baz : type value "#]], ); } @@ -756,13 +756,13 @@ pub struct Baz; "#, expect![[r#" crate - module: t + - module : type crate::module - submod: t + - submod : type crate::module::submod - Baz: t v + - Baz : type value "#]], ); } @@ -783,16 +783,16 @@ struct X; "#, expect![[r#" crate - a: t + - a : type crate::a - b: t + - b : type crate::a::b - c: t + - c : type crate::a::b::c - X: t v + - X : type value "#]], ); } @@ -814,16 +814,16 @@ struct X; "#, expect![[r#" crate - a: t + - a : type crate::a - b: t + - b : type crate::a::b - c: t + - c : type crate::a::b::c - X: t v + - X : type value "#]], ); } @@ -870,8 +870,8 @@ pub mod hash { pub trait Hash {} } "#, expect![[r#" crate - Hash: ti - core: t + - Hash : type (import) + - core : type crate::core "#]], @@ -921,16 +921,16 @@ pub enum Enum { "#, expect![[r#" crate - NoAssoc: _ - const_based: _ - module: t - new: _ - unresolved: _ + - NoAssoc : _ + - const_based : _ + - module : type + - new : _ + - unresolved : _ crate::module - C: v - Enum: t - S: t v + - C : value + - Enum : type + - S : type value "#]], ); } @@ -957,14 +957,14 @@ pub trait Trait { "#, expect![[r#" crate - ASSOC_CONST: _ - AssocType: _ - MACRO_CONST: _ - method: _ - module: t + - ASSOC_CONST : _ + - AssocType : _ + - MACRO_CONST : _ + - method : _ + - module : type crate::module - Trait: t + - Trait : type "#]], ); check( @@ -987,10 +987,10 @@ pub trait Trait { "#, expect![[r#" crate - module: t + - module : type crate::module - Trait: t + - Trait : type "#]], ); } @@ -1015,10 +1015,10 @@ pub trait Trait { "#, expect![[r#" crate - ASSOC_CONST: _ - AssocType: _ - MACRO_CONST: _ - method: _ + - ASSOC_CONST : _ + - AssocType : _ + - MACRO_CONST : _ + - method : _ "#]], ); check( diff --git a/crates/hir-def/src/nameres/tests/primitives.rs b/crates/hir-def/src/nameres/tests/primitives.rs index 271eb1c79b12..861690238d47 100644 --- a/crates/hir-def/src/nameres/tests/primitives.rs +++ b/crates/hir-def/src/nameres/tests/primitives.rs @@ -13,11 +13,11 @@ pub use i32 as int; "#, expect![[r#" crate - foo: t - int: ti + - foo : type + - int : type (import) crate::foo - int: ti + - int : type (import) "#]], ); } From 217e3ca6180d00e5dfebd4aa6c2e87cf9b4c714c Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 22 Nov 2025 12:39:47 +1100 Subject: [PATCH 2/3] Also dump the macro sub-namespace of macros --- .../src/expr_store/tests/body/block.rs | 2 +- crates/hir-def/src/item_scope.rs | 14 +++-- crates/hir-def/src/nameres.rs | 2 +- crates/hir-def/src/nameres/tests.rs | 2 +- crates/hir-def/src/nameres/tests/globs.rs | 8 +-- crates/hir-def/src/nameres/tests/macros.rs | 54 +++++++++---------- 6 files changed, 45 insertions(+), 37 deletions(-) diff --git a/crates/hir-def/src/expr_store/tests/body/block.rs b/crates/hir-def/src/expr_store/tests/body/block.rs index e36f6550d512..f13f857cccc3 100644 --- a/crates/hir-def/src/expr_store/tests/body/block.rs +++ b/crates/hir-def/src/expr_store/tests/body/block.rs @@ -326,7 +326,7 @@ fn main() { - FooWorks : type value crate - - foo : macro + - foo : macro! - main : value "#]], ); diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs index 01adef5bccdb..204031ebee4b 100644 --- a/crates/hir-def/src/item_scope.rs +++ b/crates/hir-def/src/item_scope.rs @@ -4,7 +4,7 @@ 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; @@ -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, }; @@ -735,10 +736,16 @@ 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 { let display_name: &dyn fmt::Display = match &name { Some(name) => &name.display(db, Edition::LATEST), @@ -763,8 +770,9 @@ impl ItemScope { None => (), } } - if let Some(Item { import, .. }) = def.macros { + 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_str(" (import)"), Some(ImportOrExternCrate::Glob(_)) => buf.push_str(" (glob)"), diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs index 50958efa455f..f44187ec59c1 100644 --- a/crates/hir-def/src/nameres.rs +++ b/crates/hir-def/src/nameres.rs @@ -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, diff --git a/crates/hir-def/src/nameres/tests.rs b/crates/hir-def/src/nameres/tests.rs index 68f47f50bfcf..23d60d58f085 100644 --- a/crates/hir-def/src/nameres/tests.rs +++ b/crates/hir-def/src/nameres/tests.rs @@ -889,7 +889,7 @@ use self::m::S::{self}; - m : type crate::m - - S : type value macro + - S : type value macro! "#]], ); } diff --git a/crates/hir-def/src/nameres/tests/globs.rs b/crates/hir-def/src/nameres/tests/globs.rs index 779f7769bbe9..62887e29410f 100644 --- a/crates/hir-def/src/nameres/tests/globs.rs +++ b/crates/hir-def/src/nameres/tests/globs.rs @@ -391,24 +391,24 @@ use reexport::*; - Trait : type (glob) - defs : type - function : value (glob) - - makro : macro (glob) + - makro : macro! (glob) - reexport : type crate::defs - Trait : type - function : value - - makro : macro + - makro : macro! crate::reexport - Trait : type (glob) - function : value (glob) - inner : type - - makro : macro (glob) + - makro : macro! (glob) crate::reexport::inner - Trait : type (import) - function : value (import) - - makro : macro (import) + - makro : macro! (import) "#]], ); } diff --git a/crates/hir-def/src/nameres/tests/macros.rs b/crates/hir-def/src/nameres/tests/macros.rs index 5b60031ae371..9c2ca1b57f69 100644 --- a/crates/hir-def/src/nameres/tests/macros.rs +++ b/crates/hir-def/src/nameres/tests/macros.rs @@ -207,8 +207,8 @@ macro_rules! bar { expect![[r#" crate - Foo : type - - bar : macro (import) - - foo : macro (import) + - bar : macro! (import) + - foo : macro! (import) "#]], ); } @@ -555,9 +555,9 @@ fn baz() {} "#, expect![[r#" crate - - bar : type (import) macro (import) - - baz : type (import) value macro (import) - - foo : type macro + - bar : type (import) macro! (import) + - baz : type (import) value macro! (import) + - foo : type macro! "#]], ); } @@ -628,13 +628,13 @@ mod m { - OkAliasSuper : type value - OkCrate : type value - OkPlain : type value - - bar : macro + - bar : macro! - m : type crate::m - - alias1 : macro (import) - - alias2 : macro (import) - - alias3 : macro (import) + - alias1 : macro! (import) + - alias2 : macro! (import) + - alias3 : macro! (import) - not_found : _ "#]], ); @@ -794,7 +794,7 @@ pub trait Clone {} "#, expect![[r#" crate - - Clone : type (glob) macro (glob) + - Clone : type (glob) macro# (glob) "#]], ); } @@ -910,7 +910,7 @@ fn derive() {} expect![[r#" crate - S : type value - - derive : macro + - derive : macro# "#]], ); } @@ -1029,13 +1029,13 @@ pub fn derive_macro_2(_item: TokenStream) -> TokenStream { "#, expect![[r#" crate - - AnotherTrait : macro - - DummyTrait : macro + - AnotherTrait : macro# + - DummyTrait : macro# - TokenStream : type value - - attribute_macro : value macro + - attribute_macro : value macro# - derive_macro : value - derive_macro_2 : value - - function_like_macro : value macro + - function_like_macro : value macro! "#]], ); } @@ -1075,9 +1075,9 @@ macro_rules! mbe { "#, expect![[r#" crate - - DummyTrait : macro (glob) - - attribute_macro : macro (glob) - - function_like_macro : macro (glob) + - DummyTrait : macro# (glob) + - attribute_macro : macro# (glob) + - function_like_macro : macro! (glob) "#]], ); } @@ -1120,7 +1120,7 @@ structs!(Foo); expect![[r#" crate - Foo : type - - structs : macro + - structs : macro! "#]], ); } @@ -1196,8 +1196,8 @@ struct B; crate - A : type value - B : type value - - inner_a : macro - - inner_b : macro + - inner_a : macro! + - inner_b : macro! "#]], ); } @@ -1228,7 +1228,7 @@ struct A; expect![[r#" crate - A : type value - - inner : macro + - inner : macro! "#]], ); // eager -> MBE -> $crate::mbe @@ -1257,7 +1257,7 @@ struct A; expect![[r#" crate - A : type value - - inner : macro + - inner : macro! "#]], ); } @@ -1501,9 +1501,9 @@ pub mod prelude { expect![[r#" crate - Ok : type value - - bar : macro + - bar : macro! - dep : type (extern) - - foo : macro + - foo : macro! - ok : value "#]], ); @@ -1589,8 +1589,8 @@ pub mod prelude { expect![[r#" crate - Ok : type value - - bar : macro (import) - - foo : macro (import) + - bar : macro# (import) + - foo : macro# (import) - ok : value "#]], ); From 3081be6d59e71a0f7c3af2e1ac4c80644b5afd44 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 22 Nov 2025 12:49:30 +1100 Subject: [PATCH 3/3] Also dump legacy-textual-scope macros --- .../src/expr_store/tests/body/block.rs | 8 +++ crates/hir-def/src/item_scope.rs | 15 +++++ crates/hir-def/src/nameres/tests/macros.rs | 56 +++++++++++++++++++ .../src/nameres/tests/mod_resolution.rs | 2 + 4 files changed, 81 insertions(+) diff --git a/crates/hir-def/src/expr_store/tests/body/block.rs b/crates/hir-def/src/expr_store/tests/body/block.rs index f13f857cccc3..e8334cd973df 100644 --- a/crates/hir-def/src/expr_store/tests/body/block.rs +++ b/crates/hir-def/src/expr_store/tests/body/block.rs @@ -255,6 +255,7 @@ fn f() { crate - f : value + - (legacy) mark : macro! "#]], ); } @@ -328,6 +329,8 @@ fn main() { crate - foo : macro! - main : value + - (legacy) bar : macro! + - (legacy) foo : macro! "#]], ); } @@ -362,6 +365,7 @@ fn f() { crate::module - f : value + - (legacy) m : macro! "#]], ) } @@ -449,6 +453,8 @@ fn foo() { crate - foo : value + - (legacy) declare : macro! + - (legacy) inner_declare : macro! "#]], ) } @@ -501,6 +507,7 @@ fn foo() { crate - foo : value + - (legacy) mac : macro! "#]], ) } @@ -523,6 +530,7 @@ fn f() {$0 crate - f : value + - (legacy) foo : macro! "#]], ) } diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs index 204031ebee4b..51c42c995c9f 100644 --- a/crates/hir-def/src/item_scope.rs +++ b/crates/hir-def/src/item_scope.rs @@ -786,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::>(); + 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 ¯o_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) { diff --git a/crates/hir-def/src/nameres/tests/macros.rs b/crates/hir-def/src/nameres/tests/macros.rs index 9c2ca1b57f69..43b6e12e1357 100644 --- a/crates/hir-def/src/nameres/tests/macros.rs +++ b/crates/hir-def/src/nameres/tests/macros.rs @@ -25,10 +25,12 @@ structs!(Bar, Baz); crate - Foo : type - nested : type + - (legacy) structs : macro! crate::nested - Bar : type - Baz : type + - (legacy) structs : macro! "#]], ); } @@ -55,18 +57,23 @@ struct Y; crate - m : type - n1 : type + - (legacy) m : macro! crate::m - n3 : type + - (legacy) m : macro! crate::m::n3 - Y : type value + - (legacy) m : macro! crate::n1 - n2 : type + - (legacy) m : macro! crate::n1::n2 - X : type value + - (legacy) m : macro! "#]], ); } @@ -209,6 +216,7 @@ macro_rules! bar { - Foo : type - bar : macro! (import) - foo : macro! (import) + - (legacy) baz : macro! "#]], ); } @@ -401,6 +409,7 @@ mod prelude { - prelude : type crate::prelude + - (legacy) declare_mod : macro! "#]], ); } @@ -420,6 +429,7 @@ macro_rules! m { expect![[r#" crate - S : type value + - (legacy) m : macro! "#]], ); // FIXME: should not expand. legacy macro scoping is not implemented. @@ -509,8 +519,11 @@ macro_rules! baz { - m5 : type - m7 : type - ok_double_macro_use_shadow : value + - (legacy) baz : macro! + - (legacy) foo : macro! macro! macro! crate::m1 + - (legacy) bar : macro! crate::m2 @@ -521,18 +534,30 @@ macro_rules! baz { - m4 : type - m5 : type - ok_shadow : value + - (legacy) bar : macro! macro! + - (legacy) baz : macro! + - (legacy) foo : macro! macro! macro! macro! crate::m3::m4 - ok_shadow_deep : value + - (legacy) bar : macro! + - (legacy) foo : macro! macro! crate::m3::m5 + - (legacy) bar : macro! + - (legacy) baz : macro! + - (legacy) foo : macro! macro! macro! crate::m5 - m6 : type + - (legacy) foo : macro! macro! crate::m5::m6 + - (legacy) foo : macro! macro! crate::m7 + - (legacy) baz : macro! + - (legacy) foo : macro! macro! "#]], ); // FIXME: should not see `NotFoundBefore` @@ -558,6 +583,7 @@ fn baz() {} - bar : type (import) macro! (import) - baz : type (import) value macro! (import) - foo : type macro! + - (legacy) foo : macro! "#]], ); } @@ -630,12 +656,15 @@ mod m { - OkPlain : type value - bar : macro! - m : type + - (legacy) foo : macro! crate::m - alias1 : macro! (import) - alias2 : macro! (import) - alias3 : macro! (import) - not_found : _ + - (legacy) bar : macro! + - (legacy) foo : macro! "#]], ); } @@ -692,8 +721,10 @@ pub struct Baz; - FooSelf : type (import) value (import) - foo : type (extern) - m : type + - (legacy) current : macro! crate::m + - (legacy) current : macro! "#]], ); } @@ -884,6 +915,7 @@ extern { expect![[r#" crate - S : value + - (legacy) m : macro! "#]], ); } @@ -975,6 +1007,8 @@ b! { static = #[] ();} "#, expect![[r#" crate + - (legacy) a : macro! + - (legacy) b : macro! "#]], ); } @@ -996,6 +1030,8 @@ indirect_macro!(); expect![[r#" crate - S : type + - (legacy) indirect_macro : macro! + - (legacy) item : macro! "#]], ); } @@ -1161,6 +1197,7 @@ m!( "#, expect![[r#" crate + - (legacy) m : macro! "#]], ) } @@ -1198,6 +1235,9 @@ struct B; - B : type value - inner_a : macro! - inner_b : macro! + - (legacy) include : macro! + - (legacy) inner_a : macro! + - (legacy) inner_b : macro! "#]], ); } @@ -1229,6 +1269,9 @@ struct A; crate - A : type value - inner : macro! + - (legacy) include : macro! + - (legacy) inner : macro! + - (legacy) m : macro! "#]], ); // eager -> MBE -> $crate::mbe @@ -1258,6 +1301,9 @@ struct A; crate - A : type value - inner : macro! + - (legacy) include : macro! + - (legacy) inner : macro! + - (legacy) n : macro! "#]], ); } @@ -1393,29 +1439,37 @@ pub struct Url {} expect![[r#" crate - nested : type + - (legacy) include : macro! crate::nested - company_name : type - different_company : type - util : type + - (legacy) include : macro! crate::nested::company_name - network : type + - (legacy) include : macro! crate::nested::company_name::network - v1 : type + - (legacy) include : macro! crate::nested::company_name::network::v1 - IpAddress : type + - (legacy) include : macro! crate::nested::different_company - network : type + - (legacy) include : macro! crate::nested::different_company::network - Url : type + - (legacy) include : macro! crate::nested::util - Helper : type + - (legacy) include : macro! "#]], ); } @@ -1535,9 +1589,11 @@ macro_rules! mk_foo { crate - a : type - lib : type (extern) + - (legacy) foo : macro! crate::a - Ok : type value + - (legacy) foo : macro! "#]], ); } diff --git a/crates/hir-def/src/nameres/tests/mod_resolution.rs b/crates/hir-def/src/nameres/tests/mod_resolution.rs index 63e746d0b412..e54bcc28d27f 100644 --- a/crates/hir-def/src/nameres/tests/mod_resolution.rs +++ b/crates/hir-def/src/nameres/tests/mod_resolution.rs @@ -965,6 +965,7 @@ pub trait Trait { crate::module - Trait : type + - (legacy) m : macro! "#]], ); check( @@ -991,6 +992,7 @@ pub trait Trait { crate::module - Trait : type + - (legacy) m : macro! "#]], ); }