Skip to content

Commit 8752b40

Browse files
committed
Changed dec2flt to use the Eisel-Lemire algorithm.
Implementation is based off fast-float-rust, with a few notable changes. - Some unsafe methods have been removed. - Safe methods with inherently unsafe functionality have been removed. - All unsafe functionality is documented and provably safe. - Extensive documentation has been added for simpler maintenance. - Inline annotations on internal routines has been removed. - Fixed Python errors in src/etc/test-float-parse/runtests.py. - Updated test-float-parse to be a library, to avoid missing rand dependency. - Added regression tests for #31109 and #31407 in core tests. - Added regression tests for #31109 and #31407 in ui tests. - Use the existing slice primitive to simplify shared dec2flt methods - Remove Miri ignores from dec2flt, due to faster parsing times. - resolves #85198 - resolves #85214 - resolves #85234 - fixes #31407 - fixes #31109 - fixes #53015 - resolves #68396 - closes aldanor/fast-float-rust#15
1 parent d2b04f0 commit 8752b40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2530
-2823
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ pub enum LitToConstError {
172172
/// This is used for graceful error handling (`delay_span_bug`) in
173173
/// type checking (`Const::from_anon_const`).
174174
TypeError,
175-
UnparseableFloat,
176175
Reported,
177176
}
178177

compiler/rustc_mir_build/src/thir/constant.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ crate fn lit_to_const<'tcx>(
4646
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
4747
trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
4848
}
49-
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
50-
parse_float(*n, *fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
51-
}
49+
(ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float(*n, *fty, neg),
5250
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
5351
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
5452
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
@@ -57,12 +55,14 @@ crate fn lit_to_const<'tcx>(
5755
Ok(ty::Const::from_value(tcx, lit, ty))
5856
}
5957

60-
fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstValue<'tcx>, ()> {
58+
fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tcx> {
6159
let num = num.as_str();
6260
use rustc_apfloat::ieee::{Double, Single};
6361
let scalar = match fty {
6462
ty::FloatTy::F32 => {
65-
let rust_f = num.parse::<f32>().map_err(|_| ())?;
63+
let rust_f = num
64+
.parse::<f32>()
65+
.unwrap_or_else(|e| panic!("f32 failed to parse `{}`: {:?}", num, e));
6666
let mut f = num.parse::<Single>().unwrap_or_else(|e| {
6767
panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
6868
});
@@ -82,7 +82,9 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstVa
8282
Scalar::from_f32(f)
8383
}
8484
ty::FloatTy::F64 => {
85-
let rust_f = num.parse::<f64>().map_err(|_| ())?;
85+
let rust_f = num
86+
.parse::<f64>()
87+
.unwrap_or_else(|e| panic!("f64 failed to parse `{}`: {:?}", num, e));
8688
let mut f = num.parse::<Double>().unwrap_or_else(|e| {
8789
panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
8890
});
@@ -103,5 +105,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstVa
103105
}
104106
};
105107

106-
Ok(ConstValue::Scalar(scalar))
108+
ConstValue::Scalar(scalar)
107109
}

compiler/rustc_mir_build/src/thir/cx/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ impl<'tcx> Cx<'tcx> {
6767

6868
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) {
6969
Ok(c) => c,
70-
Err(LitToConstError::UnparseableFloat) => {
71-
// FIXME(#31407) this is only necessary because float parsing is buggy
72-
self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
73-
// create a dummy value and continue compiling
74-
self.tcx.const_error(ty)
75-
}
7670
Err(LitToConstError::Reported) => {
7771
// create a dummy value and continue compiling
7872
self.tcx.const_error(ty)

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> {
8484
}
8585

8686
impl PatCtxt<'_, '_> {
87-
fn report_inlining_errors(&self, pat_span: Span) {
87+
fn report_inlining_errors(&self) {
8888
for error in &self.errors {
8989
match *error {
9090
PatternError::StaticInPattern(span) => {
@@ -96,14 +96,6 @@ impl PatCtxt<'_, '_> {
9696
PatternError::ConstParamInPattern(span) => {
9797
self.span_e0158(span, "const parameters cannot be referenced in patterns")
9898
}
99-
PatternError::FloatBug => {
100-
// FIXME(#31407) this is only necessary because float parsing is buggy
101-
rustc_middle::mir::interpret::struct_error(
102-
self.tcx.at(pat_span),
103-
"could not evaluate float literal (see issue #31407)",
104-
)
105-
.emit();
106-
}
10799
PatternError::NonConstPath(span) => {
108100
rustc_middle::mir::interpret::struct_error(
109101
self.tcx.at(span),
@@ -142,7 +134,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
142134
let pattern: &_ = cx.pattern_arena.alloc(expand_pattern(pattern));
143135
if !patcx.errors.is_empty() {
144136
*have_errors = true;
145-
patcx.report_inlining_errors(pat.span);
137+
patcx.report_inlining_errors();
146138
}
147139
(pattern, pattern_ty)
148140
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ crate enum PatternError {
3131
AssocConstInPattern(Span),
3232
ConstParamInPattern(Span),
3333
StaticInPattern(Span),
34-
FloatBug,
3534
NonConstPath(Span),
3635
}
3736

@@ -563,10 +562,6 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
563562
LitToConstInput { lit: &lit.node, ty: self.typeck_results.expr_ty(expr), neg };
564563
match self.tcx.at(expr.span).lit_to_const(lit_input) {
565564
Ok(val) => *self.const_to_pat(val, expr.hir_id, lit.span, false).kind,
566-
Err(LitToConstError::UnparseableFloat) => {
567-
self.errors.push(PatternError::FloatBug);
568-
PatKind::Wild
569-
}
570565
Err(LitToConstError::Reported) => PatKind::Wild,
571566
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
572567
}

0 commit comments

Comments
 (0)