about summary refs log tree commit diff
path: root/src/test/ui
diff options
context:
space:
mode:
authorDylan MacKenzie <ecstaticmorse@gmail.com>2020-04-07 12:11:29 -0700
committerDylan MacKenzie <ecstaticmorse@gmail.com>2020-04-28 14:58:50 -0700
commitb58da533bc0fc6afd61eafaf3860b9934c8120b8 (patch)
tree5152c0f9e51a60139ecea1235ab726bc881cedaa /src/test/ui
parent135cfcb5cd563e8beec78d86d8afc40321c9c7da (diff)
downloadrust-b58da533bc0fc6afd61eafaf3860b9934c8120b8.tar.gz
rust-b58da533bc0fc6afd61eafaf3860b9934c8120b8.zip
Add branchy `const` in pattern tests
Diffstat (limited to 'src/test/ui')
-rw-r--r--src/test/ui/consts/const_in_pattern/custom-eq-branch-pass.rs33
-rw-r--r--src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.rs39
-rw-r--r--src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr16
-rw-r--r--src/test/ui/consts/const_in_pattern/no-eq-branch-fail.rs27
-rw-r--r--src/test/ui/consts/const_in_pattern/no-eq-branch-fail.stderr14
5 files changed, 129 insertions, 0 deletions
diff --git a/src/test/ui/consts/const_in_pattern/custom-eq-branch-pass.rs b/src/test/ui/consts/const_in_pattern/custom-eq-branch-pass.rs
new file mode 100644
index 00000000000..81a2024a81b
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/custom-eq-branch-pass.rs
@@ -0,0 +1,33 @@
+// run-pass
+
+#![feature(const_if_match)]
+#![warn(indirect_structural_match)]
+
+struct CustomEq;
+
+impl Eq for CustomEq {}
+impl PartialEq for CustomEq {
+    fn eq(&self, _: &Self) -> bool {
+        false
+    }
+}
+
+#[derive(PartialEq, Eq)]
+enum Foo {
+    Bar,
+    Baz,
+    Qux(CustomEq),
+}
+
+const BAR_BAZ: Foo = if 42 == 42 {
+    Foo::Bar
+} else {
+    Foo::Baz
+};
+
+fn main() {
+    match Foo::Qux(CustomEq) {
+        BAR_BAZ => panic!(),
+        _ => {}
+    }
+}
diff --git a/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.rs b/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.rs
new file mode 100644
index 00000000000..21c4de6fbb1
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.rs
@@ -0,0 +1,39 @@
+// check-pass
+
+#![feature(const_if_match)]
+#![warn(indirect_structural_match)]
+//~^ NOTE lint level is defined here
+
+struct CustomEq;
+
+impl Eq for CustomEq {}
+impl PartialEq for CustomEq {
+    fn eq(&self, _: &Self) -> bool {
+        false
+    }
+}
+
+#[derive(PartialEq, Eq)]
+enum Foo {
+    Bar,
+    Baz,
+    Qux(CustomEq),
+}
+
+// We know that `BAR_BAZ` will always be `Foo::Bar` and thus eligible for structural matching, but
+// dataflow will be more conservative.
+const BAR_BAZ: Foo = if 42 == 42 {
+    Foo::Bar
+} else {
+    Foo::Qux(CustomEq)
+};
+
+fn main() {
+    match Foo::Qux(CustomEq) {
+        BAR_BAZ => panic!(),
+        //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
+        //~| WARN this was previously accepted
+        //~| NOTE see issue #62411
+        _ => {}
+    }
+}
diff --git a/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr b/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr
new file mode 100644
index 00000000000..06ec2a7fdd3
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr
@@ -0,0 +1,16 @@
+warning: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
+  --> $DIR/custom-eq-branch-warn.rs:33:9
+   |
+LL |         BAR_BAZ => panic!(),
+   |         ^^^^^^^
+   |
+note: the lint level is defined here
+  --> $DIR/custom-eq-branch-warn.rs:4:9
+   |
+LL | #![warn(indirect_structural_match)]
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
+
+warning: 1 warning emitted
+
diff --git a/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.rs b/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.rs
new file mode 100644
index 00000000000..28b3fbb9525
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.rs
@@ -0,0 +1,27 @@
+#![feature(const_if_match)]
+#![warn(indirect_structural_match)]
+
+struct NoEq;
+
+enum Foo {
+    Bar,
+    Baz,
+    Qux(NoEq),
+}
+
+// Even though any of these values can be compared structurally, we still disallow it in a pattern
+// because `Foo` does not impl `PartialEq`.
+const BAR_BAZ: Foo = if 42 == 42 {
+    Foo::Baz
+} else {
+    Foo::Bar
+};
+
+fn main() {
+    match Foo::Qux(NoEq) {
+        BAR_BAZ => panic!(),
+        //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
+        //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
+        _ => {}
+    }
+}
diff --git a/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.stderr b/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.stderr
new file mode 100644
index 00000000000..cb870ec7dbc
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/no-eq-branch-fail.stderr
@@ -0,0 +1,14 @@
+error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
+  --> $DIR/no-eq-branch-fail.rs:22:9
+   |
+LL |         BAR_BAZ => panic!(),
+   |         ^^^^^^^
+
+error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
+  --> $DIR/no-eq-branch-fail.rs:22:9
+   |
+LL |         BAR_BAZ => panic!(),
+   |         ^^^^^^^
+
+error: aborting due to 2 previous errors
+