Unnamed repository; edit this file 'description' to name the repository.
Do not `#[macro_use]` the sysroot crates
They were so in the past, but in https://github.com/rust-lang/rust/pull/139493 they were changed to not be, instead macros are now explicitly exported from the prelude. This is important as now there are macros (`assert_matches!()`) that are *not* exported by default despite sitting in the crate root. That change has landed in 1.94.0, 4 Rust versions ago, and we only officially support that last stdlib, so I think it's fine to just not support older versions.
Chayim Refael Friedman 4 weeks ago
parent 63a6f0d · commit 2a422f9
-rw-r--r--crates/hir-def/src/nameres/collector.rs13
-rw-r--r--crates/hir-def/src/nameres/tests/macros.rs36
-rw-r--r--crates/test-utils/src/minicore.rs11
3 files changed, 17 insertions, 43 deletions
diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs
index a15d6d7474..bb61cebd4f 100644
--- a/crates/hir-def/src/nameres/collector.rs
+++ b/crates/hir-def/src/nameres/collector.rs
@@ -1877,7 +1877,6 @@ impl ModCollector<'_, '_> {
}
fn collect(&mut self, items: &[ModItemId], container: ItemContainerId) {
- let krate = self.def_collector.def_map.krate;
let is_crate_root = self.module_id == self.def_collector.def_map.root
&& self.def_collector.def_map.block.is_none();
@@ -1885,18 +1884,6 @@ impl ModCollector<'_, '_> {
// for macros.
self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone());
- // Prelude module is always considered to be `#[macro_use]`.
- if let Some((prelude_module, _use)) = self.def_collector.def_map.prelude {
- // Don't insert macros from the prelude into blocks, as they can be shadowed by other macros.
- if is_crate_root && prelude_module.krate(self.def_collector.db) != krate {
- cov_mark::hit!(prelude_is_macro_use);
- self.def_collector.import_macros_from_extern_crate(
- prelude_module.krate(self.def_collector.db),
- None,
- None,
- );
- }
- }
let db = self.def_collector.db;
let module_id = self.module_id;
let consider_deferred_derives =
diff --git a/crates/hir-def/src/nameres/tests/macros.rs b/crates/hir-def/src/nameres/tests/macros.rs
index f073cf777d..e9cb0f1dd3 100644
--- a/crates/hir-def/src/nameres/tests/macros.rs
+++ b/crates/hir-def/src/nameres/tests/macros.rs
@@ -339,36 +339,19 @@ macro_rules! baz3 { () => { struct OkBaz3; } }
}
#[test]
-fn prelude_is_macro_use() {
- cov_mark::check!(prelude_is_macro_use);
+fn prelude_is_not_macro_use() {
check(
r#"
//- /main.rs edition:2018 crate:main deps:std
structs!(Foo);
-structs_priv!(Bar);
-structs_outside!(Out);
-crate::structs!(MacroNotResolved2);
-
-mod bar;
-
-//- /bar.rs
-structs!(Baz);
-crate::structs!(MacroNotResolved3);
+structs_outside!(MacroNotResolved);
//- /lib.rs crate:std
pub mod prelude {
pub mod rust_2018 {
- #[macro_export]
- macro_rules! structs {
+ pub macro structs {
($i:ident) => { struct $i; }
}
-
- mod priv_mod {
- #[macro_export]
- macro_rules! structs_priv {
- ($i:ident) => { struct $i; }
- }
- }
}
}
@@ -379,13 +362,7 @@ macro_rules! structs_outside {
"#,
expect![[r#"
crate
- - Bar : type value
- Foo : type value
- - Out : type value
- - bar : type
-
- crate::bar
- - Baz : type value
"#]],
);
}
@@ -743,12 +720,11 @@ foo!();
pub use core::foo;
pub mod prelude {
- pub mod rust_2018 {}
+ pub mod rust_2018 {
+ pub use crate::foo;
+ }
}
-#[macro_use]
-mod std_macros;
-
//- /core.rs crate:core
#[macro_export]
macro_rules! foo {
diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs
index f9be47551c..f3a460b9ac 100644
--- a/crates/test-utils/src/minicore.rs
+++ b/crates/test-utils/src/minicore.rs
@@ -2515,6 +2515,7 @@ macro_rules! matches {
pub mod prelude {
pub mod v1 {
+ #[rustfmt::skip]
pub use crate::{
clone::Clone, // :clone
cmp::{Eq, PartialEq}, // :eq
@@ -2541,6 +2542,16 @@ pub mod prelude {
panic, // :panic
result::Result::{self, Err, Ok}, // :result
str::FromStr, // :str
+ write, writeln, // :write
+ assert, // :assert
+ format_args, format_args_nl, const_format_args, print, // :fmt
+ todo, // :todo
+ unimplemented, // :unimplemented
+ include, // :include
+ include_bytes, // :include_bytes
+ concat, // :concat
+ env, option_env, // :env
+ matches, // :matches
};
}