Skip to content

Commit e40b51d

Browse files
committed
Get lit_to_const in sync with const_to_valtree_inner
Without this, we'd have a discrepancy where float literals are not lowered to valtrees, but float constants are.
1 parent f8b5e00 commit e40b51d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

compiler/rustc_mir_build/src/thir/constant.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
33
use rustc_middle::ty::{self, ParamEnv, ScalarInt, TyCtxt};
44
use rustc_span::DUMMY_SP;
55

6+
use crate::build::parse_float_into_scalar;
7+
68
pub(crate) fn lit_to_const<'tcx>(
79
tcx: TyCtxt<'tcx>,
810
lit_input: LitToConstInput<'tcx>,
@@ -52,6 +54,17 @@ pub(crate) fn lit_to_const<'tcx>(
5254
ty::ValTree::from_scalar_int(scalar_int)
5355
}
5456
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
57+
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
58+
let bits = parse_float_into_scalar(*n, *fty, neg)
59+
.ok_or_else(|| {
60+
LitToConstError::Reported(tcx.sess.delay_span_bug(
61+
DUMMY_SP,
62+
format!("couldn't parse float literal: {:?}", lit_input.lit),
63+
))
64+
})?
65+
.assert_int();
66+
ty::ValTree::from_scalar_int(bits)
67+
}
5568
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
5669
(ast::LitKind::Err, _) => {
5770
return Err(LitToConstError::Reported(

0 commit comments

Comments
 (0)