Skip to content

Commit e4e7eb2

Browse files
committed
Feature gate rustc attributes harder
1 parent 0ffb643 commit e4e7eb2

11 files changed

+132
-14
lines changed

src/librustc_resolve/macros.rs

+15-4
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,7 +332,6 @@ 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
if !features.custom_attribute {

src/libsyntax/feature_gate.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,14 @@ impl<'a> Context<'a> {
16691669
}
16701670
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
16711671
return;
1672+
} else {
1673+
for segment in &attr.path.segments {
1674+
if segment.ident.as_str().starts_with("rustc") {
1675+
let msg = "attributes starting with `rustc` are \
1676+
reserved for use by the `rustc` compiler";
1677+
gate_feature!(self, rustc_attrs, segment.ident.span, msg);
1678+
}
1679+
}
16721680
}
16731681
for &(n, ty) in self.plugin_attributes {
16741682
if attr.path == n {

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

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

5-
#![feature(plugin, custom_attribute, custom_inner_attributes)]
5+
#![feature(plugin, custom_attribute, custom_inner_attributes, rustc_attrs)]
66

77
#![plugin(lint_for_crate)]
88
#![rustc_crate_okay]
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+
#![feature(decl_macro)]
4+
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+
318
#[rustc_dummy]
419
//~^ ERROR used by the test suite
5-
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]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
2+
--> $DIR/feature-gate-rustc-attrs.rs:8:3
3+
|
4+
LL | #[rustc::unknown]
5+
| ^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
9+
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+
149
error[E0658]: used by the test suite
2-
--> $DIR/feature-gate-rustc-attrs.rs:3:1
50+
--> $DIR/feature-gate-rustc-attrs.rs:18:1
351
|
452
LL | #[rustc_dummy]
553
| ^^^^^^^^^^^^^^
654
|
755
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
856
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
957

10-
error: aborting due to previous error
58+
error: aborting due to 7 previous errors
1159

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

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
extern crate derive_unstable_2;
55

66
#[derive(Unstable)]
7-
//~^ ERROR attribute `rustc_foo` is currently unknown
7+
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
8+
//~| ERROR attribute `rustc_foo` is currently unknown to the compiler
9+
810
struct A;
911

1012
fn main() {

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
2+
--> $DIR/expand-to-unstable-2.rs:6:10
3+
|
4+
LL | #[derive(Unstable)]
5+
| ^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
9+
110
error[E0658]: The attribute `rustc_foo` is currently unknown to the compiler and may have meaning added to it in the future
211
--> $DIR/expand-to-unstable-2.rs:6:10
312
|
@@ -7,6 +16,6 @@ LL | #[derive(Unstable)]
716
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
817
= help: add #![feature(custom_attribute)] to the crate attributes to enable
918

10-
error: aborting due to previous error
19+
error: aborting due to 2 previous errors
1120

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[rustc_attribute_should_be_reserved]
22
//~^ ERROR attribute `rustc_attribute_should_be_reserved` is currently unknown
3+
//~| ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
4+
35
macro_rules! foo {
46
() => (());
57
}

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
2+
--> $DIR/reserved-attr-on-macro.rs:1:3
3+
|
4+
LL | #[rustc_attribute_should_be_reserved]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
9+
110
error[E0658]: The attribute `rustc_attribute_should_be_reserved` is currently unknown to the compiler and may have meaning added to it in the future
211
--> $DIR/reserved-attr-on-macro.rs:1:3
312
|
@@ -8,13 +17,13 @@ LL | #[rustc_attribute_should_be_reserved]
817
= help: add #![feature(custom_attribute)] to the crate attributes to enable
918

1019
error: cannot determine resolution for the macro `foo`
11-
--> $DIR/reserved-attr-on-macro.rs:8:5
20+
--> $DIR/reserved-attr-on-macro.rs:10:5
1221
|
1322
LL | foo!();
1423
| ^^^
1524
|
1625
= note: import resolution is stuck, try simplifying macro imports
1726

18-
error: aborting due to 2 previous errors
27+
error: aborting due to 3 previous errors
1928

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

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ fn foo() {}
44
#[tests] //~ ERROR attribute `tests` is currently unknown to the compiler
55
fn bar() {}
66

7-
#[rustc_err] //~ ERROR attribute `rustc_err` is currently unknown
7+
#[rustc_err]
8+
//~^ ERROR attribute `rustc_err` is currently unknown
9+
//~| ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
10+
811
fn main() {}

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
2+
--> $DIR/attribute-typos.rs:7:3
3+
|
4+
LL | #[rustc_err]
5+
| ^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
8+
= help: add #![feature(rustc_attrs)] to the crate attributes to enable
9+
110
error[E0658]: The attribute `rustc_err` is currently unknown to the compiler and may have meaning added to it in the future
211
--> $DIR/attribute-typos.rs:7:3
312
|
@@ -25,6 +34,6 @@ LL | #[deprcated]
2534
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
2635
= help: add #![feature(custom_attribute)] to the crate attributes to enable
2736

28-
error: aborting due to 3 previous errors
37+
error: aborting due to 4 previous errors
2938

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

0 commit comments

Comments
 (0)