Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#![no_implicit_prelude] does not disable macros #138756

Open
kpreid opened this issue Mar 20, 2025 · 3 comments
Open

#![no_implicit_prelude] does not disable macros #138756

kpreid opened this issue Mar 20, 2025 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-maybe-future-edition Something we may consider for a future edition. C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Mar 20, 2025

I tried this code:

// edition 2018, 2021, or 2024
#![no_implicit_prelude]

fn main() {
    assert!(true);
}

I expected to see this happen: Compilation error, according to the language reference:

The no_implicit_prelude attribute may be applied at the crate level or on a module to indicate that it should not automatically bring the standard library prelude, extern prelude, or tool prelude into scope for that module or any of its descendants.

This attribute does not affect the language prelude.

Edition differences: In the 2015 edition, the no_implicit_prelude attribute does not affect the macro_use prelude, and all macros exported from the standard library are still included in the macro_use prelude. Starting in the 2018 edition, it will remove the macro_use prelude.

In particular, both the macro_use prelude and the standard library prelude should not be in scope, and assert! is not a member of the language prelude, so assert! should not be in scope.

Instead, this happened: The code compiles. The macro is still in scope.

Meta

Tested with stable 1.85.1 and nightly 1.87.0-nightly (2025-03-19 1aeb99d).
I do not know whether this ever worked.

Prompted by a discussion in the Rust Community Discord server.

@rustbot label +A-macros

@kpreid kpreid added the C-bug Category: This is a bug. label Mar 20, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Mar 20, 2025
@ehuss
Copy link
Contributor

ehuss commented Mar 20, 2025

There are "built-in" macros, which AFAIK we don't really document. I wonder if it was just an oversight that no_implicit_prelude doesn't affect built-ins?

@petrochenkov
Copy link
Contributor

petrochenkov commented Mar 20, 2025

no_implicit_prelude was there before macro modularization/scoping happened, so initially we just kept backward compatibility when introducing macro preludes.
Looks like something was done on 2015->2018 edition boundary to address this (although I don't remember implementing it), but built-in macros became implemented through library "lang items" (#[rustc_builtin_macro]) even later, so the edition change didn't affect them.

#![no_implicit_prelude] is very rarely used, so there wasn't much incentive to actively change it.

@kpreid
Copy link
Contributor Author

kpreid commented Mar 20, 2025

The use case from the person who originally asked about this behavior is to be able to provide a customized assert with a glob import:

#![no_implicit_prelude]

mod my_prelude {
    macro_rules! assert { () => {}; }
    pub(crate) use assert;
}

mod tests {
    use super::my_prelude::*;
    #[test] 
    fn my_test() { 
        assert!();
    }
}

If the language worked as the Reference says it does, this would use my_prelude::assert in the test function, but it doesn't. I'm not aware of any other way to make this behavior happen.

@lolbinarycat lolbinarycat added T-lang Relevant to the language team, which will review and decide on the PR/issue. A-maybe-future-edition Something we may consider for a future edition. labels Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-maybe-future-edition Something we may consider for a future edition. C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants