Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions compiler/rustc_public/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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<MirConst, Error> {
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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be worth creating a bridge for creating a float constant. This function eventually creates a Scalar::Integer. In practice it may not matter, but it's fragile.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had such a function in my original patch, then I found out that it is identical to try_new_const_uint. I added some comment to explain uint usage is not a mistake here, but I can add a distinct function in the bridge or rename the existing one if it is desired.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I read this wrong. I think this is fine as is. Thanks

}

pub(crate) fn try_new_ty_const_uint(
&self,
value: u128,
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_public/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ impl MirConst {
pub fn try_from_uint(value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
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<MirConst, Error> {
with(|cx| cx.try_new_const_float(value, float_ty))
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down
Loading