about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorCatherine <catherine.3.flores@gmail.com>2023-07-24 04:55:47 +0000
committerCatherine Flores <catherine.3.flores@gmail.com>2023-07-24 00:25:17 -0500
commit287db04636ffefa3fdaa39fe0fdcc3cf75b60444 (patch)
tree745b89b10863c02ffe96a33220b12aea6fcfcca2 /tests
parent8771282d4e7a5c4569e49d1f878fb3ba90a974d0 (diff)
downloadrust-287db04636ffefa3fdaa39fe0fdcc3cf75b60444.tar.gz
rust-287db04636ffefa3fdaa39fe0fdcc3cf75b60444.zip
Specify macro is invalid in certain contexts
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field-2.rs16
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field-2.stderr18
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field-3.rs15
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field-3.stderr19
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field.rs26
-rw-r--r--tests/ui/parser/macro/macro-expand-to-field.stderr29
-rw-r--r--tests/ui/parser/macro/macro-expand-to-match-arm.rs15
-rw-r--r--tests/ui/parser/macro/macro-expand-to-match-arm.stderr10
8 files changed, 148 insertions, 0 deletions
diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.rs b/tests/ui/parser/macro/macro-expand-to-field-2.rs
new file mode 100644
index 00000000000..2f806bcea84
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field-2.rs
@@ -0,0 +1,16 @@
+#![no_main]
+
+macro_rules! field {
+    ($name:ident:$type:ty) => {
+        $name:$type
+    };
+}
+
+enum EnumVariantField {
+    Named { //~ NOTE while parsing this struct
+        field!(oopsies:()), //~ NOTE macros cannot expand to struct fields
+        //~^ ERROR expected `:`, found `!`
+        //~^^ ERROR expected `,`, or `}`, found `(`
+        //~^^^ NOTE expected `:`
+    },
+}
diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.stderr b/tests/ui/parser/macro/macro-expand-to-field-2.stderr
new file mode 100644
index 00000000000..8c3758173fe
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field-2.stderr
@@ -0,0 +1,18 @@
+error: expected `:`, found `!`
+  --> $DIR/macro-expand-to-field-2.rs:11:14
+   |
+LL |         field!(oopsies:()),
+   |              ^ expected `:`
+   |
+   = note: macros cannot expand to struct fields
+
+error: expected `,`, or `}`, found `(`
+  --> $DIR/macro-expand-to-field-2.rs:11:15
+   |
+LL |     Named {
+   |     ----- while parsing this struct
+LL |         field!(oopsies:()),
+   |               ^
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.rs b/tests/ui/parser/macro/macro-expand-to-field-3.rs
new file mode 100644
index 00000000000..0a8b655e0dc
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field-3.rs
@@ -0,0 +1,15 @@
+#![no_main]
+
+macro_rules! field {
+    ($name:ident:$type:ty) => {
+        $name:$type
+    };
+}
+
+union EnumVariantField { //~ NOTE while parsing this union
+    A: u32,
+    field!(oopsies:()), //~ NOTE macros cannot expand to union fields
+    //~^ ERROR expected `:`, found `!`
+    //~^^ ERROR expected `,`, or `}`, found `(`
+    //~^^^ NOTE expected `:`
+}
diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.stderr b/tests/ui/parser/macro/macro-expand-to-field-3.stderr
new file mode 100644
index 00000000000..360b2bf793b
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field-3.stderr
@@ -0,0 +1,19 @@
+error: expected `:`, found `!`
+  --> $DIR/macro-expand-to-field-3.rs:11:10
+   |
+LL |     field!(oopsies:()),
+   |          ^ expected `:`
+   |
+   = note: macros cannot expand to union fields
+
+error: expected `,`, or `}`, found `(`
+  --> $DIR/macro-expand-to-field-3.rs:11:11
+   |
+LL | union EnumVariantField {
+   |       ---------------- while parsing this union
+LL |     A: u32,
+LL |     field!(oopsies:()),
+   |           ^
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/parser/macro/macro-expand-to-field.rs b/tests/ui/parser/macro/macro-expand-to-field.rs
new file mode 100644
index 00000000000..38055a3bbb2
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field.rs
@@ -0,0 +1,26 @@
+#![no_main]
+
+macro_rules! field {
+    ($name:ident:$type:ty) => {
+        $name:$type
+    };
+}
+
+macro_rules! variant {
+    ($name:ident) => {
+        $name
+    }
+}
+
+struct Struct { //~ NOTE while parsing this struct
+    field!(bar:u128), //~ NOTE macros cannot expand to struct fields
+    //~^ ERROR expected `:`, found `!`
+    //~^^ NOTE expected `:`
+    //~^^^ ERROR expected `,`, or `}`, found `(`
+}
+
+enum EnumVariant { //~ NOTE while parsing this enum
+    variant!(whoops), //~ NOTE macros cannot expand to enum variants
+    //~^ ERROR unexpected token: `!`
+    //~^^ NOTE unexpected token after this
+}
diff --git a/tests/ui/parser/macro/macro-expand-to-field.stderr b/tests/ui/parser/macro/macro-expand-to-field.stderr
new file mode 100644
index 00000000000..3f7fad334ec
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-field.stderr
@@ -0,0 +1,29 @@
+error: expected `:`, found `!`
+  --> $DIR/macro-expand-to-field.rs:16:10
+   |
+LL |     field!(bar:u128),
+   |          ^ expected `:`
+   |
+   = note: macros cannot expand to struct fields
+
+error: expected `,`, or `}`, found `(`
+  --> $DIR/macro-expand-to-field.rs:16:11
+   |
+LL | struct Struct {
+   |        ------ while parsing this struct
+LL |     field!(bar:u128),
+   |           ^
+
+error: unexpected token: `!`
+  --> $DIR/macro-expand-to-field.rs:23:12
+   |
+LL | enum EnumVariant {
+   |      ----------- while parsing this enum
+LL |     variant!(whoops),
+   |            ^ unexpected token after this
+   |
+   = note: macros cannot expand to enum variants
+   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
+
+error: aborting due to 3 previous errors
+
diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.rs b/tests/ui/parser/macro/macro-expand-to-match-arm.rs
new file mode 100644
index 00000000000..043bf371902
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-match-arm.rs
@@ -0,0 +1,15 @@
+macro_rules! arm {
+    ($pattern:pat => $block:block) => {
+        $pattern => $block
+    };
+}
+
+fn main() {
+    let x = Some(1);
+    match x {
+        Some(1) => {},
+        arm!(None => {}), //~ NOTE macros cannot expand to match arms
+        //~^ ERROR unexpected `,` in pattern
+        _ => {},
+    };
+}
diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.stderr b/tests/ui/parser/macro/macro-expand-to-match-arm.stderr
new file mode 100644
index 00000000000..1a5f4696858
--- /dev/null
+++ b/tests/ui/parser/macro/macro-expand-to-match-arm.stderr
@@ -0,0 +1,10 @@
+error: unexpected `,` in pattern
+  --> $DIR/macro-expand-to-match-arm.rs:11:25
+   |
+LL |         arm!(None => {}),
+   |                         ^
+   |
+   = note: macros cannot expand to match arms
+
+error: aborting due to previous error
+