Skip to content

Commit 413a975

Browse files
committed
Auto merge of #40039 - abonander:issue_40001, r=jseyfried
Don't assume plugin-whitelisted attributes are proc macro attributes closes #40001
2 parents bfe4597 + dac25e2 commit 413a975

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Diff for: src/librustc_resolve/macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ impl<'a> base::Resolver for Resolver<'a> {
181181
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
182182
-> Option<ast::Attribute> {
183183
for i in 0..attrs.len() {
184+
if self.session.plugin_attributes.borrow().iter()
185+
.any(|&(ref attr_nm, _)| attrs[i].name() == &**attr_nm) {
186+
attr::mark_known(&attrs[i]);
187+
}
188+
184189
match self.builtin_macros.get(&attrs[i].name()).cloned() {
185190
Some(binding) => match *binding.get_macro(self) {
186191
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
11+
#![crate_type = "dylib"]
12+
13+
#[macro_use]
14+
extern crate rustc;
15+
extern crate rustc_plugin;
16+
extern crate syntax;
17+
18+
use rustc_plugin::Registry;
19+
use syntax::ext::base::*;
20+
use syntax::feature_gate::AttributeType::Whitelisted;
21+
use syntax::symbol::Symbol;
22+
23+
use rustc::hir;
24+
use rustc::hir::intravisit;
25+
use rustc::hir::map as hir_map;
26+
use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext};
27+
use rustc::ty;
28+
use syntax::{ast, codemap};
29+
30+
#[plugin_registrar]
31+
pub fn plugin_registrar(reg: &mut Registry) {
32+
reg.register_late_lint_pass(box MissingWhitelistedAttrPass);
33+
reg.register_attribute("whitelisted_attr".to_string(), Whitelisted);
34+
}
35+
36+
declare_lint!(MISSING_WHITELISTED_ATTR, Deny,
37+
"Checks for missing `whitelisted_attr` attribute");
38+
39+
struct MissingWhitelistedAttrPass;
40+
41+
impl LintPass for MissingWhitelistedAttrPass {
42+
fn get_lints(&self) -> LintArray {
43+
lint_array!(MISSING_WHITELISTED_ATTR)
44+
}
45+
}
46+
47+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingWhitelistedAttrPass {
48+
fn check_fn(&mut self,
49+
cx: &LateContext<'a, 'tcx>,
50+
_: intravisit::FnKind<'tcx>,
51+
_: &'tcx hir::FnDecl,
52+
_: &'tcx hir::Body,
53+
span: codemap::Span,
54+
id: ast::NodeId) {
55+
56+
let item = match cx.tcx.hir.get(id) {
57+
hir_map::Node::NodeItem(item) => item,
58+
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
59+
};
60+
61+
if !item.attrs.iter().any(|a| a.check_name("whitelisted_attr")) {
62+
cx.span_lint(MISSING_WHITELISTED_ATTR, span,
63+
"Missing 'whitelited_attr' attribute");
64+
}
65+
}
66+
}

Diff for: src/test/run-pass-fulldeps/proc-macro/issue-40001.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-40001-plugin.rs
12+
13+
#![feature(proc_macro, plugin)]
14+
#![plugin(issue_40001_plugin)]
15+
16+
#[whitelisted_attr]
17+
fn main() {}

0 commit comments

Comments
 (0)