diff options
| author | bors <bors@rust-lang.org> | 2017-06-03 15:40:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-06-03 15:40:34 +0000 |
| commit | a8dc246dc73cddbad1dee4a8696f8ccc06f12a79 (patch) | |
| tree | 30a34f42872208ad7ddeb3347a62a431b59e66f2 | |
| parent | 4225019750f437c8c247a2682f01abe5ada69c46 (diff) | |
| parent | 03876ec1b12ed0080fe3a29a8161229cc5fd3c86 (diff) | |
| download | rust-a8dc246dc73cddbad1dee4a8696f8ccc06f12a79.tar.gz rust-a8dc246dc73cddbad1dee4a8696f8ccc06f12a79.zip | |
Auto merge of #42334 - est31:master, r=jseyfried
Extend the unused macro lint to macros 2.0 Extends the unused macro lint (added in PR #41907) to macros 2.0 (added in PR #40847). r? @jseyfried
| -rw-r--r-- | src/librustc_resolve/lib.rs | 2 | ||||
| -rw-r--r-- | src/librustc_resolve/macros.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/ext/base.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/feature-gate-decl_macro.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/unused-macro-rules.rs | 39 | ||||
| -rw-r--r-- | src/test/compile-fail/unused-macro.rs | 25 |
8 files changed, 64 insertions, 19 deletions
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index a40c191f7bd..be304b8f296 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1204,7 +1204,7 @@ pub struct Resolver<'a> { pub found_unresolved_macro: bool, // List of crate local macros that we need to warn about as being unused. - // Right now this only includes macro_rules! macros. + // Right now this only includes macro_rules! macros, and macros 2.0. unused_macros: FxHashSet<DefId>, // Maps the `Mark` of an expansion to its containing module or block. diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 9a37df76232..a950a9a23e4 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -316,6 +316,7 @@ impl<'a> base::Resolver for Resolver<'a> { for did in self.unused_macros.iter() { let id_span = match *self.macro_map[did] { SyntaxExtension::NormalTT(_, isp, _) => isp, + SyntaxExtension::DeclMacro(.., osp) => osp, _ => None, }; if let Some((id, span)) = id_span { @@ -735,6 +736,9 @@ impl<'a> Resolver<'a> { let module = self.current_module; let def = Def::Macro(def_id, MacroKind::Bang); let vis = self.resolve_visibility(&item.vis); + if vis != ty::Visibility::Public { + self.unused_macros.insert(def_id); + } self.define(module, ident, MacroNS, (def, vis, item.span, expansion)); } } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 71dc81c3759..8089fad5f36 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -552,7 +552,9 @@ pub enum SyntaxExtension { BuiltinDerive(BuiltinDeriveFn), /// A declarative macro, e.g. `macro m() {}`. - DeclMacro(Box<TTMacroExpander>, Option<Span> /* definition site span */), + /// + /// The second element is the definition site span. + DeclMacro(Box<TTMacroExpander>, Option<(ast::NodeId, Span)>), } impl SyntaxExtension { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index be077b48111..c91c77719e6 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -472,8 +472,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { let marked_tts = noop_fold_tts(mac.node.stream(), &mut Marker(mark)); let opt_expanded = match *ext { - SyntaxExtension::DeclMacro(ref expand, def_site_span) => { - if let Err(msg) = validate_and_set_expn_info(def_site_span, false) { + SyntaxExtension::DeclMacro(ref expand, def_span) => { + if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s), + false) { self.cx.span_err(path.span, &msg); return kind.dummy(span); } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 15042e529e5..0472a94e0ce 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -266,7 +266,7 @@ pub fn compile(sess: &ParseSess, features: &RefCell<Features>, def: &ast::Item) let allow_internal_unstable = attr::contains_name(&def.attrs, "allow_internal_unstable"); NormalTT(exp, Some((def.id, def.span)), allow_internal_unstable) } else { - SyntaxExtension::DeclMacro(exp, Some(def.span)) + SyntaxExtension::DeclMacro(exp, Some((def.id, def.span))) } } diff --git a/src/test/compile-fail/feature-gate-decl_macro.rs b/src/test/compile-fail/feature-gate-decl_macro.rs index af7d5fec071..c5c83977c77 100644 --- a/src/test/compile-fail/feature-gate-decl_macro.rs +++ b/src/test/compile-fail/feature-gate-decl_macro.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![allow(unused_macros)] + macro m() {} //~ ERROR `macro` is experimental (see issue #39412) //~| HELP add #![feature(decl_macro)] to the crate attributes to enable diff --git a/src/test/compile-fail/unused-macro-rules.rs b/src/test/compile-fail/unused-macro-rules.rs new file mode 100644 index 00000000000..5e401c09bda --- /dev/null +++ b/src/test/compile-fail/unused-macro-rules.rs @@ -0,0 +1,39 @@ +// Copyright 2017 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(unused_macros)] + +// Most simple case +macro_rules! unused { //~ ERROR: unused macro definition + () => {}; +} + +// Test macros created by macros +macro_rules! create_macro { + () => { + macro_rules! m { //~ ERROR: unused macro definition + () => {}; + } + }; +} +create_macro!(); + +#[allow(unused_macros)] +mod bar { + // Test that putting the #[deny] close to the macro's definition + // works. + + #[deny(unused_macros)] + macro_rules! unused { //~ ERROR: unused macro definition + () => {}; + } +} + +fn main() {} diff --git a/src/test/compile-fail/unused-macro.rs b/src/test/compile-fail/unused-macro.rs index 5e401c09bda..9e32f01724d 100644 --- a/src/test/compile-fail/unused-macro.rs +++ b/src/test/compile-fail/unused-macro.rs @@ -8,31 +8,28 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(decl_macro)] #![deny(unused_macros)] // Most simple case -macro_rules! unused { //~ ERROR: unused macro definition - () => {}; +macro unused { //~ ERROR: unused macro definition + () => {} } -// Test macros created by macros -macro_rules! create_macro { - () => { - macro_rules! m { //~ ERROR: unused macro definition - () => {}; - } - }; -} -create_macro!(); - #[allow(unused_macros)] mod bar { // Test that putting the #[deny] close to the macro's definition // works. #[deny(unused_macros)] - macro_rules! unused { //~ ERROR: unused macro definition - () => {}; + macro unused { //~ ERROR: unused macro definition + () => {} + } +} + +mod boo { + pub(crate) macro unused { //~ ERROR: unused macro definition + () => {} } } |
