Skip to content

Commit 351442b

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

25 files changed

+251
-191
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 17 additions & 14 deletions
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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,7 @@ impl<'feat> ExpansionConfig<'feat> {
15251525
}
15261526

15271527
fn enable_custom_inner_attributes(&self) -> bool {
1528-
self.features.map_or(false, |features| {
1529-
features.custom_inner_attributes || features.custom_attribute || features.rustc_attrs
1530-
})
1528+
self.features.map_or(false, |features| features.custom_inner_attributes)
15311529
}
15321530
}
15331531

src/libsyntax/feature_gate.rs

Lines changed: 47 additions & 13 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 0 additions & 52 deletions
This file was deleted.

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

Lines changed: 2 additions & 4 deletions
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() {}
Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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() {}
Lines changed: 3 additions & 3 deletions
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

0 commit comments

Comments
 (0)