Skip to content

Commit 9f14fc4

Browse files
committed
add test checking behavior of matching on floats, and NaNs in consts
1 parent 1254ee4 commit 9f14fc4

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

tests/ui/match/match-float.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-pass
2+
// Makes sure we use `==` (not bitwise) semantics for float comparison.
3+
4+
fn main() {
5+
const F1: f32 = 0.0;
6+
const F2: f32 = -0.0;
7+
assert_eq!(F1, F2);
8+
assert_ne!(F1.to_bits(), F2.to_bits());
9+
assert!(matches!(F1, F2));
10+
assert!(matches!(F2, F1));
11+
}

tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/issue-6804-nan-match.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
const NAN: f64 = f64::NAN;
66

7+
#[derive(PartialEq, Eq)]
8+
struct MyType<T>(T);
9+
10+
const C: MyType<f32> = MyType(f32::NAN);
11+
712
fn main() {
813
let x = NAN;
914
match x {
@@ -16,6 +21,11 @@ fn main() {
1621
_ => {},
1722
};
1823

24+
match MyType(1.0f32) {
25+
C => {}, //~ ERROR cannot use NaN in patterns
26+
_ => {},
27+
}
28+
1929
// Also cover range patterns
2030
match x {
2131
NAN..=1.0 => {}, //~ ERROR cannot use NaN in patterns
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: cannot use NaN in patterns
2-
--> $DIR/issue-6804-nan-match.rs:10:9
2+
--> $DIR/issue-6804-nan-match.rs:15:9
33
|
44
LL | NAN => {},
55
| ^^^
@@ -8,7 +8,7 @@ LL | NAN => {},
88
= help: try using the `is_nan` method instead
99

1010
error: cannot use NaN in patterns
11-
--> $DIR/issue-6804-nan-match.rs:15:10
11+
--> $DIR/issue-6804-nan-match.rs:20:10
1212
|
1313
LL | [NAN, _] => {},
1414
| ^^^
@@ -17,7 +17,16 @@ LL | [NAN, _] => {},
1717
= help: try using the `is_nan` method instead
1818

1919
error: cannot use NaN in patterns
20-
--> $DIR/issue-6804-nan-match.rs:21:9
20+
--> $DIR/issue-6804-nan-match.rs:25:9
21+
|
22+
LL | C => {},
23+
| ^
24+
|
25+
= note: NaNs compare inequal to everything, even themselves, so this pattern would never match
26+
= help: try using the `is_nan` method instead
27+
28+
error: cannot use NaN in patterns
29+
--> $DIR/issue-6804-nan-match.rs:31:9
2130
|
2231
LL | NAN..=1.0 => {},
2332
| ^^^
@@ -26,13 +35,13 @@ LL | NAN..=1.0 => {},
2635
= help: try using the `is_nan` method instead
2736

2837
error[E0030]: lower range bound must be less than or equal to upper
29-
--> $DIR/issue-6804-nan-match.rs:21:9
38+
--> $DIR/issue-6804-nan-match.rs:31:9
3039
|
3140
LL | NAN..=1.0 => {},
3241
| ^^^^^^^^^ lower bound larger than upper bound
3342

3443
error: cannot use NaN in patterns
35-
--> $DIR/issue-6804-nan-match.rs:23:16
44+
--> $DIR/issue-6804-nan-match.rs:33:16
3645
|
3746
LL | -1.0..=NAN => {},
3847
| ^^^
@@ -41,13 +50,13 @@ LL | -1.0..=NAN => {},
4150
= help: try using the `is_nan` method instead
4251

4352
error[E0030]: lower range bound must be less than or equal to upper
44-
--> $DIR/issue-6804-nan-match.rs:23:9
53+
--> $DIR/issue-6804-nan-match.rs:33:9
4554
|
4655
LL | -1.0..=NAN => {},
4756
| ^^^^^^^^^^ lower bound larger than upper bound
4857

4958
error: cannot use NaN in patterns
50-
--> $DIR/issue-6804-nan-match.rs:25:9
59+
--> $DIR/issue-6804-nan-match.rs:35:9
5160
|
5261
LL | NAN.. => {},
5362
| ^^^
@@ -56,13 +65,13 @@ LL | NAN.. => {},
5665
= help: try using the `is_nan` method instead
5766

5867
error[E0030]: lower range bound must be less than or equal to upper
59-
--> $DIR/issue-6804-nan-match.rs:25:9
68+
--> $DIR/issue-6804-nan-match.rs:35:9
6069
|
6170
LL | NAN.. => {},
6271
| ^^^^^ lower bound larger than upper bound
6372

6473
error: cannot use NaN in patterns
65-
--> $DIR/issue-6804-nan-match.rs:27:11
74+
--> $DIR/issue-6804-nan-match.rs:37:11
6675
|
6776
LL | ..NAN => {},
6877
| ^^^
@@ -71,12 +80,12 @@ LL | ..NAN => {},
7180
= help: try using the `is_nan` method instead
7281

7382
error[E0579]: lower range bound must be less than upper
74-
--> $DIR/issue-6804-nan-match.rs:27:9
83+
--> $DIR/issue-6804-nan-match.rs:37:9
7584
|
7685
LL | ..NAN => {},
7786
| ^^^^^
7887

79-
error: aborting due to 10 previous errors
88+
error: aborting due to 11 previous errors
8089

8190
Some errors have detailed explanations: E0030, E0579.
8291
For more information about an error, try `rustc --explain E0030`.

0 commit comments

Comments
 (0)