]> git.lizzy.rs Git - rust.git/commitdiff
Don't assume plugin-whitelisted attributes are proc macro attributes
authorAustin Bonander <austin.bonander@gmail.com>
Wed, 22 Feb 2017 19:15:12 +0000 (11:15 -0800)
committerAustin Bonander <austin.bonander@gmail.com>
Wed, 22 Feb 2017 23:43:03 +0000 (15:43 -0800)
closes #40001

src/librustc_resolve/macros.rs
src/test/run-pass-fulldeps/proc-macro/auxiliary/issue-40001-plugin.rs [new file with mode: 0644]
src/test/run-pass-fulldeps/proc-macro/issue-40001.rs [new file with mode: 0644]

index 8d563d20549c3b8e78108d75231941ce3b54e358..aba2de6b3a2b6106b78b04dff80223ecf5edb236 100644 (file)
@@ -181,6 +181,11 @@ fn resolve_imports(&mut self) {
     fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
                               -> Option<ast::Attribute> {
         for i in 0..attrs.len() {
+            if self.session.plugin_attributes.borrow().iter()
+                    .any(|&(ref attr_nm, _)| attrs[i].name() == &**attr_nm) {
+                attr::mark_known(&attrs[i]);
+            }
+
             match self.builtin_macros.get(&attrs[i].name()).cloned() {
                 Some(binding) => match *binding.get_macro(self) {
                     MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/issue-40001-plugin.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/issue-40001-plugin.rs
new file mode 100644 (file)
index 0000000..cf1a631
--- /dev/null
@@ -0,0 +1,66 @@
+// Copyright 2016 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.
+#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
+#![crate_type = "dylib"]
+
+#[macro_use]
+extern crate rustc;
+extern crate rustc_plugin;
+extern crate syntax;
+
+use rustc_plugin::Registry;
+use syntax::ext::base::*;
+use syntax::feature_gate::AttributeType::Whitelisted;
+use syntax::symbol::Symbol;
+
+use rustc::hir;
+use rustc::hir::intravisit;
+use rustc::hir::map as hir_map;
+use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext};
+use rustc::ty;
+use syntax::{ast, codemap};
+
+#[plugin_registrar]
+pub fn plugin_registrar(reg: &mut Registry) {
+    reg.register_late_lint_pass(box MissingWhitelistedAttrPass);
+    reg.register_attribute("whitelisted_attr".to_string(), Whitelisted);
+}
+
+declare_lint!(MISSING_WHITELISTED_ATTR, Deny,
+              "Checks for missing `whitelisted_attr` attribute");
+
+struct MissingWhitelistedAttrPass;
+
+impl LintPass for MissingWhitelistedAttrPass {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(MISSING_WHITELISTED_ATTR)
+    }
+}
+
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingWhitelistedAttrPass {
+    fn check_fn(&mut self,
+                cx: &LateContext<'a, 'tcx>,
+                _: intravisit::FnKind<'tcx>,
+                _: &'tcx hir::FnDecl,
+                _: &'tcx hir::Body,
+                span: codemap::Span,
+                id: ast::NodeId) {
+
+        let item = match cx.tcx.hir.get(id) {
+            hir_map::Node::NodeItem(item) => item,
+            _ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
+        };
+
+        if !item.attrs.iter().any(|a| a.check_name("whitelisted_attr")) {
+            cx.span_lint(MISSING_WHITELISTED_ATTR, span,
+                         "Missing 'whitelited_attr' attribute");
+        }
+    }
+}
diff --git a/src/test/run-pass-fulldeps/proc-macro/issue-40001.rs b/src/test/run-pass-fulldeps/proc-macro/issue-40001.rs
new file mode 100644 (file)
index 0000000..54e84b7
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2016 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.
+
+// aux-build:issue-40001-plugin.rs
+
+#![feature(proc_macro, plugin)]
+#![plugin(issue_40001_plugin)]
+
+#[whitelisted_attr]
+fn main() {}