Skip to content

Commit 5ffb7db

Browse files
committed
Add Gated attribute type
1 parent d5c3194 commit 5ffb7db

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/librustc/lint/builtin.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,12 @@ impl LintPass for UnusedAttributes {
642642

643643
fn check_attribute(&mut self, cx: &Context, attr: &ast::Attribute) {
644644
for &(ref name, ty) in KNOWN_ATTRIBUTES {
645-
if ty == AttributeType::Whitelisted && attr.check_name(name) {
646-
break;
645+
match ty {
646+
AttributeType::Whitelisted
647+
| AttributeType::Gated(_, _) if attr.check_name(name) => {
648+
break;
649+
},
650+
_ => ()
647651
}
648652
}
649653

src/libsyntax/feature_gate.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,17 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[
166166

167167
("macro_reexport", Normal),
168168
("macro_use", Normal),
169-
("plugin", Normal),
170169
("macro_export", Normal),
171170
("plugin_registrar", Normal),
172171

173172
("cfg", Normal),
174173
("main", Normal),
175-
("lang", Normal),
176174
("start", Normal),
177175
("test", Normal),
178176
("bench", Normal),
179177
("simd", Normal),
180178
("repr", Normal),
181179
("path", Normal),
182-
("staged_api", Normal),
183180
("abi", Normal),
184181
("rustc_move_fragments", Normal),
185182
("rustc_variance", Normal),
@@ -195,6 +192,17 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[
195192
("link_args", Normal),
196193
("macro_escape", Normal),
197194

195+
196+
("staged_api", Gated("staged_api",
197+
"staged_api is for use by rustc only")),
198+
("plugin", Gated("plugin",
199+
"compiler plugins are experimental \
200+
and possibly buggy")),
201+
("no_std", Gated("no_std",
202+
"no_std is experimental")),
203+
("lang", Gated("lang_items",
204+
"language items are subject to change")),
205+
198206
// FIXME: #14408 whitelist docs since rustdoc looks at them
199207
("doc", Whitelisted),
200208

@@ -242,7 +250,6 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[
242250
("feature", CrateLevel),
243251
("no_start", CrateLevel),
244252
("no_main", CrateLevel),
245-
("no_std", CrateLevel),
246253
("no_builtins", CrateLevel),
247254
("recursion_limit", CrateLevel),
248255
];
@@ -258,6 +265,10 @@ pub enum AttributeType {
258265
/// will be ignored by the unused_attribute lint
259266
Whitelisted,
260267

268+
/// Is gated by a given feature gate and reason
269+
/// These get whitelisted too
270+
Gated(&'static str, &'static str),
271+
261272
/// Builtin attribute that is only allowed at the crate level
262273
CrateLevel,
263274
}
@@ -573,33 +584,22 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
573584
}
574585

575586
fn visit_attribute(&mut self, attr: &ast::Attribute) {
576-
match &*attr.name() {
577-
"staged_api" => self.gate_feature("staged_api", attr.span,
578-
"staged_api is for use by rustc only"),
579-
"plugin" => self.gate_feature("plugin", attr.span,
580-
"compiler plugins are experimental \
581-
and possibly buggy"),
582-
"no_std" => self.gate_feature("no_std", attr.span,
583-
"no_std is experimental"),
584-
"unsafe_no_drop_flag" => self.gate_feature("unsafe_no_drop_flag", attr.span,
585-
"unsafe_no_drop_flag has unstable \
586-
semantics and may be removed \
587-
in the future"),
588-
"lang" => self.gate_feature("lang_items",
589-
attr.span,
590-
"language items are subject to change"),
591-
name => {
592-
// Custom attribute check
593-
if KNOWN_ATTRIBUTES.iter().all(|&(n, _)| n != name) {
594-
self.gate_feature("custom_attribute", attr.span,
595-
format!("The attribute `{}` is currently \
596-
unknown to the the compiler and \
597-
may have meaning \
598-
added to it in the future",
599-
attr.name()).as_slice());
587+
let name = &*attr.name();
588+
for &(n, ty) in KNOWN_ATTRIBUTES {
589+
if n == name {
590+
if let Gated(gate, desc) = ty {
591+
self.gate_feature(gate, attr.span, desc);
600592
}
593+
return;
601594
}
595+
602596
}
597+
self.gate_feature("custom_attribute", attr.span,
598+
format!("The attribute `{}` is currently \
599+
unknown to the the compiler and \
600+
may have meaning \
601+
added to it in the future",
602+
name).as_slice());
603603
}
604604

605605
fn visit_pat(&mut self, pattern: &ast::Pat) {

0 commit comments

Comments
 (0)