Skip to content

Commit 0003280

Browse files
terrarier2111nagisa
andcommitted
Fix an ICE when lowering a float with missing exponent magnitude
Co-authored-by: Simonas Kazlauskas <github@kazlauskas.me>
1 parent 2af5c65 commit 0003280

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

compiler/rustc_mir_build/src/thir/constant.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ 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)) => parse_float(*n, *fty, neg),
49+
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
50+
parse_float(*n, *fty, neg).ok_or(LitToConstError::Reported)?
51+
}
5052
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
5153
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
5254
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
@@ -55,14 +57,15 @@ crate fn lit_to_const<'tcx>(
5557
Ok(ty::Const::from_value(tcx, lit, ty))
5658
}
5759

58-
fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tcx> {
60+
fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Option<ConstValue<'tcx>> {
5961
let num = num.as_str();
6062
use rustc_apfloat::ieee::{Double, Single};
6163
let scalar = match fty {
6264
ty::FloatTy::F32 => {
63-
let rust_f = num
64-
.parse::<f32>()
65-
.unwrap_or_else(|e| panic!("f32 failed to parse `{}`: {:?}", num, e));
65+
let rust_f = match num.parse::<f32>() {
66+
Ok(f) => f,
67+
Err(_) => return None,
68+
};
6669
let mut f = num.parse::<Single>().unwrap_or_else(|e| {
6770
panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
6871
});
@@ -82,9 +85,10 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
8285
Scalar::from_f32(f)
8386
}
8487
ty::FloatTy::F64 => {
85-
let rust_f = num
86-
.parse::<f64>()
87-
.unwrap_or_else(|e| panic!("f64 failed to parse `{}`: {:?}", num, e));
88+
let rust_f = match num.parse::<f64>() {
89+
Ok(f) => f,
90+
Err(_) => return None,
91+
};
8892
let mut f = num.parse::<Double>().unwrap_or_else(|e| {
8993
panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
9094
});
@@ -105,5 +109,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
105109
}
106110
};
107111

108-
ConstValue::Scalar(scalar)
112+
Some(ConstValue::Scalar(scalar))
109113
}

src/test/ui/consts/issue-91434.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
[9; [[9E; h]]];
3+
//~^ ERROR: expected at least one digit in exponent
4+
//~| ERROR: cannot find value `h` in this scope [E0425]
5+
//~| ERROR: constant expression depends on a generic parameter
6+
}

src/test/ui/consts/issue-91434.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: expected at least one digit in exponent
2+
--> $DIR/issue-91434.rs:2:11
3+
|
4+
LL | [9; [[9E; h]]];
5+
| ^^
6+
7+
error[E0425]: cannot find value `h` in this scope
8+
--> $DIR/issue-91434.rs:2:15
9+
|
10+
LL | [9; [[9E; h]]];
11+
| ^ not found in this scope
12+
13+
error: constant expression depends on a generic parameter
14+
--> $DIR/issue-91434.rs:2:9
15+
|
16+
LL | [9; [[9E; h]]];
17+
| ^^^^^^^^^
18+
|
19+
= note: this may fail depending on what value the parameter takes
20+
21+
error: aborting due to 3 previous errors
22+
23+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)