about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-09-07 15:14:52 +0000
committerbors <bors@rust-lang.org>2018-09-07 15:14:52 +0000
commit24ef47bccf487a2f80f71f228d71e35f89c5e1d3 (patch)
tree69dadaae1b4c213d5e085e4ea5b781f59424bba0 /src/test
parent5a3292f163da3327523ddec5bc44d17c2378ec37 (diff)
parenta6adeae104c7dcc20025d92b984fd56427d93c1f (diff)
downloadrust-24ef47bccf487a2f80f71f228d71e35f89c5e1d3.tar.gz
rust-24ef47bccf487a2f80f71f228d71e35f89c5e1d3.zip
Auto merge of #53893 - petrochenkov:cfgexpr, r=pnkfelix
Validate syntax of `cfg` attributes

Fixes https://github.com/rust-lang/rust/issues/53298
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/cfg-arg-invalid-1.rs3
-rw-r--r--src/test/ui/cfg-arg-invalid-2.rs3
-rw-r--r--src/test/ui/cfg-arg-invalid-3.rs3
-rw-r--r--src/test/ui/cfg-arg-invalid-4.rs3
-rw-r--r--src/test/ui/cfg-arg-invalid-5.rs3
-rw-r--r--src/test/ui/cfg-arg-invalid.rs13
-rw-r--r--src/test/ui/cfg-attr-syntax-validation.rs32
-rw-r--r--src/test/ui/cfg-attr-syntax-validation.stderr60
-rw-r--r--src/test/ui/cfg-empty-codemap.rs2
-rw-r--r--src/test/ui/issues/issue-31495.rs13
10 files changed, 108 insertions, 27 deletions
diff --git a/src/test/ui/cfg-arg-invalid-1.rs b/src/test/ui/cfg-arg-invalid-1.rs
new file mode 100644
index 00000000000..36dd78dd2b1
--- /dev/null
+++ b/src/test/ui/cfg-arg-invalid-1.rs
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a(b=c)
+// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-2.rs b/src/test/ui/cfg-arg-invalid-2.rs
new file mode 100644
index 00000000000..48d656a4a28
--- /dev/null
+++ b/src/test/ui/cfg-arg-invalid-2.rs
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a{b}
+// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-3.rs b/src/test/ui/cfg-arg-invalid-3.rs
new file mode 100644
index 00000000000..96ac7828c5c
--- /dev/null
+++ b/src/test/ui/cfg-arg-invalid-3.rs
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a::b
+// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-4.rs b/src/test/ui/cfg-arg-invalid-4.rs
new file mode 100644
index 00000000000..e7dfa17b4b6
--- /dev/null
+++ b/src/test/ui/cfg-arg-invalid-4.rs
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a(b)
+// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid-5.rs b/src/test/ui/cfg-arg-invalid-5.rs
new file mode 100644
index 00000000000..a939f451038
--- /dev/null
+++ b/src/test/ui/cfg-arg-invalid-5.rs
@@ -0,0 +1,3 @@
+// compile-flags: --cfg a=10
+// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
+fn main() {}
diff --git a/src/test/ui/cfg-arg-invalid.rs b/src/test/ui/cfg-arg-invalid.rs
deleted file mode 100644
index 404630399c6..00000000000
--- a/src/test/ui/cfg-arg-invalid.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// compile-flags: --cfg a{b}
-// error-pattern: invalid --cfg argument: a{b}
-fn main() {}
diff --git a/src/test/ui/cfg-attr-syntax-validation.rs b/src/test/ui/cfg-attr-syntax-validation.rs
new file mode 100644
index 00000000000..06a22eff25c
--- /dev/null
+++ b/src/test/ui/cfg-attr-syntax-validation.rs
@@ -0,0 +1,32 @@
+#[cfg] //~ ERROR `cfg` is not followed by parentheses
+struct S1;
+
+#[cfg = 10] //~ ERROR `cfg` is not followed by parentheses
+struct S2;
+
+#[cfg()] //~ ERROR `cfg` predicate is not specified
+struct S3;
+
+#[cfg(a, b)] //~ ERROR multiple `cfg` predicates are specified
+struct S4;
+
+#[cfg("str")] //~ ERROR `cfg` predicate key cannot be a literal
+struct S5;
+
+#[cfg(a::b)] //~ ERROR `cfg` predicate key must be an identifier
+struct S6;
+
+#[cfg(a())] //~ ERROR invalid predicate `a`
+struct S7;
+
+#[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
+struct S8;
+
+macro_rules! generate_s9 {
+    ($expr: expr) => {
+        #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
+        struct S9;
+    }
+}
+
+generate_s9!(concat!("nonexistent"));
diff --git a/src/test/ui/cfg-attr-syntax-validation.stderr b/src/test/ui/cfg-attr-syntax-validation.stderr
new file mode 100644
index 00000000000..7773fdb8cf9
--- /dev/null
+++ b/src/test/ui/cfg-attr-syntax-validation.stderr
@@ -0,0 +1,60 @@
+error: `cfg` is not followed by parentheses
+  --> $DIR/cfg-attr-syntax-validation.rs:1:1
+   |
+LL | #[cfg] //~ ERROR `cfg` is not followed by parentheses
+   | ^^^^^^ help: expected syntax is: `cfg(/* predicate */)`
+
+error: `cfg` is not followed by parentheses
+  --> $DIR/cfg-attr-syntax-validation.rs:4:1
+   |
+LL | #[cfg = 10] //~ ERROR `cfg` is not followed by parentheses
+   | ^^^^^^^^^^^ help: expected syntax is: `cfg(/* predicate */)`
+
+error: `cfg` predicate is not specified
+  --> $DIR/cfg-attr-syntax-validation.rs:7:1
+   |
+LL | #[cfg()] //~ ERROR `cfg` predicate is not specified
+   | ^^^^^^^^
+
+error: multiple `cfg` predicates are specified
+  --> $DIR/cfg-attr-syntax-validation.rs:10:10
+   |
+LL | #[cfg(a, b)] //~ ERROR multiple `cfg` predicates are specified
+   |          ^
+
+error: `cfg` predicate key cannot be a literal
+  --> $DIR/cfg-attr-syntax-validation.rs:13:7
+   |
+LL | #[cfg("str")] //~ ERROR `cfg` predicate key cannot be a literal
+   |       ^^^^^
+
+error: `cfg` predicate key must be an identifier
+  --> $DIR/cfg-attr-syntax-validation.rs:16:7
+   |
+LL | #[cfg(a::b)] //~ ERROR `cfg` predicate key must be an identifier
+   |       ^^^^
+
+error[E0537]: invalid predicate `a`
+  --> $DIR/cfg-attr-syntax-validation.rs:19:7
+   |
+LL | #[cfg(a())] //~ ERROR invalid predicate `a`
+   |       ^^^
+
+error: literal in `cfg` predicate value must be a string
+  --> $DIR/cfg-attr-syntax-validation.rs:22:11
+   |
+LL | #[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
+   |           ^^
+
+error: `cfg` is not a well-formed meta-item
+  --> $DIR/cfg-attr-syntax-validation.rs:27:9
+   |
+LL |         #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
+   |         ^^^^^^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`
+...
+LL | generate_s9!(concat!("nonexistent"));
+   | ------------------------------------- in this macro invocation
+
+error: aborting due to 9 previous errors
+
+For more information about this error, try `rustc --explain E0537`.
diff --git a/src/test/ui/cfg-empty-codemap.rs b/src/test/ui/cfg-empty-codemap.rs
index f06d22d985f..5cf8135ca6b 100644
--- a/src/test/ui/cfg-empty-codemap.rs
+++ b/src/test/ui/cfg-empty-codemap.rs
@@ -12,7 +12,7 @@
 
 // compile-flags: --cfg ""
 
-// error-pattern: expected identifier, found
+// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`)
 
 pub fn main() {
 }
diff --git a/src/test/ui/issues/issue-31495.rs b/src/test/ui/issues/issue-31495.rs
deleted file mode 100644
index 794b8bb86bb..00000000000
--- a/src/test/ui/issues/issue-31495.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// compile-flags: --cfg foo(bar)
-// error-pattern: invalid predicate in --cfg command line argument: `foo`
-fn main() {}