Skip to content

Commit 62b38c1

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

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
@@ -1289,6 +1289,18 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12891289
attribute is just used for rustc unit \
12901290
tests and will never be stable",
12911291
cfg_fn!(rustc_attrs))),
1292+
(sym::rustc_dump_env_program_clauses, Whitelisted, template!(Word), Gated(Stability::Unstable,
1293+
sym::rustc_attrs,
1294+
"the `#[rustc_dump_env_program_clauses]` \
1295+
attribute is just used for rustc unit \
1296+
tests and will never be stable",
1297+
cfg_fn!(rustc_attrs))),
1298+
(sym::rustc_object_lifetime_default, Whitelisted, template!(Word), Gated(Stability::Unstable,
1299+
sym::rustc_attrs,
1300+
"the `#[rustc_object_lifetime_default]` \
1301+
attribute is just used for rustc unit \
1302+
tests and will never be stable",
1303+
cfg_fn!(rustc_attrs))),
12921304
(sym::rustc_test_marker, Normal, template!(Word), Gated(Stability::Unstable,
12931305
sym::rustc_attrs,
12941306
"the `#[rustc_test_marker]` attribute \
@@ -1350,6 +1362,26 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13501362
"internal implementation detail",
13511363
cfg_fn!(rustc_attrs))),
13521364

1365+
(sym::rustc_allocator_nounwind, Whitelisted, template!(Word), Gated(Stability::Unstable,
1366+
sym::rustc_attrs,
1367+
"internal implementation detail",
1368+
cfg_fn!(rustc_attrs))),
1369+
1370+
(sym::rustc_doc_only_macro, Whitelisted, template!(Word), Gated(Stability::Unstable,
1371+
sym::rustc_attrs,
1372+
"internal implementation detail",
1373+
cfg_fn!(rustc_attrs))),
1374+
1375+
(sym::rustc_promotable, Whitelisted, template!(Word), Gated(Stability::Unstable,
1376+
sym::rustc_attrs,
1377+
"internal implementation detail",
1378+
cfg_fn!(rustc_attrs))),
1379+
1380+
(sym::rustc_allow_const_fn_ptr, Whitelisted, template!(Word), Gated(Stability::Unstable,
1381+
sym::rustc_attrs,
1382+
"internal implementation detail",
1383+
cfg_fn!(rustc_attrs))),
1384+
13531385
(sym::rustc_dummy, Normal, template!(Word /* doesn't matter*/), Gated(Stability::Unstable,
13541386
sym::rustc_attrs,
13551387
"used by the test suite",
@@ -1636,6 +1668,14 @@ impl<'a> Context<'a> {
16361668
}
16371669
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
16381670
return;
1671+
} else {
1672+
for segment in &attr.path.segments {
1673+
if segment.ident.as_str().starts_with("rustc") {
1674+
let msg = "attributes starting with `rustc` are \
1675+
reserved for use by the `rustc` compiler";
1676+
gate_feature!(self, rustc_attrs, segment.ident.span, msg);
1677+
}
1678+
}
16391679
}
16401680
for &(n, ty) in self.plugin_attributes {
16411681
if attr.path == n {
@@ -1646,19 +1686,13 @@ impl<'a> Context<'a> {
16461686
return;
16471687
}
16481688
}
1649-
if !attr::is_known(attr) {
1650-
if attr.name_or_empty().as_str().starts_with("rustc_") {
1651-
let msg = "unless otherwise specified, attributes with the prefix `rustc_` \
1652-
are reserved for internal compiler diagnostics";
1653-
gate_feature!(self, rustc_attrs, attr.span, msg);
1654-
} else if !is_macro {
1655-
// Only run the custom attribute lint during regular feature gate
1656-
// checking. Macro gating runs before the plugin attributes are
1657-
// registered, so we skip this in that case.
1658-
let msg = format!("The attribute `{}` is currently unknown to the compiler and \
1659-
may have meaning added to it in the future", attr.path);
1660-
gate_feature!(self, custom_attribute, attr.span, &msg);
1661-
}
1689+
if !is_macro && !attr::is_known(attr) {
1690+
// Only run the custom attribute lint during regular feature gate
1691+
// checking. Macro gating runs before the plugin attributes are
1692+
// registered, so we skip this in that case.
1693+
let msg = format!("The attribute `{}` is currently unknown to the compiler and \
1694+
may have meaning added to it in the future", attr.path);
1695+
gate_feature!(self, custom_attribute, attr.span, &msg);
16621696
}
16631697
}
16641698
}

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)