From 14db11541b09161a8e1acc49c7d747c03b0657b2 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Mon, 7 Apr 2025 19:30:51 +0200 Subject: [PATCH 1/6] Explicitly export core and std macros Currently all core and std macros are automatically added to the prelude via #[macro_use]. However a situation arose where we want to add a new macro `assert_matches` but don't want to pull it into the standard prelude for compatibility reasons. By explicitly exporting the macros found in the core and std crates we get to decide on a per macro basis and can later add them via the rust_20xx preludes. --- .../src/standard_library_imports.rs | 2 +- library/core/src/prelude/v1.rs | 53 +++++++++++++++++- library/proc_macro/src/bridge/symbol.rs | 4 +- library/std/src/prelude/v1.rs | 55 +++++++++++++++++-- 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/standard_library_imports.rs b/compiler/rustc_builtin_macros/src/standard_library_imports.rs index a1ee53b7ca21..748dacc284e9 100644 --- a/compiler/rustc_builtin_macros/src/standard_library_imports.rs +++ b/compiler/rustc_builtin_macros/src/standard_library_imports.rs @@ -43,7 +43,7 @@ pub fn inject( let item = cx.item( span, - thin_vec![cx.attr_word(sym::macro_use, span)], + ast::AttrVec::new(), ast::ItemKind::ExternCrate(None, Ident::new(name, ident_span)), ); diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index c5975c030503..0a9a52fe66a5 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -61,11 +61,17 @@ pub use crate::hash::macros::Hash; #[allow(deprecated)] #[doc(no_inline)] pub use crate::{ - assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args, - format_args_nl, include, include_bytes, include_str, line, log_syntax, module_path, option_env, - stringify, trace_macros, + assert, assert_eq, assert_ne, cfg, column, compile_error, concat, debug_assert, debug_assert_eq, debug_assert_ne, env, file, format_args, include, include_bytes, include_str, line, matches, module_path, option_env, panic, stringify, todo, r#try, unimplemented, unreachable, write, writeln, }; +#[unstable(feature = "ub_checks", issue = "none")] +#[doc(no_inline)] +pub use crate::assert_unsafe_precondition; + +#[unstable(feature = "cfg_match", issue = "115585")] +#[doc(no_inline)] +pub use crate::cfg_match; + #[unstable( feature = "concat_bytes", issue = "87555", @@ -74,6 +80,47 @@ pub use crate::{ #[doc(no_inline)] pub use crate::concat_bytes; +#[unstable( + feature = "concat_idents", + issue = "29599", + reason = "`concat_idents` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use crate::concat_idents; + +#[unstable(feature = "const_format_args", issue = "none")] +#[doc(no_inline)] +pub use crate::const_format_args; + +#[unstable( + feature = "format_args_nl", + issue = "none", + reason = "`format_args_nl` is only for internal \ + language use and is subject to change" +)] +#[doc(no_inline)] +pub use crate::format_args_nl; + +#[unstable( + feature = "log_syntax", + issue = "29598", + reason = "`log_syntax!` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use crate::log_syntax; + +#[unstable(feature = "pattern_type_macro", issue = "123646")] +#[doc(no_inline)] +pub use crate::pattern_type; + +#[unstable( + feature = "trace_macros", + issue = "29598", + reason = "`trace_macros` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use crate::trace_macros; + // Do not `doc(no_inline)` so that they become doc items on their own // (no public module for them to be re-exported from). #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] diff --git a/library/proc_macro/src/bridge/symbol.rs b/library/proc_macro/src/bridge/symbol.rs index 6a1cecd69fb5..99f00323398f 100644 --- a/library/proc_macro/src/bridge/symbol.rs +++ b/library/proc_macro/src/bridge/symbol.rs @@ -12,8 +12,10 @@ use std::cell::RefCell; use std::num::NonZero; use std::str; +use std::fmt; -use super::*; +// Explicit import to avoid macro namespace collision. +use super::{arena, client, DecodeMut, Encode, fxhash, Mark, Marked, Reader, server, Unmark, Writer}; /// Handle for a symbol string stored within the Interner. #[derive(Copy, Clone, PartialEq, Eq, Hash)] diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index 4217f6586407..de0e7402ef8a 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -43,16 +43,26 @@ pub use crate::option::Option::{self, None, Some}; #[doc(no_inline)] pub use crate::result::Result::{self, Err, Ok}; -// Re-exported built-in macros +// Re-exported built-in macros and traits #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[allow(deprecated)] #[doc(no_inline)] pub use core::prelude::v1::{ - assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args, - format_args_nl, include, include_bytes, include_str, line, log_syntax, module_path, option_env, - stringify, trace_macros, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, + assert, assert_eq, assert_ne, cfg, column, compile_error, concat, debug_assert, debug_assert_eq, debug_assert_ne, env, file, format_args, include, include_bytes, include_str, line, matches, + module_path, option_env, panic, stringify, todo, r#try, unimplemented, unreachable, write, + writeln, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, }; +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +#[doc(no_inline)] +pub use crate::{ + dbg, eprint, eprintln, format, is_x86_feature_detected, print, println, thread_local, vec, +}; + +#[unstable(feature = "cfg_match", issue = "115585")] +#[doc(no_inline)] +pub use core::prelude::v1::cfg_match; + #[unstable( feature = "concat_bytes", issue = "87555", @@ -61,6 +71,43 @@ pub use core::prelude::v1::{ #[doc(no_inline)] pub use core::prelude::v1::concat_bytes; +#[unstable( + feature = "concat_idents", + issue = "29599", + reason = "`concat_idents` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use core::prelude::v1::concat_idents; + +#[unstable(feature = "const_format_args", issue = "none")] +#[doc(no_inline)] +pub use core::prelude::v1::const_format_args; + +#[unstable( + feature = "format_args_nl", + issue = "none", + reason = "`format_args_nl` is only for internal \ + language use and is subject to change" +)] +#[doc(no_inline)] +pub use core::prelude::v1::format_args_nl; + +#[unstable( + feature = "log_syntax", + issue = "29598", + reason = "`log_syntax!` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use core::prelude::v1::log_syntax; + +#[unstable( + feature = "trace_macros", + issue = "29598", + reason = "`trace_macros` is not stable enough for use and is subject to change" +)] +#[doc(no_inline)] +pub use core::prelude::v1::trace_macros; + // Do not `doc(no_inline)` so that they become doc items on their own // (no public module for them to be re-exported from). #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] From 2ac3f3276d615a4857fc1be46807502cca315e85 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Tue, 8 Apr 2025 10:55:39 +0200 Subject: [PATCH 2/6] Apply review comments --- library/core/src/prelude/v1.rs | 13 ------------- library/std/src/prelude/v1.rs | 9 --------- library/std/src/process.rs | 2 +- 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index 0a9a52fe66a5..0721b19a6c18 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -64,10 +64,6 @@ pub use crate::{ assert, assert_eq, assert_ne, cfg, column, compile_error, concat, debug_assert, debug_assert_eq, debug_assert_ne, env, file, format_args, include, include_bytes, include_str, line, matches, module_path, option_env, panic, stringify, todo, r#try, unimplemented, unreachable, write, writeln, }; -#[unstable(feature = "ub_checks", issue = "none")] -#[doc(no_inline)] -pub use crate::assert_unsafe_precondition; - #[unstable(feature = "cfg_match", issue = "115585")] #[doc(no_inline)] pub use crate::cfg_match; @@ -92,15 +88,6 @@ pub use crate::concat_idents; #[doc(no_inline)] pub use crate::const_format_args; -#[unstable( - feature = "format_args_nl", - issue = "none", - reason = "`format_args_nl` is only for internal \ - language use and is subject to change" -)] -#[doc(no_inline)] -pub use crate::format_args_nl; - #[unstable( feature = "log_syntax", issue = "29598", diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index de0e7402ef8a..8a58c7179200 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -83,15 +83,6 @@ pub use core::prelude::v1::concat_idents; #[doc(no_inline)] pub use core::prelude::v1::const_format_args; -#[unstable( - feature = "format_args_nl", - issue = "none", - reason = "`format_args_nl` is only for internal \ - language use and is subject to change" -)] -#[doc(no_inline)] -pub use core::prelude::v1::format_args_nl; - #[unstable( feature = "log_syntax", issue = "29598", diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 3b765a9537bc..5e5cb8b39417 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -171,7 +171,7 @@ use crate::sys::process as imp; #[stable(feature = "command_access", since = "1.57.0")] pub use crate::sys_common::process::CommandEnvs; use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; -use crate::{fmt, fs, str}; +use crate::{fmt, format_args_nl, fs, str}; /// Representation of a running or exited child process. /// From 6c9b6320b607773f6aac3e50ab5406d7bd4c91cb Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Tue, 8 Apr 2025 11:03:08 +0200 Subject: [PATCH 3/6] Fix formatting tidy issues --- library/proc_macro/src/bridge/symbol.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/proc_macro/src/bridge/symbol.rs b/library/proc_macro/src/bridge/symbol.rs index 99f00323398f..6ff480460e6f 100644 --- a/library/proc_macro/src/bridge/symbol.rs +++ b/library/proc_macro/src/bridge/symbol.rs @@ -11,11 +11,12 @@ use std::cell::RefCell; use std::num::NonZero; -use std::str; -use std::fmt; +use std::{fmt, str}; // Explicit import to avoid macro namespace collision. -use super::{arena, client, DecodeMut, Encode, fxhash, Mark, Marked, Reader, server, Unmark, Writer}; +use super::{ + DecodeMut, Encode, Mark, Marked, Reader, Unmark, Writer, arena, client, fxhash, server, +}; /// Handle for a symbol string stored within the Interner. #[derive(Copy, Clone, PartialEq, Eq, Hash)] From 3699f8d56a4ec9b5e3dec40dd198c9dfd083a44b Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Tue, 8 Apr 2025 19:52:28 +0200 Subject: [PATCH 4/6] Special case vec macro --- library/std/src/prelude/v1.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index 8a58c7179200..e979059072af 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -56,9 +56,20 @@ pub use core::prelude::v1::{ #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(no_inline)] pub use crate::{ - dbg, eprint, eprintln, format, is_x86_feature_detected, print, println, thread_local, vec, + dbg, eprint, eprintln, format, is_x86_feature_detected, print, println, thread_local, }; +// The `vec` macro needs special handling, so that we don't export it *and* the `std::vec` module at +// the same time. We only want the macro in the prelude. +mod vec_macro_only { + #[allow(hidden_glob_reexports)] + mod vec {} + #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] + pub use crate::*; +} +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +pub use self::vec_macro_only::vec; + #[unstable(feature = "cfg_match", issue = "115585")] #[doc(no_inline)] pub use core::prelude::v1::cfg_match; From 25f46fb7318c96914844ab80159d1c491a460525 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Wed, 9 Apr 2025 08:48:43 +0200 Subject: [PATCH 5/6] Explicitly import format_args_nl --- src/librustdoc/html/highlight.rs | 1 + .../rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs | 2 +- tests/ui/hygiene/format-args.rs | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index c943d3ad4d05..b024b62f3ec2 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -7,6 +7,7 @@ use std::collections::VecDeque; use std::fmt::{Display, Write}; +use std::format_args_nl; use rustc_data_structures::fx::FxIndexMap; use rustc_lexer::{Cursor, LiteralKind, TokenKind}; diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs index 8f69bb823000..21196ecf9468 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs @@ -445,7 +445,7 @@ fn test_string_highlighting() { //- minicore: fmt, assert, asm, concat, panic macro_rules! println { ($($arg:tt)*) => ({ - $crate::io::_print(format_args_nl!($($arg)*)); + $crate::io::_print(std::format_args_nl!($($arg)*)); }) } diff --git a/tests/ui/hygiene/format-args.rs b/tests/ui/hygiene/format-args.rs index ff08aecfd9eb..f845f5d8a4c8 100644 --- a/tests/ui/hygiene/format-args.rs +++ b/tests/ui/hygiene/format-args.rs @@ -3,6 +3,8 @@ #![allow(non_upper_case_globals)] #![feature(format_args_nl)] +use std::format_args_nl; + static arg0: () = (); fn main() { From 9eaeba8741e750323fc6ea30768efe827479eb90 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Wed, 9 Apr 2025 08:59:57 +0200 Subject: [PATCH 6/6] Exclude panic and env namespaces from prelude --- library/core/src/prelude/v1.rs | 15 ++++++++++++++- library/std/src/prelude/v1.rs | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/v1.rs index 0721b19a6c18..c012ba6ea01f 100644 --- a/library/core/src/prelude/v1.rs +++ b/library/core/src/prelude/v1.rs @@ -61,9 +61,22 @@ pub use crate::hash::macros::Hash; #[allow(deprecated)] #[doc(no_inline)] pub use crate::{ - assert, assert_eq, assert_ne, cfg, column, compile_error, concat, debug_assert, debug_assert_eq, debug_assert_ne, env, file, format_args, include, include_bytes, include_str, line, matches, module_path, option_env, panic, stringify, todo, r#try, unimplemented, unreachable, write, writeln, + assert, assert_eq, assert_ne, cfg, column, compile_error, concat, debug_assert, debug_assert_eq, debug_assert_ne, file, format_args, include, include_bytes, include_str, line, matches, module_path, option_env, stringify, todo, r#try, unimplemented, unreachable, write, writeln, }; +// These macros needs special handling, so that we don't export it *and* the modules of the same +// name. We only want the macro in the prelude. +mod ambiguous_macro_only { + #[allow(hidden_glob_reexports)] + mod env {} + #[allow(hidden_glob_reexports)] + mod panic {} + #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] + pub use crate::*; +} +#[stable(feature = "builtin_macro_prelude", since = "1.38.0")] +pub use self::ambiguous_macro_only::{env, panic}; + #[unstable(feature = "cfg_match", issue = "115585")] #[doc(no_inline)] pub use crate::cfg_match; diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs index e979059072af..729843deaf95 100644 --- a/library/std/src/prelude/v1.rs +++ b/library/std/src/prelude/v1.rs @@ -59,16 +59,16 @@ pub use crate::{ dbg, eprint, eprintln, format, is_x86_feature_detected, print, println, thread_local, }; -// The `vec` macro needs special handling, so that we don't export it *and* the `std::vec` module at -// the same time. We only want the macro in the prelude. -mod vec_macro_only { +// These macros needs special handling, so that we don't export it *and* the modules of the same +// name. We only want the macro in the prelude. +mod ambiguous_macro_only_std { #[allow(hidden_glob_reexports)] mod vec {} #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] pub use crate::*; } #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] -pub use self::vec_macro_only::vec; +pub use self::ambiguous_macro_only_std::vec; #[unstable(feature = "cfg_match", issue = "115585")] #[doc(no_inline)]