Skip to content

Commit 354a5cb

Browse files
author
Robin Kruppe
committed
Make trans const eval error on overflow and NaN, matching HIR const eval.
1 parent e999e7b commit 354a5cb

File tree

6 files changed

+154
-80
lines changed

6 files changed

+154
-80
lines changed

src/librustc_apfloat/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Status {
9696
}
9797

9898
impl<T> StatusAnd<T> {
99-
fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
99+
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> {
100100
StatusAnd {
101101
status: self.status,
102102
value: f(self.value),

src/librustc_llvm/ffi.rs

-2
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,6 @@ extern "C" {
628628
pub fn LLVMConstIntGetSExtValue(ConstantVal: ValueRef) -> c_longlong;
629629
pub fn LLVMRustConstInt128Get(ConstantVal: ValueRef, SExt: bool,
630630
high: *mut u64, low: *mut u64) -> bool;
631-
pub fn LLVMRustIsConstantFP(ConstantVal: ValueRef) -> bool;
632-
pub fn LLVMRustConstFloatGetBits(ConstantVal: ValueRef) -> u64;
633631

634632

635633
// Operations on composite constants

src/librustc_trans/mir/constant.rs

+40-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
2121
use rustc::ty::layout::{self, LayoutTyper};
2222
use rustc::ty::cast::{CastTy, IntTy};
2323
use rustc::ty::subst::{Kind, Substs, Subst};
24-
use rustc_apfloat::{ieee, Float};
24+
use rustc_apfloat::{ieee, Float, Status};
2525
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2626
use {adt, base, machine};
2727
use abi::{self, Abi};
@@ -690,16 +690,18 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
690690
llvm::LLVMConstIntCast(llval, ll_t_out.to_ref(), s)
691691
}
692692
(CastTy::Int(_), CastTy::Float) => {
693-
const_cast_int_to_float(self.ccx, llval, signed, ll_t_out)
693+
cast_const_int_to_float(self.ccx, llval, signed, ll_t_out)
694694
}
695695
(CastTy::Float, CastTy::Float) => {
696696
llvm::LLVMConstFPCast(llval, ll_t_out.to_ref())
697697
}
698698
(CastTy::Float, CastTy::Int(IntTy::I)) => {
699-
const_cast_from_float(&operand, true, ll_t_out)
699+
cast_const_float_to_int(self.ccx, &operand,
700+
true, ll_t_out, span)
700701
}
701702
(CastTy::Float, CastTy::Int(_)) => {
702-
const_cast_from_float(&operand, false, ll_t_out)
703+
cast_const_float_to_int(self.ccx, &operand,
704+
false, ll_t_out, span)
703705
}
704706
(CastTy::Ptr(_), CastTy::Ptr(_)) |
705707
(CastTy::FnPtr, CastTy::Ptr(_)) |
@@ -952,36 +954,49 @@ pub fn const_scalar_checked_binop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
952954
}
953955
}
954956

955-
unsafe fn const_cast_from_float(operand: &Const, signed: bool, int_ty: Type) -> ValueRef {
957+
unsafe fn cast_const_float_to_int(ccx: &CrateContext,
958+
operand: &Const,
959+
signed: bool,
960+
int_ty: Type,
961+
span: Span) -> ValueRef {
956962
let llval = operand.llval;
957-
// Note: this breaks if addresses can be turned into integers (is that possible?)
958-
// But at least an ICE is better than producing undef.
959-
assert!(llvm::LLVMRustIsConstantFP(llval),
960-
"const_cast_from_float: invalid llval {:?}", Value(llval));
961-
let bits = llvm::LLVMRustConstFloatGetBits(llval) as u128;
962-
let int_width = int_ty.int_width() as usize;
963963
let float_bits = match operand.ty.sty {
964964
ty::TyFloat(fty) => fty.bit_width(),
965-
_ => bug!("const_cast_from_float: operand not a float"),
965+
_ => bug!("cast_const_float_to_int: operand not a float"),
966966
};
967-
// Ignore the Status, to_i128 does the Right Thing(tm) on overflow and NaN even though it
968-
// sets INVALID_OP.
967+
// Note: this breaks if llval is a complex constant expression rather than a simple constant.
968+
// One way that might happen would be if addresses could be turned into integers in constant
969+
// expressions, but that doesn't appear to be possible?
970+
// In any case, an ICE is better than producing undef.
971+
let llval_bits = consts::bitcast(llval, Type::ix(ccx, float_bits as u64));
972+
let bits = const_to_opt_u128(llval_bits, false).unwrap_or_else(|| {
973+
panic!("could not get bits of constant float {:?}",
974+
Value(llval));
975+
});
976+
let int_width = int_ty.int_width() as usize;
977+
// Try to convert, but report an error for overflow and NaN. This matches HIR const eval.
969978
let cast_result = match float_bits {
970-
32 if signed => ieee::Single::from_bits(bits).to_i128(int_width).value as u128,
971-
64 if signed => ieee::Double::from_bits(bits).to_i128(int_width).value as u128,
972-
32 => ieee::Single::from_bits(bits).to_u128(int_width).value,
973-
64 => ieee::Double::from_bits(bits).to_u128(int_width).value,
979+
32 if signed => ieee::Single::from_bits(bits).to_i128(int_width).map(|v| v as u128),
980+
64 if signed => ieee::Double::from_bits(bits).to_i128(int_width).map(|v| v as u128),
981+
32 => ieee::Single::from_bits(bits).to_u128(int_width),
982+
64 => ieee::Double::from_bits(bits).to_u128(int_width),
974983
n => bug!("unsupported float width {}", n),
975984
};
976-
C_big_integral(int_ty, cast_result)
985+
if cast_result.status.contains(Status::INVALID_OP) {
986+
let err = ConstEvalErr { span: span, kind: ErrKind::CannotCast };
987+
err.report(ccx.tcx(), span, "expression");
988+
}
989+
C_big_integral(int_ty, cast_result.value)
977990
}
978991

979-
unsafe fn const_cast_int_to_float(ccx: &CrateContext,
980-
llval: ValueRef,
981-
signed: bool,
982-
float_ty: Type) -> ValueRef {
983-
// Note: this breaks if addresses can be turned into integers (is that possible?)
984-
// But at least an ICE is better than producing undef.
992+
unsafe fn cast_const_int_to_float(ccx: &CrateContext,
993+
llval: ValueRef,
994+
signed: bool,
995+
float_ty: Type) -> ValueRef {
996+
// Note: this breaks if llval is a complex constant expression rather than a simple constant.
997+
// One way that might happen would be if addresses could be turned into integers in constant
998+
// expressions, but that doesn't appear to be possible?
999+
// In any case, an ICE is better than producing undef.
9851000
let value = const_to_opt_u128(llval, signed).unwrap_or_else(|| {
9861001
panic!("could not get z128 value of constant integer {:?}",
9871002
Value(llval));

src/rustllvm/RustWrapper.cpp

-13
Original file line numberDiff line numberDiff line change
@@ -1373,19 +1373,6 @@ extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *hig
13731373
return true;
13741374
}
13751375

1376-
extern "C" uint64_t LLVMRustConstFloatGetBits(LLVMValueRef CV) {
1377-
auto C = unwrap<llvm::ConstantFP>(CV);
1378-
APInt Bits = C->getValueAPF().bitcastToAPInt();
1379-
if (!Bits.isIntN(64)) {
1380-
report_fatal_error("Float bit pattern >64 bits");
1381-
}
1382-
return Bits.getLimitedValue();
1383-
}
1384-
1385-
extern "C" bool LLVMRustIsConstantFP(LLVMValueRef CV) {
1386-
return isa<llvm::ConstantFP>(unwrap<llvm::Value>(CV));
1387-
}
1388-
13891376
extern "C" LLVMContextRef LLVMRustGetValueContext(LLVMValueRef V) {
13901377
return wrap(&unwrap(V)->getContext());
13911378
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(i128_type)]
12+
#![allow(const_err)] // this test is only about hard errors
13+
14+
use std::{f32, f64};
15+
16+
// Forces evaluation of constants, triggering hard error
17+
fn force<T>(_: T) {}
18+
19+
fn main() {
20+
{ const X: u16 = -1. as u16; force(X); } //~ ERROR constant evaluation error
21+
{ const X: u128 = -100. as u128; force(X); } //~ ERROR constant evaluation error
22+
23+
{ const X: i8 = f32::NAN as i8; force(X); } //~ ERROR constant evaluation error
24+
{ const X: i32 = f32::NAN as i32; force(X); } //~ ERROR constant evaluation error
25+
{ const X: u64 = f32::NAN as u64; force(X); } //~ ERROR constant evaluation error
26+
{ const X: u128 = f32::NAN as u128; force(X); } //~ ERROR constant evaluation error
27+
28+
{ const X: i8 = f32::INFINITY as i8; force(X); } //~ ERROR constant evaluation error
29+
{ const X: u32 = f32::INFINITY as u32; force(X); } //~ ERROR constant evaluation error
30+
{ const X: i128 = f32::INFINITY as i128; force(X); } //~ ERROR constant evaluation error
31+
{ const X: u128 = f32::INFINITY as u128; force(X); } //~ ERROR constant evaluation error
32+
33+
{ const X: u8 = f32::NEG_INFINITY as u8; force(X); } //~ ERROR constant evaluation error
34+
{ const X: u16 = f32::NEG_INFINITY as u16; force(X); } //~ ERROR constant evaluation error
35+
{ const X: i64 = f32::NEG_INFINITY as i64; force(X); } //~ ERROR constant evaluation error
36+
{ const X: i128 = f32::NEG_INFINITY as i128; force(X); } //~ ERROR constant evaluation error
37+
38+
{ const X: i8 = f64::NAN as i8; force(X); } //~ ERROR constant evaluation error
39+
{ const X: i32 = f64::NAN as i32; force(X); } //~ ERROR constant evaluation error
40+
{ const X: u64 = f64::NAN as u64; force(X); } //~ ERROR constant evaluation error
41+
{ const X: u128 = f64::NAN as u128; force(X); } //~ ERROR constant evaluation error
42+
43+
{ const X: i8 = f64::INFINITY as i8; force(X); } //~ ERROR constant evaluation error
44+
{ const X: u32 = f64::INFINITY as u32; force(X); } //~ ERROR constant evaluation error
45+
{ const X: i128 = f64::INFINITY as i128; force(X); } //~ ERROR constant evaluation error
46+
{ const X: u128 = f64::INFINITY as u128; force(X); } //~ ERROR constant evaluation error
47+
48+
{ const X: u8 = f64::NEG_INFINITY as u8; force(X); } //~ ERROR constant evaluation error
49+
{ const X: u16 = f64::NEG_INFINITY as u16; force(X); } //~ ERROR constant evaluation error
50+
{ const X: i64 = f64::NEG_INFINITY as i64; force(X); } //~ ERROR constant evaluation error
51+
{ const X: i128 = f64::NEG_INFINITY as i128; force(X); } //~ ERROR constant evaluation error
52+
53+
{ const X: u8 = 256. as u8; force(X); } //~ ERROR constant evaluation error
54+
{ const X: i8 = -129. as i8; force(X); } //~ ERROR constant evaluation error
55+
{ const X: i8 = 128. as i8; force(X); } //~ ERROR constant evaluation error
56+
{ const X: i32 = 2147483648. as i32; force(X); } //~ ERROR constant evaluation error
57+
{ const X: i32 = -2147483904. as i32; force(X); } //~ ERROR constant evaluation error
58+
{ const X: u32 = 4294967296. as u32; force(X); } //~ ERROR constant evaluation error
59+
{ const X: u128 = 1e40 as u128; force(X); } //~ ERROR constant evaluation error
60+
{ const X: i128 = 1e40 as i128; force(X); } //~ ERROR constant evaluation error
61+
}

src/test/run-pass/saturating-float-casts.rs

+52-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -22,15 +22,28 @@ macro_rules! test {
2222
($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => (
2323
// black_box disables constant evaluation to test run-time conversions:
2424
assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
25-
"run time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
26-
// ... whereas this variant triggers constant evaluation:
25+
"run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
26+
);
27+
28+
($fval:expr, f* -> $ity:ident, $ival:expr) => (
29+
test!($fval, f32 -> $ity, $ival);
30+
test!($fval, f64 -> $ity, $ival);
31+
)
32+
}
33+
34+
// This macro tests const eval in addition to run-time evaluation.
35+
// If and when saturating casts are adopted, this macro should be merged with test!() to ensure
36+
// that run-time and const eval agree on inputs that currently trigger a const eval error.
37+
macro_rules! test_c {
38+
($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
39+
test!($val, $src_ty -> $dest_ty, $expected);
2740
{
2841
const X: $src_ty = $val;
2942
const Y: $dest_ty = X as $dest_ty;
3043
assert_eq!(Y, $expected,
3144
"const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
3245
}
33-
);
46+
});
3447

3548
($fval:expr, f* -> $ity:ident, $ival:expr) => (
3649
test!($fval, f32 -> $ity, $ival);
@@ -48,11 +61,11 @@ macro_rules! common_fptoi_tests {
4861
// as well, the test is just slightly misplaced.
4962
test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
5063
test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
51-
test!(0., $fty -> $ity, 0);
52-
test!($fty::MIN_POSITIVE, $fty -> $ity, 0);
64+
test_c!(0., $fty -> $ity, 0);
65+
test_c!($fty::MIN_POSITIVE, $fty -> $ity, 0);
5366
test!(-0.9, $fty -> $ity, 0);
54-
test!(1., $fty -> $ity, 1);
55-
test!(42., $fty -> $ity, 42);
67+
test_c!(1., $fty -> $ity, 1);
68+
test_c!(42., $fty -> $ity, 42);
5669
)+ });
5770

5871
(f* -> $($ity:ident)+) => ({
@@ -84,58 +97,58 @@ pub fn main() {
8497

8598
// The following tests cover edge cases for some integer types.
8699

87-
// u8
88-
test!(254., f* -> u8, 254);
100+
// # u8
101+
test_c!(254., f* -> u8, 254);
89102
test!(256., f* -> u8, 255);
90103

91-
// i8
92-
test!(-127., f* -> i8, -127);
104+
// # i8
105+
test_c!(-127., f* -> i8, -127);
93106
test!(-129., f* -> i8, -128);
94-
test!(126., f* -> i8, 126);
107+
test_c!(126., f* -> i8, 126);
95108
test!(128., f* -> i8, 127);
96109

97-
// i32
110+
// # i32
98111
// -2147483648. is i32::MIN (exactly)
99-
test!(-2147483648., f* -> i32, i32::MIN);
112+
test_c!(-2147483648., f* -> i32, i32::MIN);
100113
// 2147483648. is i32::MAX rounded up
101114
test!(2147483648., f32 -> i32, 2147483647);
102115
// With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
103116
// multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
104-
test!(2147483520., f32 -> i32, 2147483520);
117+
test_c!(2147483520., f32 -> i32, 2147483520);
105118
// Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
106119
test!(-2147483904., f* -> i32, i32::MIN);
107-
test!(-2147483520., f* -> i32, -2147483520);
120+
test_c!(-2147483520., f* -> i32, -2147483520);
108121

109-
// u32 -- round(MAX) and nextUp(round(MAX))
110-
test!(4294967040., f* -> u32, 4294967040);
122+
// # u32
123+
// round(MAX) and nextUp(round(MAX))
124+
test_c!(4294967040., f* -> u32, 4294967040);
111125
test!(4294967296., f* -> u32, 4294967295);
112126

113-
// u128
114-
// # float->int
115-
test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
127+
// # u128
128+
// float->int:
129+
test_c!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
116130
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
117131
const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
118-
test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
119-
// # int->float
132+
test_c!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
133+
134+
// int->float:
120135
// f32::MAX - 0.5 ULP and smaller should be rounded down
121-
test!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
122-
test!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
123-
test!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
136+
test_c!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
137+
test_c!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
138+
test_c!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
124139
// numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
125-
test!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
126-
test!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
127-
test!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
128-
test!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
129-
test!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
140+
test_c!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
141+
test_c!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
142+
test_c!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
143+
test_c!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
144+
test_c!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
130145
// f32::MAX + 0.5 ULP and greater should be rounded to infinity
131-
test!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
132-
test!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
133-
test!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
134-
135-
test!(!0, u128 -> f32, f32::INFINITY);
146+
test_c!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
147+
test_c!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
148+
test_c!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
136149

137150
// u128->f64 should not be affected by the u128->f32 checks
138-
test!(0xffffff80000000000000000000000000, u128 -> f64,
151+
test_c!(0xffffff80000000000000000000000000, u128 -> f64,
139152
340282356779733661637539395458142568448.0);
140-
test!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);
153+
test_c!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);
141154
}

0 commit comments

Comments
 (0)