Skip to content

Commit 551244f

Browse files
committed
Auto merge of #54093 - petrochenkov:noinner, r=alexcrichton
Feature gate non-builtin attributes in inner attribute position Closes item 3 from #50911 (comment)
2 parents 3c3e372 + 615eaba commit 551244f

File tree

8 files changed

+48
-12
lines changed

8 files changed

+48
-12
lines changed

src/libsyntax/ext/expand.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,21 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10741074
self.collect(kind, InvocationKind::Attr { attr, traits, item })
10751075
}
10761076

1077+
fn find_attr_invoc(&self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
1078+
let attr = attrs.iter()
1079+
.position(|a| !attr::is_known(a) && !is_builtin_attr(a))
1080+
.map(|i| attrs.remove(i));
1081+
if let Some(attr) = &attr {
1082+
if !self.cx.ecfg.enable_custom_inner_attributes() &&
1083+
attr.style == ast::AttrStyle::Inner && attr.path != "test" {
1084+
emit_feature_err(&self.cx.parse_sess, "custom_inner_attributes",
1085+
attr.span, GateIssue::Language,
1086+
"non-builtin inner attributes are unstable");
1087+
}
1088+
}
1089+
attr
1090+
}
1091+
10771092
/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
10781093
fn classify_item<T>(&mut self, mut item: T) -> (Option<ast::Attribute>, Vec<Path>, T)
10791094
where T: HasAttrs,
@@ -1087,7 +1102,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10871102
return attrs;
10881103
}
10891104

1090-
attr = find_attr_invoc(&mut attrs);
1105+
attr = self.find_attr_invoc(&mut attrs);
10911106
traits = collect_derives(&mut self.cx, &mut attrs);
10921107
attrs
10931108
});
@@ -1108,7 +1123,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
11081123
return attrs;
11091124
}
11101125

1111-
attr = find_attr_invoc(&mut attrs);
1126+
attr = self.find_attr_invoc(&mut attrs);
11121127
attrs
11131128
});
11141129

@@ -1145,12 +1160,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
11451160
}
11461161
}
11471162

1148-
pub fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
1149-
attrs.iter()
1150-
.position(|a| !attr::is_known(a) && !is_builtin_attr(a))
1151-
.map(|i| attrs.remove(i))
1152-
}
1153-
11541163
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
11551164
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
11561165
let mut expr = self.cfg.configure_expr(expr).into_inner();
@@ -1582,6 +1591,12 @@ impl<'feat> ExpansionConfig<'feat> {
15821591
fn proc_macro_expr = proc_macro_expr,
15831592
fn proc_macro_non_items = proc_macro_non_items,
15841593
}
1594+
1595+
fn enable_custom_inner_attributes(&self) -> bool {
1596+
self.features.map_or(false, |features| {
1597+
features.custom_inner_attributes || features.custom_attribute || features.rustc_attrs
1598+
})
1599+
}
15851600
}
15861601

15871602
// A Marker adds the given mark to the syntax context.

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ declare_features! (
518518
// #![test_runner]
519519
// #[test_case]
520520
(active, custom_test_frameworks, "1.30.0", Some(50297), None),
521+
522+
// Non-builtin attributes in inner attribute position
523+
(active, custom_inner_attributes, "1.30.0", Some(38356), None),
521524
);
522525

523526
declare_features! (

src/test/compile-fail-fulldeps/proc-macro/attribute-with-error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// aux-build:attribute-with-error.rs
1212
// ignore-stage1
1313

14+
#![feature(custom_inner_attributes)]
15+
1416
extern crate attribute_with_error;
1517

1618
use attribute_with_error::foo;

src/test/compile-fail-fulldeps/proc-macro/issue-41211.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// FIXME: https://github.com/rust-lang/rust/issues/41430
1414
// This is a temporary regression test for the ICE reported in #41211
1515

16+
#![feature(custom_inner_attributes)]
17+
1618
#![emit_unchanged]
1719
//~^ ERROR attribute `emit_unchanged` is currently unknown to the compiler
1820
extern crate issue_41211;

src/test/compile-fail-fulldeps/proc-macro/proc-macro-gates.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ extern crate proc_macro_gates as foo;
2222
use foo::*;
2323

2424
fn _test_inner() {
25-
#![a] // OK
25+
#![a] //~ ERROR: non-builtin inner attributes are unstable
2626
}
2727

2828
#[a] //~ ERROR: custom attributes cannot be applied to modules
2929
mod _test2 {}
3030

3131
mod _test2_inner {
3232
#![a] //~ ERROR: custom attributes cannot be applied to modules
33+
//~| ERROR: non-builtin inner attributes are unstable
3334
}
3435

3536
#[a = y] //~ ERROR: must only be followed by a delimiter token

src/test/ui/feature-gate/issue-43106-gating-of-bench.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// See issue-12997-1.rs and issue-12997-2.rs to see how `#[bench]` is
2121
// handled in "weird places" when `--test` is passed.
2222

23+
#![feature(custom_inner_attributes)]
24+
2325
#![bench = "4100"]
2426

2527
fn main() { }

src/test/ui/span/issue-36530.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// gate-test-custom_inner_attributes
12+
1113
#[foo] //~ ERROR is currently unknown to the compiler
1214
mod foo {
1315
#![foo] //~ ERROR is currently unknown to the compiler
16+
//~| ERROR non-builtin inner attributes are unstable
1417
}

src/test/ui/span/issue-36530.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
error[E0658]: The attribute `foo` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
2-
--> $DIR/issue-36530.rs:11:3
2+
--> $DIR/issue-36530.rs:13:3
33
|
44
LL | #[foo] //~ ERROR is currently unknown to the compiler
55
| ^^^
66
|
77
= help: add #![feature(custom_attribute)] to the crate attributes to enable
88

9+
error[E0658]: non-builtin inner attributes are unstable (see issue #38356)
10+
--> $DIR/issue-36530.rs:15:5
11+
|
12+
LL | #![foo] //~ ERROR is currently unknown to the compiler
13+
| ^^^^^^^
14+
|
15+
= help: add #![feature(custom_inner_attributes)] to the crate attributes to enable
16+
917
error[E0658]: The attribute `foo` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
10-
--> $DIR/issue-36530.rs:13:8
18+
--> $DIR/issue-36530.rs:15:8
1119
|
1220
LL | #![foo] //~ ERROR is currently unknown to the compiler
1321
| ^^^
1422
|
1523
= help: add #![feature(custom_attribute)] to the crate attributes to enable
1624

17-
error: aborting due to 2 previous errors
25+
error: aborting due to 3 previous errors
1826

1927
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)