]> git.lizzy.rs Git - rust.git/commitdiff
Feature gate `rustc` attributes harder
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 30 Jun 2019 10:00:45 +0000 (13:00 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 30 Jun 2019 10:57:12 +0000 (13:57 +0300)
src/librustc_resolve/macros.rs
src/libsyntax/feature_gate.rs
src/test/run-pass-fulldeps/issue-15778-pass.rs
src/test/ui/feature-gates/feature-gate-rustc-attrs.rs
src/test/ui/feature-gates/feature-gate-rustc-attrs.stderr
src/test/ui/proc-macro/expand-to-unstable-2.rs
src/test/ui/proc-macro/expand-to-unstable-2.stderr
src/test/ui/reserved/reserved-attr-on-macro.rs
src/test/ui/reserved/reserved-attr-on-macro.stderr
src/test/ui/suggestions/attribute-typos.rs
src/test/ui/suggestions/attribute-typos.stderr

index 08a105572376cae4e8e0b4030ba506722465979e..522dc1aa9f8deb7698751d349feade5f4fef9e3a 100644 (file)
@@ -19,9 +19,8 @@
 use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
 use syntax::ext::hygiene::Mark;
 use syntax::ext::tt::macro_rules;
-use syntax::feature_gate::{
-    feature_err, is_builtin_attr_name, AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES,
-};
+use syntax::feature_gate::{feature_err, emit_feature_err, is_builtin_attr_name};
+use syntax::feature_gate::{AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES};
 use syntax::symbol::{Symbol, kw, sym};
 use syntax::visit::Visitor;
 use syntax::util::lev_distance::find_best_match_for_name;
@@ -298,12 +297,25 @@ fn resolve_macro_to_res(
         let res = self.resolve_macro_to_res_inner(path, kind, parent_scope, trace, force);
 
         // Report errors and enforce feature gates for the resolved macro.
+        let features = self.session.features_untracked();
         if res != Err(Determinacy::Undetermined) {
             // Do not report duplicated errors on every undetermined resolution.
             for segment in &path.segments {
                 if let Some(args) = &segment.args {
                     self.session.span_err(args.span(), "generic arguments in macro path");
                 }
+                if kind == MacroKind::Attr && !features.rustc_attrs &&
+                   segment.ident.as_str().starts_with("rustc") {
+                    let msg = "attributes starting with `rustc` are \
+                               reserved for use by the `rustc` compiler";
+                    emit_feature_err(
+                        &self.session.parse_sess,
+                        sym::rustc_attrs,
+                        segment.ident.span,
+                        GateIssue::Language,
+                        msg,
+                    );
+                }
             }
         }
 
@@ -320,7 +332,6 @@ fn resolve_macro_to_res(
             }
             Res::NonMacroAttr(attr_kind) => {
                 if kind == MacroKind::Attr {
-                    let features = self.session.features_untracked();
                     if attr_kind == NonMacroAttrKind::Custom {
                         assert!(path.segments.len() == 1);
                         if !features.custom_attribute {
index 9aea995831edb4e03dbc6aa1454feab89edebbfa..6037b06eaf20ec94285fb496b17d10c32f020ea2 100644 (file)
@@ -1669,6 +1669,14 @@ fn check_attribute(
             }
             debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
             return;
+        } else {
+            for segment in &attr.path.segments {
+                if segment.ident.as_str().starts_with("rustc") {
+                    let msg = "attributes starting with `rustc` are \
+                               reserved for use by the `rustc` compiler";
+                    gate_feature!(self, rustc_attrs, segment.ident.span, msg);
+                }
+            }
         }
         for &(n, ty) in self.plugin_attributes {
             if attr.path == n {
index 23c1c0766770b1a1c5432beb91bfeeda5ad49e5a..35152e7f4babd4a6a28c8bc261979e07dbddcb59 100644 (file)
@@ -2,7 +2,7 @@
 // ignore-stage1
 // compile-flags: -D crate-not-okay
 
-#![feature(plugin, custom_attribute, custom_inner_attributes)]
+#![feature(plugin, custom_attribute, custom_inner_attributes, rustc_attrs)]
 
 #![plugin(lint_for_crate)]
 #![rustc_crate_okay]
index 95ff18213f7978c0ddef6cf49b047e7f1448be36..d3a2e486416afd9ae47c570f4f8c31c716b8e510 100644 (file)
@@ -1,6 +1,23 @@
 // Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.
 
+#![feature(decl_macro)]
+
+mod rustc { pub macro unknown() {} }
+mod unknown { pub macro rustc() {} }
+
+#[rustc::unknown]
+//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+//~| ERROR macro `rustc::unknown` may not be used in attributes
+fn f() {}
+
+#[unknown::rustc]
+//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+//~| ERROR macro `unknown::rustc` may not be used in attributes
+fn g() {}
+
 #[rustc_dummy]
 //~^ ERROR used by the test suite
-
+#[rustc_unknown]
+//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+//~| ERROR attribute `rustc_unknown` is currently unknown
 fn main() {}
index 57dcc77e518f39f27d039886371d70886ce0c403..cdc7b27a749e5f4c21eb31bc814e9b3c6c7f8155 100644 (file)
@@ -1,5 +1,53 @@
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/feature-gate-rustc-attrs.rs:8:3
+   |
+LL | #[rustc::unknown]
+   |   ^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
+error: macro `rustc::unknown` may not be used in attributes
+  --> $DIR/feature-gate-rustc-attrs.rs:8:1
+   |
+LL | #[rustc::unknown]
+   | ^^^^^^^^^^^^^^^^^
+
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/feature-gate-rustc-attrs.rs:13:12
+   |
+LL | #[unknown::rustc]
+   |            ^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
+error: macro `unknown::rustc` may not be used in attributes
+  --> $DIR/feature-gate-rustc-attrs.rs:13:1
+   |
+LL | #[unknown::rustc]
+   | ^^^^^^^^^^^^^^^^^
+
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/feature-gate-rustc-attrs.rs:20:3
+   |
+LL | #[rustc_unknown]
+   |   ^^^^^^^^^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
+error[E0658]: The attribute `rustc_unknown` is currently unknown to the compiler and may have meaning added to it in the future
+  --> $DIR/feature-gate-rustc-attrs.rs:20:3
+   |
+LL | #[rustc_unknown]
+   |   ^^^^^^^^^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(custom_attribute)] to the crate attributes to enable
+
 error[E0658]: used by the test suite
-  --> $DIR/feature-gate-rustc-attrs.rs:3:1
+  --> $DIR/feature-gate-rustc-attrs.rs:18:1
    |
 LL | #[rustc_dummy]
    | ^^^^^^^^^^^^^^
@@ -7,6 +55,6 @@ LL | #[rustc_dummy]
    = note: for more information, see https://github.com/rust-lang/rust/issues/29642
    = help: add #![feature(rustc_attrs)] to the crate attributes to enable
 
-error: aborting due to previous error
+error: aborting due to 7 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
index 8ce3cde6f043287b09f8526133ef352e746481f2..437ae930934709c94b2e4e54f33cf46bc83a8cb2 100644 (file)
@@ -4,7 +4,9 @@
 extern crate derive_unstable_2;
 
 #[derive(Unstable)]
-//~^ ERROR attribute `rustc_foo` is currently unknown
+//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+//~| ERROR attribute `rustc_foo` is currently unknown to the compiler
+
 struct A;
 
 fn main() {
index 77be3d52723940f1d312ebacb4196a9f99061ead..803773db88e9636cecdcd07a0d10d59ad37a3c09 100644 (file)
@@ -1,3 +1,12 @@
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/expand-to-unstable-2.rs:6:10
+   |
+LL | #[derive(Unstable)]
+   |          ^^^^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
 error[E0658]: The attribute `rustc_foo` is currently unknown to the compiler and may have meaning added to it in the future
   --> $DIR/expand-to-unstable-2.rs:6:10
    |
@@ -7,6 +16,6 @@ LL | #[derive(Unstable)]
    = note: for more information, see https://github.com/rust-lang/rust/issues/29642
    = help: add #![feature(custom_attribute)] to the crate attributes to enable
 
-error: aborting due to previous error
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
index 0ee6577f8a11d8a5a87af8b72f0a4f0842f5ce57..cb535362266c00365163e35fd25ce5aa82f32b64 100644 (file)
@@ -1,5 +1,7 @@
 #[rustc_attribute_should_be_reserved]
 //~^ ERROR attribute `rustc_attribute_should_be_reserved` is currently unknown
+//~| ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+
 macro_rules! foo {
     () => (());
 }
index 34b21ecd1265d201767b3da9e71aa18b3be6d507..0c62c82017e18fb8c91879fccdb2e195a19a863f 100644 (file)
@@ -1,3 +1,12 @@
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/reserved-attr-on-macro.rs:1:3
+   |
+LL | #[rustc_attribute_should_be_reserved]
+   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
 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
   --> $DIR/reserved-attr-on-macro.rs:1:3
    |
@@ -8,13 +17,13 @@ LL | #[rustc_attribute_should_be_reserved]
    = help: add #![feature(custom_attribute)] to the crate attributes to enable
 
 error: cannot determine resolution for the macro `foo`
-  --> $DIR/reserved-attr-on-macro.rs:8:5
+  --> $DIR/reserved-attr-on-macro.rs:10:5
    |
 LL |     foo!();
    |     ^^^
    |
    = note: import resolution is stuck, try simplifying macro imports
 
-error: aborting due to 2 previous errors
+error: aborting due to 3 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
index b45acbdd9206c4a82813f65eca4a6cc9aafbf2b0..0e10131ce8d1f4d325935d7d5b0d835db04ff8d9 100644 (file)
@@ -4,5 +4,8 @@ fn foo() {}
 #[tests] //~ ERROR attribute `tests` is currently unknown to the compiler
 fn bar() {}
 
-#[rustc_err] //~ ERROR attribute `rustc_err` is currently unknown
+#[rustc_err]
+//~^ ERROR attribute `rustc_err` is currently unknown
+//~| ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
+
 fn main() {}
index ee063aaa122201a3883bde60671152200740156e..958688b4d390652f111e5a559de34780c51eab8a 100644 (file)
@@ -1,3 +1,12 @@
+error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
+  --> $DIR/attribute-typos.rs:7:3
+   |
+LL | #[rustc_err]
+   |   ^^^^^^^^^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/29642
+   = help: add #![feature(rustc_attrs)] to the crate attributes to enable
+
 error[E0658]: The attribute `rustc_err` is currently unknown to the compiler and may have meaning added to it in the future
   --> $DIR/attribute-typos.rs:7:3
    |
@@ -25,6 +34,6 @@ LL | #[deprcated]
    = note: for more information, see https://github.com/rust-lang/rust/issues/29642
    = help: add #![feature(custom_attribute)] to the crate attributes to enable
 
-error: aborting due to 3 previous errors
+error: aborting due to 4 previous errors
 
 For more information about this error, try `rustc --explain E0658`.