Skip to content

Commit

Permalink
add test checking behavior of matching on floats, and NaNs in consts
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jan 26, 2024
1 parent 1254ee4 commit 9f14fc4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
11 changes: 11 additions & 0 deletions tests/ui/match/match-float.rs
Original file line number Diff line number Diff line change
@@ -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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 => {},
| ^^^
Expand All @@ -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, _] => {},
| ^^^
Expand All @@ -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 => {},
| ^^^
Expand All @@ -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 => {},
| ^^^
Expand All @@ -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.. => {},
| ^^^
Expand All @@ -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 => {},
| ^^^
Expand All @@ -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`.

0 comments on commit 9f14fc4

Please sign in to comment.