From c4bda511e35bc06575c2aca578064cb48a38c8c0 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Mon, 20 Jul 2026 21:48:08 +0330 Subject: [PATCH] Support creating float constants in rustc_public mir --- .../rustc_public/src/compiler_interface.rs | 20 +++++++++++++++++-- compiler/rustc_public/src/ty.rs | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index b4d4205e6fa01..89ef152e70f51 100644 --- a/compiler/rustc_public/src/compiler_interface.rs +++ b/compiler/rustc_public/src/compiler_interface.rs @@ -18,7 +18,7 @@ use crate::mir::{BinOp, Body, Place, UnOp}; use crate::target::{MachineInfo, MachineSize}; use crate::ty::{ AdtDef, AdtKind, Allocation, AssocItem, Asyncness, ClosureDef, ClosureKind, Constness, - CoroutineDef, Discr, FieldDef, FnDef, ForeignDef, ForeignItemKind, ForeignModule, + CoroutineDef, Discr, FieldDef, FloatTy, FnDef, ForeignDef, ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, TraitRef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef, VariantIdx, VtblEntry, @@ -514,7 +514,7 @@ impl<'tcx> CompilerInterface<'tcx> { self.with_cx(|tables, cx| cx.new_const_bool(value).stable(tables, cx)) } - /// Create a new constant that represents the given value. + /// Create a new integer constant that represents the given value. pub(crate) fn try_new_const_uint( &self, value: u128, @@ -526,6 +526,22 @@ impl<'tcx> CompilerInterface<'tcx> { }) } + /// Create a new float constant that represents the given value. + /// The value is the binary representation of the float constant. + /// Example: `try_new_const_float(2.5_f32.to_bits() as u128, FloatTy::F32)`. + pub(crate) fn try_new_const_float( + &self, + value: u128, + float_ty: FloatTy, + ) -> Result { + let mut tables = self.tables.borrow_mut(); + let cx = &*self.cx.borrow(); + let ty = cx.new_rigid_ty(RigidTy::Float(float_ty).internal(&mut *tables, cx.tcx)); + // We use `try_new_const_uint` here since it is capable of constructing all scalars in the mir + // that are not pointer. + cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx)) + } + pub(crate) fn try_new_ty_const_uint( &self, value: u128, diff --git a/compiler/rustc_public/src/ty.rs b/compiler/rustc_public/src/ty.rs index aae1bdece2fea..ece87c4e1c929 100644 --- a/compiler/rustc_public/src/ty.rs +++ b/compiler/rustc_public/src/ty.rs @@ -212,6 +212,13 @@ impl MirConst { pub fn try_from_uint(value: u128, uint_ty: UintTy) -> Result { with(|cx| cx.try_new_const_uint(value, uint_ty)) } + + /// Build a new constant that represents the given floating point number. + /// The value is the binary representation of the float constant. + /// Example: `try_from_float(2.5_f32.to_bits() as u128, FloatTy::F32)`. + pub fn try_from_float(value: u128, float_ty: FloatTy) -> Result { + with(|cx| cx.try_new_const_float(value, float_ty)) + } } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]