about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-09-23 23:02:42 +0200
committerRalf Jung <post@ralfj.de>2024-01-26 17:25:03 +0100
commit9f14fc4af4554b5ae8b727ae68460777051f1f74 (patch)
tree1ef578a78f5819d9a331297087fd18c99a02c950
parent1254ee48c458a5fe8fc1847539169df013098e05 (diff)
downloadrust-9f14fc4af4554b5ae8b727ae68460777051f1f74.tar.gz
rust-9f14fc4af4554b5ae8b727ae68460777051f1f74.zip
add test checking behavior of matching on floats, and NaNs in consts
-rw-r--r--tests/ui/match/match-float.rs11
-rw-r--r--tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs10
-rw-r--r--tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr31
3 files changed, 41 insertions, 11 deletions
diff --git a/tests/ui/match/match-float.rs b/tests/ui/match/match-float.rs
new file mode 100644
index 00000000000..8da6a9ed204
--- /dev/null
+++ b/tests/ui/match/match-float.rs
@@ -0,0 +1,11 @@
+// run-pass
+// Makes sure we use `==` (not bitwise) semantics for float comparison.
+
+fn main() {
+    const F1: f32 = 0.0;
+    const F2: f32 = -0.0;
+    assert_eq!(F1, F2);
+    assert_ne!(F1.to_bits(), F2.to_bits());
+    assert!(matches!(F1, F2));
+    assert!(matches!(F2, F1));
+}
diff --git a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs
index c8808c7eea9..d43db576b38 100644
--- a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs
+++ b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs
@@ -4,6 +4,11 @@
 
 const NAN: f64 = f64::NAN;
 
+#[derive(PartialEq, Eq)]
+struct MyType<T>(T);
+
+const C: MyType<f32> = MyType(f32::NAN);
+
 fn main() {
     let x = NAN;
     match x {
@@ -16,6 +21,11 @@ fn main() {
         _ => {},
     };
 
+    match MyType(1.0f32) {
+        C => {}, //~ ERROR cannot use NaN in patterns
+        _ => {},
+    }
+
     // Also cover range patterns
     match x {
         NAN..=1.0 => {}, //~ ERROR cannot use NaN in patterns
diff --git a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
index bfcf919fbe8..167ada783c2 100644
--- a/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
+++ b/tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.stderr
@@ -1,5 +1,5 @@
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:10:9
+  --> $DIR/issue-6804-nan-match.rs:15:9
    |
 LL |         NAN => {},
    |         ^^^
@@ -8,7 +8,7 @@ LL |         NAN => {},
    = help: try using the `is_nan` method instead
 
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:15:10
+  --> $DIR/issue-6804-nan-match.rs:20:10
    |
 LL |         [NAN, _] => {},
    |          ^^^
@@ -17,7 +17,16 @@ LL |         [NAN, _] => {},
    = help: try using the `is_nan` method instead
 
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:21:9
+  --> $DIR/issue-6804-nan-match.rs:25:9
+   |
+LL |         C => {},
+   |         ^
+   |
+   = note: NaNs compare inequal to everything, even themselves, so this pattern would never match
+   = help: try using the `is_nan` method instead
+
+error: cannot use NaN in patterns
+  --> $DIR/issue-6804-nan-match.rs:31:9
    |
 LL |         NAN..=1.0 => {},
    |         ^^^
@@ -26,13 +35,13 @@ LL |         NAN..=1.0 => {},
    = help: try using the `is_nan` method instead
 
 error[E0030]: lower range bound must be less than or equal to upper
-  --> $DIR/issue-6804-nan-match.rs:21:9
+  --> $DIR/issue-6804-nan-match.rs:31:9
    |
 LL |         NAN..=1.0 => {},
    |         ^^^^^^^^^ lower bound larger than upper bound
 
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:23:16
+  --> $DIR/issue-6804-nan-match.rs:33:16
    |
 LL |         -1.0..=NAN => {},
    |                ^^^
@@ -41,13 +50,13 @@ LL |         -1.0..=NAN => {},
    = help: try using the `is_nan` method instead
 
 error[E0030]: lower range bound must be less than or equal to upper
-  --> $DIR/issue-6804-nan-match.rs:23:9
+  --> $DIR/issue-6804-nan-match.rs:33:9
    |
 LL |         -1.0..=NAN => {},
    |         ^^^^^^^^^^ lower bound larger than upper bound
 
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:25:9
+  --> $DIR/issue-6804-nan-match.rs:35:9
    |
 LL |         NAN.. => {},
    |         ^^^
@@ -56,13 +65,13 @@ LL |         NAN.. => {},
    = help: try using the `is_nan` method instead
 
 error[E0030]: lower range bound must be less than or equal to upper
-  --> $DIR/issue-6804-nan-match.rs:25:9
+  --> $DIR/issue-6804-nan-match.rs:35:9
    |
 LL |         NAN.. => {},
    |         ^^^^^ lower bound larger than upper bound
 
 error: cannot use NaN in patterns
-  --> $DIR/issue-6804-nan-match.rs:27:11
+  --> $DIR/issue-6804-nan-match.rs:37:11
    |
 LL |         ..NAN => {},
    |           ^^^
@@ -71,12 +80,12 @@ LL |         ..NAN => {},
    = help: try using the `is_nan` method instead
 
 error[E0579]: lower range bound must be less than upper
-  --> $DIR/issue-6804-nan-match.rs:27:9
+  --> $DIR/issue-6804-nan-match.rs:37:9
    |
 LL |         ..NAN => {},
    |         ^^^^^
 
-error: aborting due to 10 previous errors
+error: aborting due to 11 previous errors
 
 Some errors have detailed explanations: E0030, E0579.
 For more information about an error, try `rustc --explain E0030`.