-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #1325 - RalfJung:float_to_int_unchecked, r=RalfJung
implement float_to_int_unchecked @hanna-kruppe would be great if you could have a look at this. `float.rs` tests legal casts. `test_cast` checks that both `as` casts and unchecked casts work (i.e., these are not saturating). The `compile-fail` tests should ensure that illegal casts via the intrinsic are detected as such. Fixes #1264
- Loading branch information
Showing
25 changed files
with
425 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, i32>(f32::INFINITY); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, i32>(f32::NEG_INFINITY); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, u32>(f32::NAN); } //~ ERROR: cannot be represented in target type `u32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, u32>(-f32::NAN); } //~ ERROR: cannot be represented in target type `u32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, u32>(-1.000000001f32); } //~ ERROR: cannot be represented in target type `u32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, i32>(2147483648.0f32); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, u32>((u32::MAX-127) as f32); } //~ ERROR: cannot be represented in target type `u32` | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/compile-fail/intrinsics/float_to_int_32_too_small1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f32, i32>(-2147483904.0f32); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u128>(f64::INFINITY); } //~ ERROR: cannot be represented in target type `u128` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u128>(f64::NEG_INFINITY); } //~ ERROR: cannot be represented in target type `u128` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i128>(f64::NEG_INFINITY); } //~ ERROR: cannot be represented in target type `i128` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u32>(f64::NAN); } //~ ERROR: cannot be represented in target type `u32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u128>(-1.0000000000001f64); } //~ ERROR: cannot be represented in target type `u128` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i32>(2147483648.0f64); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i64>(9223372036854775808.0f64); } //~ ERROR: cannot be represented in target type `i64` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u64>(18446744073709551616.0f64); } //~ ERROR: cannot be represented in target type `u64` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u128>(u128::MAX as f64); } //~ ERROR: cannot be represented in target type `u128` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i128>(240282366920938463463374607431768211455.0f64); } //~ ERROR: cannot be represented in target type `i128` | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/compile-fail/intrinsics/float_to_int_64_too_small1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i32>(-2147483649.0f64); } //~ ERROR: cannot be represented in target type `i32` | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/compile-fail/intrinsics/float_to_int_64_too_small2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i64>(-9223372036854777856.0f64); } //~ ERROR: cannot be represented in target type `i64` | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/compile-fail/intrinsics/float_to_int_64_too_small3.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(intrinsics)] | ||
|
||
// Directly call intrinsic to avoid debug assertions in libstd | ||
extern "rust-intrinsic" { | ||
fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; | ||
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, i128>(-240282366920938463463374607431768211455.0f64); } //~ ERROR: cannot be represented in target type `i128` | ||
} |
Oops, something went wrong.