Skip to content

Commit

Permalink
Improve the warning cycle for use $crate;.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Jan 15, 2017
1 parent 6fe2371 commit c02d577
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -143,7 +143,7 @@ impl<'a> Resolver<'a> {
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");

match view_path.node {
ViewPathSimple(binding, ref full_path) => {
ViewPathSimple(mut binding, ref full_path) => {
let mut source = full_path.segments.last().unwrap().identifier;
let source_name = source.name;
if source_name == "mod" || source_name == "self" {
Expand All @@ -157,6 +157,9 @@ impl<'a> Resolver<'a> {
ModuleKind::Block(..) => unreachable!(),
};
source.name = crate_name;
if binding.name == "$crate" {
binding.name = crate_name;
}

self.session.struct_span_warn(item.span, "`$crate` may not be imported")
.note("`use $crate;` was erroneously allowed and \
Expand Down
7 changes: 6 additions & 1 deletion src/test/compile-fail/auxiliary/import_crate_var.rs
Expand Up @@ -8,5 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn f() {}

#[macro_export]
macro_rules! m { () => { use $crate; } }
macro_rules! m { () => {
use $crate;
import_crate_var::f();
} }
6 changes: 4 additions & 2 deletions src/test/compile-fail/import-crate-var.rs
Expand Up @@ -11,11 +11,13 @@
// aux-build:import_crate_var.rs
// error-pattern: `$crate` may not be imported
// error-pattern: `use $crate;` was erroneously allowed and will become a hard error
// error-pattern: compilation successful

#![feature(rustc_attrs)]

#[macro_use] extern crate import_crate_var;
m!();

#[rustc_error]
fn main() {}
fn main() {
m!();
}

0 comments on commit c02d577

Please sign in to comment.