about summary refs log tree commit diff
diff options
context:
space:
mode:
authorthreadexception <hannes.gaumann@outlook.de>2021-12-11 15:42:25 +0100
committerthreadexception <hannes.gaumann@outlook.de>2021-12-19 11:52:33 +0100
commit0003280b9b4ef53e11b89ef69d0a2c4ffc725459 (patch)
tree8376e03e4d816da40a605bac4c8644a466d5057a
parent2af5c6562deed1878000e791f2cb21b981a53959 (diff)
downloadrust-0003280b9b4ef53e11b89ef69d0a2c4ffc725459.tar.gz
rust-0003280b9b4ef53e11b89ef69d0a2c4ffc725459.zip
Fix an ICE when lowering a float with missing exponent magnitude
Co-authored-by: Simonas Kazlauskas <github@kazlauskas.me>
-rw-r--r--compiler/rustc_mir_build/src/thir/constant.rs22
-rw-r--r--src/test/ui/consts/issue-91434.rs6
-rw-r--r--src/test/ui/consts/issue-91434.stderr23
3 files changed, 42 insertions, 9 deletions
diff --git a/compiler/rustc_mir_build/src/thir/constant.rs b/compiler/rustc_mir_build/src/thir/constant.rs
index f9e7b39f704..9b54db0d7de 100644
--- a/compiler/rustc_mir_build/src/thir/constant.rs
+++ b/compiler/rustc_mir_build/src/thir/constant.rs
@@ -46,7 +46,9 @@ crate fn lit_to_const<'tcx>(
         (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
             trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
         }
-        (ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float(*n, *fty, neg),
+        (ast::LitKind::Float(n, _), ty::Float(fty)) => {
+            parse_float(*n, *fty, neg).ok_or(LitToConstError::Reported)?
+        }
         (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
         (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
         (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
@@ -55,14 +57,15 @@ crate fn lit_to_const<'tcx>(
     Ok(ty::Const::from_value(tcx, lit, ty))
 }
 
-fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tcx> {
+fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Option<ConstValue<'tcx>> {
     let num = num.as_str();
     use rustc_apfloat::ieee::{Double, Single};
     let scalar = match fty {
         ty::FloatTy::F32 => {
-            let rust_f = num
-                .parse::<f32>()
-                .unwrap_or_else(|e| panic!("f32 failed to parse `{}`: {:?}", num, e));
+            let rust_f = match num.parse::<f32>() {
+                Ok(f) => f,
+                Err(_) => return None,
+            };
             let mut f = num.parse::<Single>().unwrap_or_else(|e| {
                 panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
             });
@@ -82,9 +85,10 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
             Scalar::from_f32(f)
         }
         ty::FloatTy::F64 => {
-            let rust_f = num
-                .parse::<f64>()
-                .unwrap_or_else(|e| panic!("f64 failed to parse `{}`: {:?}", num, e));
+            let rust_f = match num.parse::<f64>() {
+                Ok(f) => f,
+                Err(_) => return None,
+            };
             let mut f = num.parse::<Double>().unwrap_or_else(|e| {
                 panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
             });
@@ -105,5 +109,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tc
         }
     };
 
-    ConstValue::Scalar(scalar)
+    Some(ConstValue::Scalar(scalar))
 }
diff --git a/src/test/ui/consts/issue-91434.rs b/src/test/ui/consts/issue-91434.rs
new file mode 100644
index 00000000000..fc7731291b3
--- /dev/null
+++ b/src/test/ui/consts/issue-91434.rs
@@ -0,0 +1,6 @@
+fn main() {
+    [9; [[9E; h]]];
+    //~^ ERROR: expected at least one digit in exponent
+    //~| ERROR: cannot find value `h` in this scope [E0425]
+    //~| ERROR: constant expression depends on a generic parameter
+}
diff --git a/src/test/ui/consts/issue-91434.stderr b/src/test/ui/consts/issue-91434.stderr
new file mode 100644
index 00000000000..9d3fe5f2016
--- /dev/null
+++ b/src/test/ui/consts/issue-91434.stderr
@@ -0,0 +1,23 @@
+error: expected at least one digit in exponent
+  --> $DIR/issue-91434.rs:2:11
+   |
+LL |     [9; [[9E; h]]];
+   |           ^^
+
+error[E0425]: cannot find value `h` in this scope
+  --> $DIR/issue-91434.rs:2:15
+   |
+LL |     [9; [[9E; h]]];
+   |               ^ not found in this scope
+
+error: constant expression depends on a generic parameter
+  --> $DIR/issue-91434.rs:2:9
+   |
+LL |     [9; [[9E; h]]];
+   |         ^^^^^^^^^
+   |
+   = note: this may fail depending on what value the parameter takes
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0425`.