From bbbb85a4ec029e961c01fa95725ee065621c07dc Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Fri, 2 Jan 2015 22:21:28 -0800 Subject: [PATCH] Forbid '#[macro_use] extern crate' outside the crate root --- src/librustc/plugin/load.rs | 16 +++++++++++++++ src/test/compile-fail/lint-stability.rs | 7 ++++--- .../macro-crate-nonterminal-non-root.rs | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/macro-crate-nonterminal-non-root.rs diff --git a/src/librustc/plugin/load.rs b/src/librustc/plugin/load.rs index 64a5a02b34b47..44a223954858a 100644 --- a/src/librustc/plugin/load.rs +++ b/src/librustc/plugin/load.rs @@ -20,6 +20,7 @@ use std::dynamic_lib::DynamicLibrary; use std::collections::HashSet; use syntax::ast; use syntax::attr; +use syntax::codemap::Span; use syntax::parse::token; use syntax::ptr::P; use syntax::visit; @@ -45,6 +46,7 @@ pub struct Plugins { struct PluginLoader<'a> { sess: &'a Session, + span_whitelist: HashSet, reader: CrateReader<'a>, plugins: Plugins, } @@ -54,6 +56,7 @@ impl<'a> PluginLoader<'a> { PluginLoader { sess: sess, reader: CrateReader::new(sess), + span_whitelist: HashSet::new(), plugins: Plugins { macros: vec!(), registrars: vec!(), @@ -66,6 +69,14 @@ impl<'a> PluginLoader<'a> { pub fn load_plugins(sess: &Session, krate: &ast::Crate, addl_plugins: Option) -> Plugins { let mut loader = PluginLoader::new(sess); + + // We need to error on `#[macro_use] extern crate` when it isn't at the + // crate root, because `$crate` won't work properly. Identify these by + // spans, because the crate map isn't set up yet. + for vi in krate.module.view_items.iter() { + loader.span_whitelist.insert(vi.span); + } + visit::walk_crate(&mut loader, krate); let mut plugins = loader.plugins; @@ -158,6 +169,11 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> { }; let load_registrar = plugin_attr.is_some(); + if load_macros && !self.span_whitelist.contains(&vi.span) { + self.sess.span_err(vi.span, "an `extern crate` loading macros must be at \ + the crate root"); + } + if load_macros || load_registrar { let pmd = self.reader.read_plugin_metadata(vi); if load_macros { diff --git a/src/test/compile-fail/lint-stability.rs b/src/test/compile-fail/lint-stability.rs index 87bdb15f6edf7..63dc8d692bacd 100644 --- a/src/test/compile-fail/lint-stability.rs +++ b/src/test/compile-fail/lint-stability.rs @@ -19,13 +19,14 @@ #![deny(experimental)] #![allow(dead_code)] +#[macro_use] +extern crate lint_stability; //~ ERROR: use of unmarked item + mod cross_crate { extern crate stability_cfg1; extern crate stability_cfg2; //~ ERROR: use of experimental item - #[macro_use] - extern crate lint_stability; //~ ERROR: use of unmarked item - use self::lint_stability::*; + use lint_stability::*; fn test() { let foo = MethodTester; diff --git a/src/test/compile-fail/macro-crate-nonterminal-non-root.rs b/src/test/compile-fail/macro-crate-nonterminal-non-root.rs new file mode 100644 index 0000000000000..67aaf05c3101b --- /dev/null +++ b/src/test/compile-fail/macro-crate-nonterminal-non-root.rs @@ -0,0 +1,20 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:macro_crate_nonterminal.rs +// ignore-stage1 + +mod foo { + #[macro_use] + extern crate macro_crate_nonterminal; //~ ERROR must be at the crate root +} + +fn main() { +}