Fix name resolution of shadowed builtin macro

This commit is contained in:
Chayim Refael Friedman 2024-08-28 23:20:46 +03:00
parent 199c01df7a
commit 4880eee781
3 changed files with 33 additions and 1 deletions

View File

@ -1606,7 +1606,11 @@ fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
// Prelude module is always considered to be `#[macro_use]`. // Prelude module is always considered to be `#[macro_use]`.
if let Some((prelude_module, _use)) = self.def_collector.def_map.prelude { 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); cov_mark::hit!(prelude_is_macro_use);
self.def_collector.import_macros_from_extern_crate( self.def_collector.import_macros_from_extern_crate(
prelude_module.krate, prelude_module.krate,

View File

@ -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!();
}
"#,
);
}
} }

View File

@ -63,6 +63,7 @@
//! unsize: sized //! unsize: sized
//! todo: panic //! todo: panic
//! unimplemented: panic //! unimplemented: panic
//! column:
#![rustc_coherence_is_core] #![rustc_coherence_is_core]
@ -1617,6 +1618,14 @@ fn source(&self) -> Option<&(dyn Error + 'static)> {
} }
// endregion:error // endregion:error
// region:column
#[rustc_builtin_macro]
#[macro_export]
macro_rules! column {
() => {};
}
// endregion:column
pub mod prelude { pub mod prelude {
pub mod v1 { pub mod v1 {
pub use crate::{ pub use crate::{