From 5ffb7db42307226503242dececdcfd7c52063d7c Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 17 Feb 2015 00:14:51 +0530 Subject: [PATCH] Add `Gated` attribute type --- src/librustc/lint/builtin.rs | 8 +++-- src/libsyntax/feature_gate.rs | 56 +++++++++++++++++------------------ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index b17d35abcf6..b1eb4f01d84 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -642,8 +642,12 @@ impl LintPass for UnusedAttributes { fn check_attribute(&mut self, cx: &Context, attr: &ast::Attribute) { for &(ref name, ty) in KNOWN_ATTRIBUTES { - if ty == AttributeType::Whitelisted && attr.check_name(name) { - break; + match ty { + AttributeType::Whitelisted + | AttributeType::Gated(_, _) if attr.check_name(name) => { + break; + }, + _ => () } } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 62eb5badb77..6ef20a6a62b 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -166,20 +166,17 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[ ("macro_reexport", Normal), ("macro_use", Normal), - ("plugin", Normal), ("macro_export", Normal), ("plugin_registrar", Normal), ("cfg", Normal), ("main", Normal), - ("lang", Normal), ("start", Normal), ("test", Normal), ("bench", Normal), ("simd", Normal), ("repr", Normal), ("path", Normal), - ("staged_api", Normal), ("abi", Normal), ("rustc_move_fragments", Normal), ("rustc_variance", Normal), @@ -195,6 +192,17 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[ ("link_args", Normal), ("macro_escape", Normal), + + ("staged_api", Gated("staged_api", + "staged_api is for use by rustc only")), + ("plugin", Gated("plugin", + "compiler plugins are experimental \ + and possibly buggy")), + ("no_std", Gated("no_std", + "no_std is experimental")), + ("lang", Gated("lang_items", + "language items are subject to change")), + // FIXME: #14408 whitelist docs since rustdoc looks at them ("doc", Whitelisted), @@ -242,7 +250,6 @@ pub static KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[ ("feature", CrateLevel), ("no_start", CrateLevel), ("no_main", CrateLevel), - ("no_std", CrateLevel), ("no_builtins", CrateLevel), ("recursion_limit", CrateLevel), ]; @@ -258,6 +265,10 @@ pub enum AttributeType { /// will be ignored by the unused_attribute lint Whitelisted, + /// Is gated by a given feature gate and reason + /// These get whitelisted too + Gated(&'static str, &'static str), + /// Builtin attribute that is only allowed at the crate level CrateLevel, } @@ -573,33 +584,22 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { } fn visit_attribute(&mut self, attr: &ast::Attribute) { - match &*attr.name() { - "staged_api" => self.gate_feature("staged_api", attr.span, - "staged_api is for use by rustc only"), - "plugin" => self.gate_feature("plugin", attr.span, - "compiler plugins are experimental \ - and possibly buggy"), - "no_std" => self.gate_feature("no_std", attr.span, - "no_std is experimental"), - "unsafe_no_drop_flag" => self.gate_feature("unsafe_no_drop_flag", attr.span, - "unsafe_no_drop_flag has unstable \ - semantics and may be removed \ - in the future"), - "lang" => self.gate_feature("lang_items", - attr.span, - "language items are subject to change"), - name => { - // Custom attribute check - if KNOWN_ATTRIBUTES.iter().all(|&(n, _)| n != name) { - self.gate_feature("custom_attribute", attr.span, - format!("The attribute `{}` is currently \ - unknown to the the compiler and \ - may have meaning \ - added to it in the future", - attr.name()).as_slice()); + let name = &*attr.name(); + for &(n, ty) in KNOWN_ATTRIBUTES { + if n == name { + if let Gated(gate, desc) = ty { + self.gate_feature(gate, attr.span, desc); } + return; } + } + self.gate_feature("custom_attribute", attr.span, + format!("The attribute `{}` is currently \ + unknown to the the compiler and \ + may have meaning \ + added to it in the future", + name).as_slice()); } fn visit_pat(&mut self, pattern: &ast::Pat) {