Skip to content

Commit 17c52d4

Browse files
committed
add tests for invalid float-to-int casts
1 parent 25c71e5 commit 17c52d4

21 files changed

+210
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, i32>(f32::INFINITY); } //~ ERROR: cannot be represented in target type `i32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, i32>(f32::NEG_INFINITY); } //~ ERROR: cannot be represented in target type `i32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, u32>(f32::NAN); } //~ ERROR: cannot be represented in target type `u32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, u32>(-f32::NAN); } //~ ERROR: cannot be represented in target type `u32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, u32>(-1.000000001f32); } //~ ERROR: cannot be represented in target type `u32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, i32>(2147483648.0f32); } //~ ERROR: cannot be represented in target type `i32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, u32>((u32::MAX-127) as f32); } //~ ERROR: cannot be represented in target type `u32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f32, i32>(-2147483904.0f32); } //~ ERROR: cannot be represented in target type `i32`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f64, u128>(f64::INFINITY); } //~ ERROR: cannot be represented in target type `u128`
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(intrinsics)]
2+
3+
// Directly call intrinsic to avoid debug assertions in libstd
4+
extern "rust-intrinsic" {
5+
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
6+
}
7+
8+
fn main() {
9+
unsafe { float_to_int_unchecked::<f64, u128>(f64::NEG_INFINITY); } //~ ERROR: cannot be represented in target type `u128`
10+
}

0 commit comments

Comments
 (0)