Skip to content

Commit c02d577

Browse files
committed
Improve the warning cycle for use $crate;.
1 parent 6fe2371 commit c02d577

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/librustc_resolve/build_reduced_graph.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> Resolver<'a> {
143143
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
144144

145145
match view_path.node {
146-
ViewPathSimple(binding, ref full_path) => {
146+
ViewPathSimple(mut binding, ref full_path) => {
147147
let mut source = full_path.segments.last().unwrap().identifier;
148148
let source_name = source.name;
149149
if source_name == "mod" || source_name == "self" {
@@ -157,6 +157,9 @@ impl<'a> Resolver<'a> {
157157
ModuleKind::Block(..) => unreachable!(),
158158
};
159159
source.name = crate_name;
160+
if binding.name == "$crate" {
161+
binding.name = crate_name;
162+
}
160163

161164
self.session.struct_span_warn(item.span, "`$crate` may not be imported")
162165
.note("`use $crate;` was erroneously allowed and \

src/test/compile-fail/auxiliary/import_crate_var.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
pub fn f() {}
12+
1113
#[macro_export]
12-
macro_rules! m { () => { use $crate; } }
14+
macro_rules! m { () => {
15+
use $crate;
16+
import_crate_var::f();
17+
} }

src/test/compile-fail/import-crate-var.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
// aux-build:import_crate_var.rs
1212
// error-pattern: `$crate` may not be imported
1313
// error-pattern: `use $crate;` was erroneously allowed and will become a hard error
14+
// error-pattern: compilation successful
1415

1516
#![feature(rustc_attrs)]
1617

1718
#[macro_use] extern crate import_crate_var;
18-
m!();
1919

2020
#[rustc_error]
21-
fn main() {}
21+
fn main() {
22+
m!();
23+
}

0 commit comments

Comments
 (0)