diff options
| author | Keegan McAllister <kmcallister@mozilla.com> | 2015-01-02 22:21:28 -0800 |
|---|---|---|
| committer | Keegan McAllister <kmcallister@mozilla.com> | 2015-01-05 18:21:14 -0800 |
| commit | bbbb85a4ec029e961c01fa95725ee065621c07dc (patch) | |
| tree | 161bd7dced7c2d118a55c95f2b9945b8b311429f /src/librustc/plugin | |
| parent | c2e26972e307a2e82b9ff7a5345a5bff47a99501 (diff) | |
| download | rust-bbbb85a4ec029e961c01fa95725ee065621c07dc.tar.gz rust-bbbb85a4ec029e961c01fa95725ee065621c07dc.zip | |
Forbid '#[macro_use] extern crate' outside the crate root
Diffstat (limited to 'src/librustc/plugin')
| -rw-r--r-- | src/librustc/plugin/load.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/librustc/plugin/load.rs b/src/librustc/plugin/load.rs index 64a5a02b34b..44a22395485 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<Span>, 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>) -> 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 { |
