Skip to content

Commit 193e9f2

Browse files
authored
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 rust-lang/rust#137630. cc @tgross35 changelog: none
2 parents f9a1a1f + 416acd8 commit 193e9f2

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

clippy_lints/src/neg_multiply.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ impl<'tcx> LateLintPass<'tcx> for NegMultiply {
4949
}
5050

5151
fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
52+
const F16_ONE: u16 = 1.0_f16.to_bits();
53+
const F128_ONE: u128 = 1.0_f128.to_bits();
5254
if let ExprKind::Lit(l) = lit.kind
5355
&& matches!(
5456
consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)),
55-
Constant::Int(1) | Constant::F16(1.0) | Constant::F32(1.0) | Constant::F64(1.0) | Constant::F128(1.0)
57+
Constant::Int(1)
58+
| Constant::F16(F16_ONE)
59+
| Constant::F32(1.0)
60+
| Constant::F64(1.0)
61+
| Constant::F128(F128_ONE)
5662
)
5763
&& cx.typeck_results().expr_ty(exp).is_numeric()
5864
{

clippy_utils/src/consts.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ pub enum Constant<'tcx> {
4343
Char(char),
4444
/// An integer's bit representation.
4545
Int(u128),
46-
/// An `f16`.
47-
F16(f16),
46+
/// An `f16` bitcast to a `u16`.
47+
// FIXME(f16_f128): use `f16` once builtins are available on all host tools platforms.
48+
F16(u16),
4849
/// An `f32`.
4950
F32(f32),
5051
/// An `f64`.
5152
F64(f64),
52-
/// An `f128`.
53-
F128(f128),
53+
/// An `f128` bitcast to a `u128`.
54+
// FIXME(f16_f128): use `f128` once builtins are available on all host tools platforms.
55+
F128(u128),
5456
/// `true` or `false`.
5557
Bool(bool),
5658
/// An array of constants.
@@ -177,7 +179,7 @@ impl Hash for Constant<'_> {
177179
},
178180
Self::F16(f) => {
179181
// FIXME(f16_f128): once conversions to/from `f128` are available on all platforms,
180-
f.to_bits().hash(state);
182+
f.hash(state);
181183
},
182184
Self::F32(f) => {
183185
f64::from(f).to_bits().hash(state);
@@ -186,7 +188,7 @@ impl Hash for Constant<'_> {
186188
f.to_bits().hash(state);
187189
},
188190
Self::F128(f) => {
189-
f.to_bits().hash(state);
191+
f.hash(state);
190192
},
191193
Self::Bool(b) => {
192194
b.hash(state);
@@ -292,12 +294,12 @@ impl Constant<'_> {
292294

293295
fn parse_f16(s: &str) -> Self {
294296
let f: Half = s.parse().unwrap();
295-
Self::F16(f16::from_bits(f.to_bits().try_into().unwrap()))
297+
Self::F16(f.to_bits().try_into().unwrap())
296298
}
297299

298300
fn parse_f128(s: &str) -> Self {
299301
let f: Quad = s.parse().unwrap();
300-
Self::F128(f128::from_bits(f.to_bits()))
302+
Self::F128(f.to_bits())
301303
}
302304
}
303305

@@ -868,10 +870,10 @@ pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::Const<'tcx>) -> Option
868870
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
869871
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
870872
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
871-
ty::Float(FloatTy::F16) => Some(Constant::F16(f16::from_bits(int.into()))),
873+
ty::Float(FloatTy::F16) => Some(Constant::F16(int.into())),
872874
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
873875
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
874-
ty::Float(FloatTy::F128) => Some(Constant::F128(f128::from_bits(int.into()))),
876+
ty::Float(FloatTy::F128) => Some(Constant::F128(int.into())),
875877
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
876878
_ => None,
877879
},
@@ -892,10 +894,10 @@ pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, result: mir::Const<'tcx>) -> Option
892894
let range = alloc_range(offset + size * idx, size);
893895
let val = alloc.read_scalar(&tcx, range, /* read_provenance */ false).ok()?;
894896
res.push(match flt {
895-
FloatTy::F16 => Constant::F16(f16::from_bits(val.to_u16().discard_err()?)),
897+
FloatTy::F16 => Constant::F16(val.to_u16().discard_err()?),
896898
FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().discard_err()?)),
897899
FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().discard_err()?)),
898-
FloatTy::F128 => Constant::F128(f128::from_bits(val.to_u128().discard_err()?)),
900+
FloatTy::F128 => Constant::F128(val.to_u128().discard_err()?),
899901
});
900902
}
901903
Some(Constant::Vec(res))

clippy_utils/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![feature(array_chunks)]
22
#![feature(box_patterns)]
3-
#![feature(f128)]
4-
#![feature(f16)]
53
#![feature(if_let_guard)]
64
#![feature(macro_metavar_expr_concat)]
75
#![feature(let_chains)]

0 commit comments

Comments
 (0)