about summary refs log tree commit diff
path: root/src/test/ui/structs
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-02-23 02:51:44 +0100
committerGitHub <noreply@github.com>2021-02-23 02:51:44 +0100
commit8e51bd4315bad8456c6cabbfc338be97c17f3700 (patch)
tree9ab4c7c343fb46f4b8c6e6cd8ef4ddab138dc806 /src/test/ui/structs
parent72e6d51583ed5fa97ee06fe0259b82b28367874e (diff)
parentd8540ae5a98b6135253521cdbf34c5953494a5bf (diff)
downloadrust-8e51bd4315bad8456c6cabbfc338be97c17f3700.tar.gz
rust-8e51bd4315bad8456c6cabbfc338be97c17f3700.zip
Rollup merge of #81235 - reese:rw-tuple-diagnostics, r=estebank
Improve suggestion for tuple struct pattern matching errors.

Closes #80174

This change allows numbers to be parsed as field names when pattern matching on structs, which allows us to provide better error messages when tuple structs are matched using a struct pattern.

r? ``@estebank``
Diffstat (limited to 'src/test/ui/structs')
-rw-r--r--src/test/ui/structs/struct-tuple-field-names.rs15
-rw-r--r--src/test/ui/structs/struct-tuple-field-names.stderr25
2 files changed, 40 insertions, 0 deletions
diff --git a/src/test/ui/structs/struct-tuple-field-names.rs b/src/test/ui/structs/struct-tuple-field-names.rs
new file mode 100644
index 00000000000..7bd54af1dbe
--- /dev/null
+++ b/src/test/ui/structs/struct-tuple-field-names.rs
@@ -0,0 +1,15 @@
+struct S(i32, f32);
+enum E {
+    S(i32, f32),
+}
+fn main() {
+    let x = E::S(1, 2.2);
+    match x {
+        E::S { 0, 1 } => {}
+        //~^ ERROR tuple variant `E::S` written as struct variant [E0769]
+    }
+    let y = S(1, 2.2);
+    match y {
+        S { } => {} //~ ERROR: tuple variant `S` written as struct variant [E0769]
+    }
+}
diff --git a/src/test/ui/structs/struct-tuple-field-names.stderr b/src/test/ui/structs/struct-tuple-field-names.stderr
new file mode 100644
index 00000000000..29e72146521
--- /dev/null
+++ b/src/test/ui/structs/struct-tuple-field-names.stderr
@@ -0,0 +1,25 @@
+error[E0769]: tuple variant `E::S` written as struct variant
+  --> $DIR/struct-tuple-field-names.rs:8:9
+   |
+LL |         E::S { 0, 1 } => {}
+   |         ^^^^^^^^^^^^^
+   |
+help: use the tuple variant pattern syntax instead
+   |
+LL |         E::S(_, _) => {}
+   |             ^^^^^^
+
+error[E0769]: tuple variant `S` written as struct variant
+  --> $DIR/struct-tuple-field-names.rs:13:9
+   |
+LL |         S { } => {}
+   |         ^^^^^
+   |
+help: use the tuple variant pattern syntax instead
+   |
+LL |         S(_, _) => {}
+   |          ^^^^^^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0769`.