Skip to content

Commit b5246fd

Browse files
Rollup merge of rust-lang#62133 - petrochenkov:norustc, r=eddyb
Feature gate `rustc` attributes harder Fixes rust-lang#62116
2 parents 7bae0b7 + e4e7eb2 commit b5246fd

25 files changed

+251
-191
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#![feature(concat_idents)]
7575
#![feature(const_fn)]
7676
#![feature(const_fn_union)]
77+
#![feature(custom_inner_attributes)]
7778
#![feature(doc_cfg)]
7879
#![feature(doc_spotlight)]
7980
#![feature(extern_types)]

src/librustc_resolve/macros.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ use syntax::ext::base::{MacroKind, SyntaxExtension};
1919
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
2020
use syntax::ext::hygiene::Mark;
2121
use syntax::ext::tt::macro_rules;
22-
use syntax::feature_gate::{
23-
feature_err, is_builtin_attr_name, AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES,
24-
};
22+
use syntax::feature_gate::{feature_err, emit_feature_err, is_builtin_attr_name};
23+
use syntax::feature_gate::{AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES};
2524
use syntax::symbol::{Symbol, kw, sym};
2625
use syntax::visit::Visitor;
2726
use syntax::util::lev_distance::find_best_match_for_name;
@@ -298,12 +297,25 @@ impl<'a> Resolver<'a> {
298297
let res = self.resolve_macro_to_res_inner(path, kind, parent_scope, trace, force);
299298

300299
// Report errors and enforce feature gates for the resolved macro.
300+
let features = self.session.features_untracked();
301301
if res != Err(Determinacy::Undetermined) {
302302
// Do not report duplicated errors on every undetermined resolution.
303303
for segment in &path.segments {
304304
if let Some(args) = &segment.args {
305305
self.session.span_err(args.span(), "generic arguments in macro path");
306306
}
307+
if kind == MacroKind::Attr && !features.rustc_attrs &&
308+
segment.ident.as_str().starts_with("rustc") {
309+
let msg = "attributes starting with `rustc` are \
310+
reserved for use by the `rustc` compiler";
311+
emit_feature_err(
312+
&self.session.parse_sess,
313+
sym::rustc_attrs,
314+
segment.ident.span,
315+
GateIssue::Language,
316+
msg,
317+
);
318+
}
307319
}
308320
}
309321

@@ -320,24 +332,15 @@ impl<'a> Resolver<'a> {
320332
}
321333
Res::NonMacroAttr(attr_kind) => {
322334
if kind == MacroKind::Attr {
323-
let features = self.session.features_untracked();
324335
if attr_kind == NonMacroAttrKind::Custom {
325336
assert!(path.segments.len() == 1);
326-
let name = path.segments[0].ident.as_str();
327-
if name.starts_with("rustc_") {
328-
if !features.rustc_attrs {
329-
let msg = "unless otherwise specified, attributes with the prefix \
330-
`rustc_` are reserved for internal compiler diagnostics";
331-
self.report_unknown_attribute(path.span, &name, msg,
332-
sym::rustc_attrs);
333-
}
334-
} else if !features.custom_attribute {
337+
if !features.custom_attribute {
335338
let msg = format!("The attribute `{}` is currently unknown to the \
336339
compiler and may have meaning added to it in the \
337340
future", path);
338341
self.report_unknown_attribute(
339342
path.span,
340-
&name,
343+
&path.segments[0].ident.as_str(),
341344
&msg,
342345
sym::custom_attribute,
343346
);

src/libsyntax/ext/expand.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1504,9 +1504,7 @@ impl<'feat> ExpansionConfig<'feat> {
15041504
}
15051505

15061506
fn enable_custom_inner_attributes(&self) -> bool {
1507-
self.features.map_or(false, |features| {
1508-
features.custom_inner_attributes || features.custom_attribute || features.rustc_attrs
1509-
})
1507+
self.features.map_or(false, |features| features.custom_inner_attributes)
15101508
}
15111509
}
15121510

src/libsyntax/feature_gate.rs

+47-13
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,18 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12921292
attribute is just used for rustc unit \
12931293
tests and will never be stable",
12941294
cfg_fn!(rustc_attrs))),
1295+
(sym::rustc_dump_env_program_clauses, Whitelisted, template!(Word), Gated(Stability::Unstable,
1296+
sym::rustc_attrs,
1297+
"the `#[rustc_dump_env_program_clauses]` \
1298+
attribute is just used for rustc unit \
1299+
tests and will never be stable",
1300+
cfg_fn!(rustc_attrs))),
1301+
(sym::rustc_object_lifetime_default, Whitelisted, template!(Word), Gated(Stability::Unstable,
1302+
sym::rustc_attrs,
1303+
"the `#[rustc_object_lifetime_default]` \
1304+
attribute is just used for rustc unit \
1305+
tests and will never be stable",
1306+
cfg_fn!(rustc_attrs))),
12951307
(sym::rustc_test_marker, Normal, template!(Word), Gated(Stability::Unstable,
12961308
sym::rustc_attrs,
12971309
"the `#[rustc_test_marker]` attribute \
@@ -1353,6 +1365,26 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13531365
"internal implementation detail",
13541366
cfg_fn!(rustc_attrs))),
13551367

1368+
(sym::rustc_allocator_nounwind, Whitelisted, template!(Word), Gated(Stability::Unstable,
1369+
sym::rustc_attrs,
1370+
"internal implementation detail",
1371+
cfg_fn!(rustc_attrs))),
1372+
1373+
(sym::rustc_doc_only_macro, Whitelisted, template!(Word), Gated(Stability::Unstable,
1374+
sym::rustc_attrs,
1375+
"internal implementation detail",
1376+
cfg_fn!(rustc_attrs))),
1377+
1378+
(sym::rustc_promotable, Whitelisted, template!(Word), Gated(Stability::Unstable,
1379+
sym::rustc_attrs,
1380+
"internal implementation detail",
1381+
cfg_fn!(rustc_attrs))),
1382+
1383+
(sym::rustc_allow_const_fn_ptr, Whitelisted, template!(Word), Gated(Stability::Unstable,
1384+
sym::rustc_attrs,
1385+
"internal implementation detail",
1386+
cfg_fn!(rustc_attrs))),
1387+
13561388
(sym::rustc_dummy, Normal, template!(Word /* doesn't matter*/), Gated(Stability::Unstable,
13571389
sym::rustc_attrs,
13581390
"used by the test suite",
@@ -1639,6 +1671,14 @@ impl<'a> Context<'a> {
16391671
}
16401672
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
16411673
return;
1674+
} else {
1675+
for segment in &attr.path.segments {
1676+
if segment.ident.as_str().starts_with("rustc") {
1677+
let msg = "attributes starting with `rustc` are \
1678+
reserved for use by the `rustc` compiler";
1679+
gate_feature!(self, rustc_attrs, segment.ident.span, msg);
1680+
}
1681+
}
16421682
}
16431683
for &(n, ty) in self.plugin_attributes {
16441684
if attr.path == n {
@@ -1649,19 +1689,13 @@ impl<'a> Context<'a> {
16491689
return;
16501690
}
16511691
}
1652-
if !attr::is_known(attr) {
1653-
if attr.name_or_empty().as_str().starts_with("rustc_") {
1654-
let msg = "unless otherwise specified, attributes with the prefix `rustc_` \
1655-
are reserved for internal compiler diagnostics";
1656-
gate_feature!(self, rustc_attrs, attr.span, msg);
1657-
} else if !is_macro {
1658-
// Only run the custom attribute lint during regular feature gate
1659-
// checking. Macro gating runs before the plugin attributes are
1660-
// registered, so we skip this in that case.
1661-
let msg = format!("The attribute `{}` is currently unknown to the compiler and \
1662-
may have meaning added to it in the future", attr.path);
1663-
gate_feature!(self, custom_attribute, attr.span, &msg);
1664-
}
1692+
if !is_macro && !attr::is_known(attr) {
1693+
// Only run the custom attribute lint during regular feature gate
1694+
// checking. Macro gating runs before the plugin attributes are
1695+
// registered, so we skip this in that case.
1696+
let msg = format!("The attribute `{}` is currently unknown to the compiler and \
1697+
may have meaning added to it in the future", attr.path);
1698+
gate_feature!(self, custom_attribute, attr.span, &msg);
16651699
}
16661700
}
16671701
}

src/test/run-pass-fulldeps/issue-15778-pass.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// ignore-stage1
33
// compile-flags: -D crate-not-okay
44

5-
#![feature(plugin, rustc_attrs)]
5+
#![feature(plugin, custom_attribute, custom_inner_attributes, rustc_attrs)]
6+
67
#![plugin(lint_for_crate)]
78
#![rustc_crate_okay]
89
#![rustc_crate_blue]
910
#![rustc_crate_red]
1011
#![rustc_crate_grey]
1112
#![rustc_crate_green]
1213

13-
pub fn main() { }
14+
fn main() {}

src/test/run-pass/attr-on-generic-formals.rs

-52
This file was deleted.

src/test/ui/attributes/attrs-with-no-formal-in-generics-1.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
struct RefIntPair<'a, 'b>(&'a u32, &'b u32);
88

9-
impl<#[rustc_1] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
9+
impl<#[rustc_dummy] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
1010
//~^ ERROR trailing attribute after generic parameter
1111
}
1212

13-
fn main() {
14-
15-
}
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: trailing attribute after generic parameter
2-
--> $DIR/attrs-with-no-formal-in-generics-1.rs:9:25
2+
--> $DIR/attrs-with-no-formal-in-generics-1.rs:9:29
33
|
4-
LL | impl<#[rustc_1] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
5-
| ^^^^^^^ attributes must go before parameters
4+
LL | impl<#[rustc_dummy] 'a, 'b, #[oops]> RefIntPair<'a, 'b> {
5+
| ^^^^^^^ attributes must go before parameters
66

77
error: aborting due to previous error
88

src/test/ui/attributes/attrs-with-no-formal-in-generics-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
struct RefAny<'a, T>(&'a T);
88

9-
impl<#[rustc_1] 'a, #[rustc_2] T, #[oops]> RefAny<'a, T> {}
9+
impl<#[rustc_dummy] 'a, #[rustc_dummy] T, #[oops]> RefAny<'a, T> {}
1010
//~^ ERROR trailing attribute after generic parameter
1111

1212
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: trailing attribute after generic parameter
2-
--> $DIR/attrs-with-no-formal-in-generics-2.rs:9:35
2+
--> $DIR/attrs-with-no-formal-in-generics-2.rs:9:43
33
|
4-
LL | impl<#[rustc_1] 'a, #[rustc_2] T, #[oops]> RefAny<'a, T> {}
5-
| ^^^^^^^ attributes must go before parameters
4+
LL | impl<#[rustc_dummy] 'a, #[rustc_dummy] T, #[oops]> RefAny<'a, T> {}
5+
| ^^^^^^^ attributes must go before parameters
66

77
error: aborting due to previous error
88

src/test/ui/consts/min_const_fn/allow_const_fn_ptr_feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const fn error(_: fn()) {}
55

66
#[stable(feature = "rust1", since = "1.0.0")]
77
#[rustc_allow_const_fn_ptr]
8-
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
8+
//~^ ERROR internal implementation detail
99
const fn compiles(_: fn()) {}
1010

1111
fn main() {}

src/test/ui/consts/min_const_fn/allow_const_fn_ptr_feature_gate.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
2-
--> $DIR/allow_const_fn_ptr_feature_gate.rs:7:3
1+
error[E0658]: internal implementation detail
2+
--> $DIR/allow_const_fn_ptr_feature_gate.rs:7:1
33
|
44
LL | #[rustc_allow_const_fn_ptr]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
88
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
// Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.
22

3-
#[rustc_foo]
4-
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
3+
#![feature(decl_macro)]
54

5+
mod rustc { pub macro unknown() {} }
6+
mod unknown { pub macro rustc() {} }
7+
8+
#[rustc::unknown]
9+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
10+
//~| ERROR macro `rustc::unknown` may not be used in attributes
11+
fn f() {}
12+
13+
#[unknown::rustc]
14+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
15+
//~| ERROR macro `unknown::rustc` may not be used in attributes
16+
fn g() {}
17+
18+
#[rustc_dummy]
19+
//~^ ERROR used by the test suite
20+
#[rustc_unknown]
21+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
22+
//~| ERROR attribute `rustc_unknown` is currently unknown
623
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,60 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
2-
--> $DIR/feature-gate-rustc-attrs.rs:3:3
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
2+
--> $DIR/feature-gate-rustc-attrs.rs:8:3
33
|
4-
LL | #[rustc_foo]
5-
| ^^^^^^^^^
4+
LL | #[rustc::unknown]
5+
| ^^^^^
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
88
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
99

10-
error: aborting due to previous error
10+
error: macro `rustc::unknown` may not be used in attributes
11+
--> $DIR/feature-gate-rustc-attrs.rs:8:1
12+
|
13+
LL | #[rustc::unknown]
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
17+
--> $DIR/feature-gate-rustc-attrs.rs:13:12
18+
|
19+
LL | #[unknown::rustc]
20+
| ^^^^^
21+
|
22+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
23+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
24+
25+
error: macro `unknown::rustc` may not be used in attributes
26+
--> $DIR/feature-gate-rustc-attrs.rs:13:1
27+
|
28+
LL | #[unknown::rustc]
29+
| ^^^^^^^^^^^^^^^^^
30+
31+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
32+
--> $DIR/feature-gate-rustc-attrs.rs:20:3
33+
|
34+
LL | #[rustc_unknown]
35+
| ^^^^^^^^^^^^^
36+
|
37+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
38+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
39+
40+
error[E0658]: The attribute `rustc_unknown` is currently unknown to the compiler and may have meaning added to it in the future
41+
--> $DIR/feature-gate-rustc-attrs.rs:20:3
42+
|
43+
LL | #[rustc_unknown]
44+
| ^^^^^^^^^^^^^
45+
|
46+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
47+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
48+
49+
error[E0658]: used by the test suite
50+
--> $DIR/feature-gate-rustc-attrs.rs:18:1
51+
|
52+
LL | #[rustc_dummy]
53+
| ^^^^^^^^^^^^^^
54+
|
55+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
56+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
57+
58+
error: aborting due to 7 previous errors
1159

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

0 commit comments

Comments
 (0)