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] should only apply to the current module, not to all children #14969

Closed
Kimundi opened this issue Jun 17, 2014 · 6 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`)

Comments

@Kimundi
Copy link
Member

Kimundi commented Jun 17, 2014

Currently, the #![no_implicit_prelude] attribute can be used to disable the libstd prelude imports for a module. However, it seems to disable them for all submodules as well, which makes no sense seeing how the prelude is just a bunch of use statements, and those don't propagate into child modules.

Example:

fn a() {
    let _: Vec<usize> = vec![];
}

mod foo {
    #![no_implicit_prelude]

    fn b() {
        let _: Vec<usize> = vec![];
    }

    mod bar {
        fn c() {
            let _: Vec<usize> = vec![];
        }   
    }
}

fn main() {}

... which results in an error for the bar module as well:

<anon>:9:16: 9:25 error: use of undeclared type name `Vec`
<anon>:9         let _: Vec<usize> = vec![];
                        ^~~~~~~~~
<anon>:14:20: 14:29 error: use of undeclared type name `Vec`
<anon>:14             let _: Vec<usize> = vec![];
                             ^~~~~~~~~

As a fix, #![no_implicit_prelude] should be changed to only apply to the module it is applied on.

@huonw
Copy link
Member

huonw commented Jun 17, 2014

I originally implemented it with these semantics because it seemed reasonable/common* for someone to not want the prelude in their entire crate (e.g. if they're using a custom prelude), and having to type #[no_implicit_prelude] in each and every module seems annoying.

However, I have no evidence for this claim.

* Common among uses of #[no_implicit_prelude] anyway.

@Kimundi
Copy link
Member Author

Kimundi commented Jun 17, 2014

@huonw Well, as a contra point, I just had to manually import prelude items in like 5 submodules because of that , so I personally think it should be changed :)

I'd understand it the attribute where a crate-root only attribute but the current semantic is neither here nor there imo.

@Kimundi
Copy link
Member Author

Kimundi commented Jun 17, 2014

About avoiding writing that attribute all the time: Now that macros can expand to multible items, something like prelude!{foo::bar} that expands to #![no_implicit_prelude] use foo::bar::*; should be possible, right?

@steveklabnik steveklabnik added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Jan 23, 2015
@steveklabnik
Copy link
Member

Triage: this still works this way today.

@Nemo157
Copy link
Member

Nemo157 commented Feb 25, 2016

I agree that disabling for the whole crate seems like it would be the common case (as a contra contra point it's how I want to use it 😛).

If it does get changed then it would be nice to be able to apply it as a crate attribute as well that disables the prelude throughout the crate (although, I guess that would mean not being able to apply it to just the root module, maybe it would need to be a different name for the crate only attribute?).

@Mark-Simulacrum
Copy link
Member

From RFC 501:

Make name and behavior of the #![no_std] and #![no_implicit_prelude] attributes consistent by renaming the latter to #![no_prelude] and having it only apply to the current module.

Once #20561 is implemented, this will no longer be an issue, since the #![no_prelude] attribute will apply only to the current module. As such, this isn't tracking anything more than that is, so I'm going to close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`)
Projects
None yet
Development

No branches or pull requests

5 participants