Skip to content

Commit 75e920f

Browse files
committed
Rollup merge of #55630 - petrochenkov:noprelude, r=Centril
resolve: Filter away macro prelude in modules with `#[no_implicit_prelude]` on 2018 edition This is a tiny thing. For historical reasons macro prelude (macros from `#[macro_use] extern crate ...`, including `extern crate std`) is still available in modules with `#[no_implicit_prelude]`. This PR provides proper isolation and removes those names from scope. `#[no_implicit_prelude]` modules still have built-in types (`u8`), built-in attributes (`#[inline]`) and built-in macros (`env!("PATH")`) in scope. We can introduce some `#[no_implicit_prelude_at_all]` to remove those as well, but that's a separate issue. The change is done only on 2018 edition for backward compatibility. I'm pretty sure this can be done on 2015 as well because `#[no_implicit_prelude]` is rarely used, but I don't want to go through the crater/deprecation process right now, maybe later. cc #53977 r? @ghost
2 parents 17ae507 + 9ed9d6d commit 75e920f

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

Diff for: src/librustc/session/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,10 @@ impl Session {
963963
self.opts.debugging_opts.teach && self.diagnostic().must_teach(code)
964964
}
965965

966+
pub fn rust_2015(&self) -> bool {
967+
self.opts.edition == Edition::Edition2015
968+
}
969+
966970
/// Are we allowed to use features from the Rust 2018 edition?
967971
pub fn rust_2018(&self) -> bool {
968972
self.opts.edition >= Edition::Edition2018

Diff for: src/librustc_resolve/macros.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
663663
binding.map(|binding| (binding, Flags::MODULE, Flags::empty()))
664664
}
665665
WhereToResolve::MacroUsePrelude => {
666-
match self.macro_use_prelude.get(&ident.name).cloned() {
667-
Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
668-
None => Err(Determinacy::Determined),
666+
let mut result = Err(Determinacy::Determined);
667+
if use_prelude || self.session.rust_2015() {
668+
if let Some(binding) = self.macro_use_prelude.get(&ident.name).cloned() {
669+
result = Ok((binding, Flags::PRELUDE, Flags::empty()));
670+
}
669671
}
672+
result
670673
}
671674
WhereToResolve::BuiltinMacros => {
672675
match self.builtin_macros.get(&ident.name).cloned() {
@@ -685,7 +688,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
685688
}
686689
}
687690
WhereToResolve::LegacyPluginHelpers => {
688-
if self.session.plugin_attributes.borrow().iter()
691+
if (use_prelude || self.session.rust_2015()) &&
692+
self.session.plugin_attributes.borrow().iter()
689693
.any(|(name, _)| ident.name == &**name) {
690694
let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
691695
ty::Visibility::Public, ident.span, Mark::root())

Diff for: src/test/ui/hygiene/no_implicit_prelude-2018.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// edition:2018
2+
3+
#[no_implicit_prelude]
4+
mod bar {
5+
fn f() {
6+
::std::print!(""); // OK
7+
print!(); //~ ERROR cannot find macro `print!` in this scope
8+
}
9+
}
10+
11+
fn main() {}

Diff for: src/test/ui/hygiene/no_implicit_prelude-2018.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: cannot find macro `print!` in this scope
2+
--> $DIR/no_implicit_prelude-2018.rs:7:9
3+
|
4+
LL | print!(); //~ ERROR cannot find macro `print!` in this scope
5+
| ^^^^^
6+
|
7+
= help: have you added the `#[macro_use]` on the module/import?
8+
9+
error: aborting due to previous error
10+

Diff for: src/test/ui/hygiene/no_implicit_prelude.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ mod bar {
2121
Vec::new(); //~ ERROR failed to resolve
2222
().clone() //~ ERROR no method named `clone` found
2323
}
24-
fn f() { ::foo::m!(); }
24+
fn f() {
25+
::foo::m!();
26+
println!(); // OK on 2015 edition (at least for now)
27+
}
2528
}
2629

2730
fn main() {}

0 commit comments

Comments
 (0)