diff --git a/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs b/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs index 22eb5a174d3..96db3db8f0d 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs @@ -1606,7 +1606,11 @@ fn collect(&mut self, items: &[ModItem], container: ItemContainerId) { // Prelude module is always considered to be `#[macro_use]`. if let Some((prelude_module, _use)) = self.def_collector.def_map.prelude { - if prelude_module.krate != krate && is_crate_root { + // Don't insert macros from the prelude into blocks, as they can be shadowed by other macros. + if prelude_module.krate != krate + && is_crate_root + && self.def_collector.def_map.block.is_none() + { cov_mark::hit!(prelude_is_macro_use); self.def_collector.import_macros_from_extern_crate( prelude_module.krate, diff --git a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs index 5769f2cabc7..971cd3ef585 100644 --- a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs +++ b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs @@ -2731,4 +2731,23 @@ fn main() { "#, ) } + + #[test] + fn shadow_builtin_macro() { + check( + r#" +//- minicore: column +//- /a.rs crate:a +#[macro_export] +macro_rules! column { () => {} } + // ^^^^^^ + +//- /b.rs crate:b deps:a +use a::column; +fn foo() { + $0column!(); +} + "#, + ); + } } diff --git a/src/tools/rust-analyzer/crates/test-utils/src/minicore.rs b/src/tools/rust-analyzer/crates/test-utils/src/minicore.rs index 7dbc498ead1..3be4469beef 100644 --- a/src/tools/rust-analyzer/crates/test-utils/src/minicore.rs +++ b/src/tools/rust-analyzer/crates/test-utils/src/minicore.rs @@ -63,6 +63,7 @@ //! unsize: sized //! todo: panic //! unimplemented: panic +//! column: #![rustc_coherence_is_core] @@ -1617,6 +1618,14 @@ fn source(&self) -> Option<&(dyn Error + 'static)> { } // endregion:error +// region:column +#[rustc_builtin_macro] +#[macro_export] +macro_rules! column { + () => {}; +} +// endregion:column + pub mod prelude { pub mod v1 { pub use crate::{