Skip to content

Conversation

@Voultapher
Copy link
Contributor

@Voultapher Voultapher commented Apr 7, 2025

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.

Closes #53977
Unlocks #137487

@rustbot
Copy link
Collaborator

rustbot commented Apr 7, 2025

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 7, 2025
@Voultapher
Copy link
Contributor Author

r? @Amanieu

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Voultapher
Copy link
Contributor Author

@Amanieu the tidy issue highlights an annoying and unforeseen side-effect of this change. The vec module is now part of the prelude. In effect this means that for example this code:

fn xx(i: vec::IntoIter<i32>) {
    let _ = i.as_slice();
}

fn main() {}

that currently doesn't compile on stable would now compile. Initially I thought this would cause name collisions if users define their own vec module but so far I wasn't able to produce those, it seems to always prefer the local module. But regardless, I think we don't want to allow access to a standard library namespace without going through std, alloc or core. AFAIK there is no way to pub use only the macro and not the module namespace without modifications. I have two ideas how to tackle this, maybe we can rename vec to vec_xx internally and have separate use expressions or we have to add another crate that we can #[macro_use] inject into the prelude that only contains the vec macro. Thoughts?

@traviscross
Copy link
Contributor

@petrochenkov
Copy link
Contributor

There's an issue for this change - #53977.

@dtolnay
Copy link
Member

dtolnay commented Apr 8, 2025

@Voultapher, avoiding the vec module re-export can be done like this:

#[macro_export]
macro_rules! myvec {
    () => {};
}

pub mod myvec {
    pub struct Vec;
}

pub mod prelude {
    // Bad: re-exports both macro and type namespace
    // pub use crate::myvec;
    
    mod vec_macro_only {
        #[allow(hidden_glob_reexports)]
        mod myvec {}
        pub use crate::*;
    }
    pub use self::vec_macro_only::myvec;
}

