Skip to content

Commit 32453db

Browse files
committed
resolve: Fully prohibit shadowing of in-scope names by globs in macro paths
1 parent 2eb83ee commit 32453db

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ impl<'a> Resolver<'a> {
588588
return potential_illegal_shadower;
589589
}
590590
}
591-
if binding.expansion != Mark::root() ||
592-
(binding.is_glob_import() && module.unwrap().def().is_some()) {
591+
if binding.is_glob_import() || binding.expansion != Mark::root() {
593592
potential_illegal_shadower = result;
594593
} else {
595594
return result;

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'a> Resolver<'a> {
233233
// What on earth is this?
234234
// Apparently one more subtle interaction with `resolve_lexical_macro_path_segment`
235235
// that are going to be removed in the next commit.
236-
if restricted_shadowing && module.def().is_some() {
236+
if restricted_shadowing {
237237
return Err(Determined);
238238
}
239239

src/test/ui/imports/glob-shadowing.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod m {
14+
pub macro env($e: expr) { $e }
15+
pub macro fenv() { 0 }
16+
}
17+
18+
mod glob_in_normal_module {
19+
use m::*;
20+
fn check() {
21+
let x = env!("PATH"); //~ ERROR `env` is ambiguous
22+
}
23+
}
24+
25+
mod glob_in_block_module {
26+
fn block() {
27+
use m::*;
28+
fn check() {
29+
let x = env!("PATH"); //~ ERROR `env` is ambiguous
30+
}
31+
}
32+
}
33+
34+
mod glob_shadows_item {
35+
pub macro fenv($e: expr) { $e }
36+
fn block() {
37+
use m::*;
38+
fn check() {
39+
let x = fenv!(); //~ ERROR `fenv` is ambiguous
40+
}
41+
}
42+
}
43+
44+
fn main() {}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0659]: `env` is ambiguous
2+
--> $DIR/glob-shadowing.rs:21:17
3+
|
4+
LL | let x = env!("PATH"); //~ ERROR `env` is ambiguous
5+
| ^^^
6+
|
7+
note: `env` could refer to the name imported here
8+
--> $DIR/glob-shadowing.rs:19:9
9+
|
10+
LL | use m::*;
11+
| ^^^^
12+
= note: `env` is also a builtin macro
13+
= note: consider adding an explicit import of `env` to disambiguate
14+
15+
error[E0659]: `env` is ambiguous
16+
--> $DIR/glob-shadowing.rs:29:21
17+
|
18+
LL | let x = env!("PATH"); //~ ERROR `env` is ambiguous
19+
| ^^^
20+
|
21+
note: `env` could refer to the name imported here
22+
--> $DIR/glob-shadowing.rs:27:13
23+
|
24+
LL | use m::*;
25+
| ^^^^
26+
= note: `env` is also a builtin macro
27+
= note: consider adding an explicit import of `env` to disambiguate
28+
29+
error[E0659]: `fenv` is ambiguous
30+
--> $DIR/glob-shadowing.rs:39:21
31+
|
32+
LL | let x = fenv!(); //~ ERROR `fenv` is ambiguous
33+
| ^^^^
34+
|
35+
note: `fenv` could refer to the name imported here
36+
--> $DIR/glob-shadowing.rs:37:13
37+
|
38+
LL | use m::*;
39+
| ^^^^
40+
note: `fenv` could also refer to the name defined here
41+
--> $DIR/glob-shadowing.rs:35:5
42+
|
43+
LL | pub macro fenv($e: expr) { $e }
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
= note: consider adding an explicit import of `fenv` to disambiguate
46+
47+
error: aborting due to 3 previous errors
48+
49+
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)