Skip to content

Commit dfdebc4

Browse files
petrochenkovpietroalbini
authored andcommitted
resolve: Filter away macro prelude in modules with #[no_implicit_prelude] on 2018 edition
1 parent c63d548 commit dfdebc4

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

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

src/librustc_resolve/macros.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
659659
binding.map(|binding| (binding, Flags::MODULE, Flags::empty()))
660660
}
661661
WhereToResolve::MacroUsePrelude => {
662-
match self.macro_use_prelude.get(&ident.name).cloned() {
663-
Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
664-
None => Err(Determinacy::Determined),
662+
let mut result = Err(Determinacy::Determined);
663+
if use_prelude || self.session.rust_2015() {
664+
if let Some(binding) = self.macro_use_prelude.get(&ident.name).cloned() {
665+
result = Ok((binding, Flags::PRELUDE, Flags::empty()));
666+
}
665667
}
668+
result
666669
}
667670
WhereToResolve::BuiltinMacros => {
668671
match self.builtin_macros.get(&ident.name).cloned() {
@@ -681,7 +684,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
681684
}
682685
}
683686
WhereToResolve::LegacyPluginHelpers => {
684-
if self.session.plugin_attributes.borrow().iter()
687+
if (use_prelude || self.session.rust_2015()) &&
688+
self.session.plugin_attributes.borrow().iter()
685689
.any(|(name, _)| ident.name == &**name) {
686690
let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
687691
ty::Visibility::Public, ident.span, Mark::root())
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() {}
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+

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)