fn main() {
    prelude::myvec!();
    let _: prelude::myvec::Vec; // error
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5e50828c593e04ba0e98f48c9d8696b4

@Voultapher
Copy link
Contributor Author

I've applied the suggestion by @dtolnay local tests seem promising. @Kobzol could we please do a timer run to see if this PR impacts compile-times.

@petrochenkov
Copy link
Contributor

env and panic (and maybe something else now?) need to be treated in the same way as vec.

@rust-log-analyzer

This comment has been minimized.

@Kobzol
Copy link
Member

Kobzol commented Apr 8, 2025

@Voultapher Based on the CI failure I think that a try build would fail now.

@Voultapher
Copy link
Contributor Author

Ok, I'll try to get the CI passing first.

@Voultapher
Copy link
Contributor Author

@petrochenkov I went through all macros and searched the docs and env and panic seem to be the only other ones affected.

@rust-log-analyzer

This comment has been minimized.

@Voultapher
Copy link
Contributor Author

@Amanieu this program previously worked:

use std::*;

fn main() {
    panic!("panic works")
}

and now runs into:

error[E0659]: `panic` is ambiguous
   --> src/main.rs:4:5
    |
4   |     panic!("panic works")
    |     ^^^^^ ambiguous name
    |
    = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
note: `panic` could refer to the macro imported here
   --> src/main.rs:1:5
    |
1   | use std::*;
    |     ^^^^^^
    = help: consider adding an explicit import of `panic` to disambiguate
    = help: or use `crate::panic` to refer to this macro unambiguously
note: `panic` could also refer to the macro defined here
   --> rust/library/std/src/prelude/mod.rs:157:13
    |
157 |     pub use super::v1::*;
    |             ^^^^^^^^^

I don't see how we can resolve that without changing language import rules and or special casing the prelude import.

@Amanieu
Copy link
Member

Amanieu commented Apr 9, 2025

@petrochenkov Do you have any ideas about that?

@petrochenkov petrochenkov self-assigned this Apr 9, 2025
@petrochenkov
Copy link
Contributor

Could you add a test making sure that the modules vec, env and panic are not in prelude?

@petrochenkov
Copy link
Contributor

@petrochenkov Do you have any ideas about that?

The ambiguity wouldn't happen if it was the same panic in std root and in the stdlib prelude.
However, std and core have two different panic macros.

Previously #[macro_use] extern crate std; would add the std's panic to macro_use prelude, and #[macro_use] extern crate core; would add the core's panic.
This PR always adds the core's panic.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 23, 2025
@petrochenkov
Copy link
Contributor

@bors try

@craterbot run mode=check-only p=1 crates=https://crater-reports.s3.amazonaws.com/pr-139493-1/retry-regressed-list.txt

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 23, 2025
…ros, r=<try>

Explicitly export core and std macros
@petrochenkov petrochenkov added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2025
@rust-bors
Copy link

rust-bors bot commented Oct 23, 2025

☀️ Try build successful (CI)
Build commit: 09335b6 (09335b6f1bff25ec39f47f1533e126fbe1ca5f5d, parent: 11d2046fe9962720558cb15f72541f7fd170dec9)

@petrochenkov
Copy link
Contributor

@craterbot
Copy link
Collaborator

👌 Experiment pr-139493-2 created and queued.
🤖 Automatically detected try build 09335b6
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🚧 Experiment pr-139493-2 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-139493-2 is completed!
📊 21 regressed and 0 fixed (6580 total)
📊 262 spurious results on the retry-regessed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-139493-2/retry-regressed-list.txt

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 24, 2025
@petrochenkov
Copy link
Contributor

Significant regressions:

  • themasch.rust-jvm.2d7418c568670d8bcc52ef7e64cc0704f9c34eb1 - dbg! is ambiguous (glob import of custom dbg vs prelude)
  • dmidecode-1.0.0 - assert_eq! is ambiguous (manual glob import from prelude vs macro-expanded single import of custom partial_eq from pretty_assertions)
  • gcode-0.6.1 - assert_eq! is ambiguous (manual glob import from prelude vs macro_use of custom partial_eq from pretty_assertions)
  • threescalers-0.8.0 - matches! is ambiguous (manual glob import from prelude vs custom matches)
  • ipcrypt-0.1.0 - cannot find macro assert_eq due to no_implicit_prelude

Insignificant regressions:

  • cometbft-config-0.1.0-alpha.2 - ambiguous_panic_imports with denied lints
  • deranged-0.5.4 - ambiguous_panic_imports with denied lints
  • tendermint-config-0.40.4 - ambiguous_panic_imports with denied lints
  • playdate-* crates - uses format_args_nl (unstable feature)
  • thescribe11.Rust-SST.3765b213d621b25f400804842a040ed5916fb048 - uses format_args_nl (unstable feature)
  • berkus.mre-write_to.f0dca907fc0573b643ad076f12c3f0515665cabf - uses format_args_nl (unstable feature)

@petrochenkov
Copy link
Contributor

Looks good enough to me, if you send fixes to the crates from the "significant" category.

Could you also write a more detailed proposal / change description for this change, so I could send it to language and library teams for formal approval?
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 24, 2025
@bors
Copy link
Collaborator

bors commented Oct 29, 2025

☔ The latest upstream changes (presumably #148208) made this pull request unmergeable. Please resolve the merge conflicts.

@PoignardAzur
Copy link
Contributor

PoignardAzur commented Nov 5, 2025

Fixing the relevant crates sounds easy enough. I'll see if I can make progress.

@Voultapher
Copy link
Contributor Author

@PoignardAzur I have some time Friday and had planned on tackling it then, how about we split the work?

PoignardAzur added a commit to PoignardAzur/gcode-rs that referenced this pull request Nov 5, 2025
These uses of assert_eq will be broken by changes to how the prelude handles macros: rust-lang/rust#139493.
PoignardAzur added a commit to PoignardAzur/rust-ipcrypt that referenced this pull request Nov 5, 2025
These uses of assert_eq will be broken by changes to how the prelude handles macros: rust-lang/rust#139493.
@PoignardAzur
Copy link
Contributor

PoignardAzur commented Nov 5, 2025

Well, too late, I've done most of it.

Some notes:

Overall I don't think this PR needs to wait on any of the crates listed in the crater report except maybe threescalers. There is virtually no chance that a crate sees one of its dependencies stop working because of this PR (aside from maybe playdate dependents, but those are on nightly).

@unleashed
Copy link
Contributor

Overall I don't think this PR needs to wait on any of the crates listed in the crater report except maybe threescalers. There is virtually no chance that a crate sees one of its dependencies stop working because of this PR (aside from maybe playdate dependents, but those are on nightly).

threescalers maintainer here. Don't wait on this crate either as all known users either use older toolchains and are very slow to update or point to a recent git revision. I'll be releasing an update this week to crates.io to fix this, but no need to wait.

@Voultapher
Copy link
Contributor Author

@PoignardAzur wonderful, thanks for the help.

@Voultapher
Copy link
Contributor Author

Voultapher commented Nov 7, 2025

@petrochenkov A more detailed change description as discussion basis for the lang team:


This change if merged would change the code injected into user crates to no longer include #[macro_use] on extern crate core and extern crate std. This change is motivated by a near term goal and a longer term goal. The near term goal is to allow a macro to be defined at the std or core crate root but not have it be part of the implicit prelude. Such macros can then be separately promoted to the prelude in a new edition. Specifically this is blocking the stabilization of assert_matches #137487. The longer term goal is to gradually deprecate #[macro_use]. By no longer requiring it for standard library usage, this serves as a step towards that goal. For more information see #53977. Currently, prelude macros have a higher priority than glob-imported macros. This causes a previously silent ambiguity to become an error. A minimal example:

#![no_std]

extern crate std;
use std::prelude::v1::*;

fn xx() {
    panic!(); // resolves to core::panic
}

This resolution is surprising or at least not obvious. It's possible for user code to invoke this ambiguity by defining their own macros with standard library names and glob importing them, e.g. use nom::* importing nom::dbg. In practice this happens rarely based on crater data. The 3 public crates where this was an issue, have been fixed. The ambiguous panic import is more common and affects a non-trivial amount of the public - and likely private - crate ecosystem. To avoid a breaking change, a new future incompatible lint was added ambiguous_panic_imports see #147319. This allows current code to continue compiling, albeit with a new warning. Future editions of Rust make this an error and future versions of Rust can choose to make this error. Technically this is a breaking change, but crater gives us the confidence that the impact will be at worst a new warning for 99+% of public and private crates.


There are some side-effect changes like no longer exporting format_args_nl but I'm not listing them since they aren't relevant to the bigger picture. Please let me know if this is what you had in mind and works for the intended purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rustdoc-search Area: Rustdoc's search feature I-lang-radar Items that are on lang's radar and will need eventual work or consideration. perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not apply #[macro_use] to implicitly injected extern crate std;, use standard library prelude instead