Don't use f16 and f128 directly in clippy_utils (#14528)

Not all host tools platforms support `f16`/`f128` builtins yet due to
LLVM assertion failures and miscompilations. Until them, Clippy should
avoid using `f16`/`f128` at runtime itself. See
https://github.com/rust-lang/rust/issues/137630.

cc @tgross35

changelog: none
This commit is contained in:
Jason Newcomb
2025-04-03 10:14:59 +00:00
committed by GitHub
3 changed files with 21 additions and 15 deletions
+7 -1
View File
@@ -49,10 +49,16 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
}
fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
const F16_ONE: u16 = 1.0_f16.to_bits();
const F128_ONE: u128 = 1.0_f128.to_bits();
if let ExprKind::Lit(l) = lit.kind
&& matches!(
consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)),
Constant::Int(1) | Constant::F16(1.0) | Constant::F32(1.0) | Constant::F64(1.0) | Constant::F128(1.0)
Constant::Int(1)
| Constant::F16(F16_ONE)
| Constant::F32(1.0)
| Constant::F64(1.0)
| Constant::F128(F128_ONE)
)
&& cx.typeck_results().expr_ty(exp).is_numeric()
{
+14 -12
View File
@@ -43,14 +43,16 @@ pub enum Constant<'tcx> {
Char(char),
/// An integer's bit representation.
Int(u128),
/// An `f16`.
F16(f16),
/// An `f16` bitcast to a `u16`.
// FIXME(f16_f128): use `f16` once builtins are available on all host tools platforms.
F16(u16),
/// An `f32`.
F32(f32),
/// An `f64`.
F64(f64),
/// An `f128`.
F128(f128),
/// An `f128` bitcast to a `u128`.
// FIXME(f16_f128): use `f128` once builtins are available on all host tools platforms.
F128(u128),
/// `true` or `false`.
Bool(bool),
/// An array of constants.
@@ -177,7 +179,7 @@ fn hash<H>(&self, state: &mut H)
},
Self::F16(f) => {
// FIXME(f16_f128): once conversions to/from `f128` are available on all platforms,
f.to_bits().hash(state);
f.hash(state);
},
Self::F32(f) => {
f64::from(f).to_bits().hash(state);
@@ -186,7 +188,7 @@ fn hash<H>(&self, state: &mut H)
f.to_bits().hash(state);
},
Self::F128(f) => {
f.to_bits().hash(state);
f.hash(state);
},
Self::Bool(b) => {
b.hash(state);
@@ -292,12 +294,12 @@ pub fn peel_refs(mut self) -> Self {
fn parse_f16(s: &str) -> Self {
let f: Half = s.parse().unwrap();
Self::F16(f16::from_bits(f.to_bits().try_into().unwrap()))
Self::F16(f.to_bits().try_into().unwrap())
}
fn parse_f128(s: &str) -> Self {
let f: Quad = s.parse().unwrap();
Self::F128(f128::from_bits(f.to_bits()))
Self::F128(f.to_bits())
}
}
@@ -868,10 +870,10 @@ pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::Const<'tcx>) -> Option
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
ty::Float(FloatTy::F16) => Some(Constant::F16(f16::from_bits(int.into()))),
ty::Float(FloatTy::F16) => Some(Constant::F16(int.into())),
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
ty::Float(FloatTy::F128) => Some(Constant::F128(f128::from_bits(int.into()))),
ty::Float(FloatTy::F128) => Some(Constant::F128(int.into())),
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
_ => None,
},
@@ -892,10 +894,10 @@ pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::Const<'tcx>) -> Option
let range = alloc_range(offset + size * idx, size);
let val = alloc.read_scalar(&tcx, range, /* read_provenance */ false).ok()?;
res.push(match flt {
FloatTy::F16 => Constant::F16(f16::from_bits(val.to_u16().discard_err()?)),
FloatTy::F16 => Constant::F16(val.to_u16().discard_err()?),
FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().discard_err()?)),
FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().discard_err()?)),
FloatTy::F128 => Constant::F128(f128::from_bits(val.to_u128().discard_err()?)),
FloatTy::F128 => Constant::F128(val.to_u128().discard_err()?),
});
}
Some(Constant::Vec(res))
-2
View File
@@ -1,7 +1,5 @@
#![feature(array_chunks)]
#![feature(box_patterns)]
#![feature(f128)]
#![feature(f16)]
#![feature(if_let_guard)]
#![feature(macro_metavar_expr_concat)]
#![feature(let_chains)]