Skip to content

Commit 8ec8125

Browse files
committed
Auto merge of #62133 - petrochenkov:norustc, r=<try>
Feature gate `rustc` attributes harder cc #62116
2 parents 5f9c044 + 7fbb936 commit 8ec8125

11 files changed

+34
-26
lines changed

src/librustc_resolve/macros.rs

+16-12
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,18 +332,10 @@ 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);
326337
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 {
338+
if !features.custom_attribute && !name.starts_with("rustc_") {
335339
let msg = format!("The attribute `{}` is currently unknown to the \
336340
compiler and may have meaning added to it in the \
337341
future", path);

src/libsyntax/feature_gate.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,14 @@ impl<'a> Context<'a> {
16371637
}
16381638
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
16391639
return;
1640+
} else {
1641+
for segment in &attr.path.segments {
1642+
if segment.ident.as_str().starts_with("rustc") {
1643+
let msg = "attributes starting with `rustc` are \
1644+
reserved for use by the `rustc` compiler";
1645+
gate_feature!(self, rustc_attrs, segment.ident.span, msg);
1646+
}
1647+
}
16401648
}
16411649
for &(n, ty) in self.plugin_attributes {
16421650
if attr.path == n {
@@ -1648,11 +1656,7 @@ impl<'a> Context<'a> {
16481656
}
16491657
}
16501658
if !attr::is_known(attr) {
1651-
if attr.name_or_empty().as_str().starts_with("rustc_") {
1652-
let msg = "unless otherwise specified, attributes with the prefix `rustc_` \
1653-
are reserved for internal compiler diagnostics";
1654-
gate_feature!(self, rustc_attrs, attr.span, msg);
1655-
} else if !is_macro {
1659+
if !is_macro && !attr.name_or_empty().as_str().starts_with("rustc_") {
16561660
// Only run the custom attribute lint during regular feature gate
16571661
// checking. Macro gating runs before the plugin attributes are
16581662
// registered, so we skip this in that case.

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 attributes starting with `rustc` are reserved for use by the `rustc` compiler
99
const fn compiles(_: fn()) {}
1010

1111
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
22
--> $DIR/allow_const_fn_ptr_feature_gate.rs:7:3
33
|
44
LL | #[rustc_allow_const_fn_ptr]
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.
22

33
#[rustc_foo]
4-
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
4+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
55

66
fn main() {}

src/test/ui/feature-gates/feature-gate-rustc-attrs.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
22
--> $DIR/feature-gate-rustc-attrs.rs:3:3
33
|
44
LL | #[rustc_foo]

src/test/ui/proc-macro/expand-to-unstable-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern crate derive_unstable_2;
77

88
#[derive(Unstable)]
9-
//~^ ERROR: reserved for internal compiler
9+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
1010
struct A;
1111

1212
fn main() {

src/test/ui/proc-macro/expand-to-unstable-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
22
--> $DIR/expand-to-unstable-2.rs:8:10
33
|
44
LL | #[derive(Unstable)]

src/test/ui/reserved/reserved-attr-on-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[rustc_attribute_should_be_reserved]
2-
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
2+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
33
macro_rules! foo {
44
() => (());
55
}

src/test/ui/reserved/reserved-attr-on-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
22
--> $DIR/reserved-attr-on-macro.rs:1:3
33
|
44
LL | #[rustc_attribute_should_be_reserved]

src/test/ui/suggestions/attribute-typos.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
22
--> $DIR/attribute-typos.rs:11:3
33
|
44
LL | #[rustc_err]

0 commit comments

Comments
 (0)