From 615eaba14b51adb177a2e249354ae7c35d51e403 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 10 Sep 2018 01:54:51 +0300 Subject: Feature gate non-builtin attributes in inner attribute position --- src/libsyntax/ext/expand.rs | 31 +++++++++++++++++++++++-------- src/libsyntax/feature_gate.rs | 3 +++ 2 files changed, 26 insertions(+), 8 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 3bb19121ee3..7b4e1814a33 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1074,6 +1074,21 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { self.collect(kind, InvocationKind::Attr { attr, traits, item }) } + fn find_attr_invoc(&self, attrs: &mut Vec) -> Option { + let attr = attrs.iter() + .position(|a| !attr::is_known(a) && !is_builtin_attr(a)) + .map(|i| attrs.remove(i)); + if let Some(attr) = &attr { + if !self.cx.ecfg.enable_custom_inner_attributes() && + attr.style == ast::AttrStyle::Inner && attr.path != "test" { + emit_feature_err(&self.cx.parse_sess, "custom_inner_attributes", + attr.span, GateIssue::Language, + "non-builtin inner attributes are unstable"); + } + } + attr + } + /// If `item` is an attr invocation, remove and return the macro attribute and derive traits. fn classify_item(&mut self, mut item: T) -> (Option, Vec, T) where T: HasAttrs, @@ -1087,7 +1102,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { return attrs; } - attr = find_attr_invoc(&mut attrs); + attr = self.find_attr_invoc(&mut attrs); traits = collect_derives(&mut self.cx, &mut attrs); attrs }); @@ -1108,7 +1123,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { return attrs; } - attr = find_attr_invoc(&mut attrs); + attr = self.find_attr_invoc(&mut attrs); attrs }); @@ -1145,12 +1160,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { } } -pub fn find_attr_invoc(attrs: &mut Vec) -> Option { - attrs.iter() - .position(|a| !attr::is_known(a) && !is_builtin_attr(a)) - .map(|i| attrs.remove(i)) -} - impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { fn fold_expr(&mut self, expr: P) -> P { let mut expr = self.cfg.configure_expr(expr).into_inner(); @@ -1582,6 +1591,12 @@ impl<'feat> ExpansionConfig<'feat> { fn proc_macro_expr = proc_macro_expr, fn proc_macro_non_items = proc_macro_non_items, } + + fn enable_custom_inner_attributes(&self) -> bool { + self.features.map_or(false, |features| { + features.custom_inner_attributes || features.custom_attribute || features.rustc_attrs + }) + } } // A Marker adds the given mark to the syntax context. diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index e912482cf58..3e880bb6cf6 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -518,6 +518,9 @@ declare_features! ( // #![test_runner] // #[test_case] (active, custom_test_frameworks, "1.30.0", Some(50297), None), + + // Non-builtin attributes in inner attribute position + (active, custom_inner_attributes, "1.30.0", Some(38356), None), ); declare_features! ( -- cgit 1.4.1-3-g733a5