Skip to content

Commit d353817

Browse files
authored
Unrolled build for rust-lang#126636
Rollup merge of rust-lang#126636 - tgross35:clippy-f16-f128-fixme, r=flip1995 Resolve Clippy `f16` and `f128` `unimplemented!`/`FIXME`s This was originally a PR against the Clippy repo, rust-lang/rust-clippy#12950 r? ``@flip1995`` Tracking issue: rust-lang#116909 Fixes: rust-lang#126636
2 parents 1aaab8b + 477e9e8 commit d353817

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+845
-532
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ dependencies = [
709709
"clippy_config",
710710
"itertools 0.12.1",
711711
"rustc-semver",
712+
"rustc_apfloat",
712713
]
713714

714715
[[package]]

src/tools/clippy/clippy_lints/src/casts/cast_nan_to_int.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2121

2222
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
2323
match constant(cx, cx.typeck_results(), e) {
24+
// FIXME(f16_f128): add these types when nan checks are available on all platforms
2425
Some(Constant::F64(n)) => n.is_nan(),
2526
Some(Constant::F32(n)) => n.is_nan(),
2627
_ => false,

src/tools/clippy/clippy_lints/src/float_literal.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,17 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
141141
#[must_use]
142142
fn max_digits(fty: FloatTy) -> u32 {
143143
match fty {
144-
// FIXME(f16_f128): replace the magic numbers once `{f16,f128}::DIGITS` are available
145-
FloatTy::F16 => 3,
144+
FloatTy::F16 => f16::DIGITS,
146145
FloatTy::F32 => f32::DIGITS,
147146
FloatTy::F64 => f64::DIGITS,
148-
FloatTy::F128 => 33,
147+
FloatTy::F128 => f128::DIGITS,
149148
}
150149
}
151150

152151
/// Counts the digits excluding leading zeros
153152
#[must_use]
154153
fn count_digits(s: &str) -> usize {
155-
// Note that s does not contain the f32/64 suffix, and underscores have been stripped
154+
// Note that s does not contain the `f{16,32,64,128}` suffix, and underscores have been stripped
156155
s.chars()
157156
.filter(|c| *c != '-' && *c != '.')
158157
.take_while(|c| *c != 'e' && *c != 'E')

src/tools/clippy/clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![feature(array_windows)]
22
#![feature(binary_heap_into_iter_sorted)]
33
#![feature(box_patterns)]
4+
#![feature(f128)]
5+
#![feature(f16)]
46
#![feature(if_let_guard)]
57
#![feature(iter_intersperse)]
68
#![feature(let_chains)]

src/tools/clippy/clippy_lints/src/manual_float_methods.rs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
156156

157157
fn is_infinity(constant: &Constant<'_>) -> bool {
158158
match constant {
159+
// FIXME(f16_f128): add f16 and f128 when constants are available
159160
Constant::F32(float) => *float == f32::INFINITY,
160161
Constant::F64(float) => *float == f64::INFINITY,
161162
_ => false,
@@ -164,6 +165,7 @@ fn is_infinity(constant: &Constant<'_>) -> bool {
164165

165166
fn is_neg_infinity(constant: &Constant<'_>) -> bool {
166167
match constant {
168+
// FIXME(f16_f128): add f16 and f128 when constants are available
167169
Constant::F32(float) => *float == f32::NEG_INFINITY,
168170
Constant::F64(float) => *float == f64::NEG_INFINITY,
169171
_ => false,

src/tools/clippy/clippy_lints/src/operators/float_cmp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn get_lint_and_message(is_local: bool, is_comparing_arrays: bool) -> (&'static
8686

8787
fn is_allowed(val: &Constant<'_>) -> bool {
8888
match val {
89+
// FIXME(f16_f128): add when equality check is available on all platforms
8990
&Constant::F32(f) => f == 0.0 || f.is_infinite(),
9091
&Constant::F64(f) => f == 0.0 || f.is_infinite(),
9192
Constant::Vec(vec) => vec.iter().all(|f| match f {

src/tools/clippy/clippy_lints/src/operators/modulo_arithmetic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_>, expr: &Expr<'_>) ->
7979
},
8080
_ => {},
8181
},
82+
// FIXME(f16_f128): add when casting is available on all platforms
8283
Some(Constant::F32(f)) => {
8384
return Some(floating_point_operand_info(&f));
8485
},

src/tools/clippy/clippy_lints/src/zero_div_zero.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl<'tcx> LateLintPass<'tcx> for ZeroDiv {
3838
// do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.
3939
&& let Some(lhs_value) = constant_simple(cx, cx.typeck_results(), left)
4040
&& let Some(rhs_value) = constant_simple(cx, cx.typeck_results(), right)
41+
// FIXME(f16_f128): add these types when eq is available on all platforms
4142
&& (Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value)
4243
&& (Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value)
4344
{

src/tools/clippy/clippy_utils/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ clippy_config = { path = "../clippy_config" }
99
arrayvec = { version = "0.7", default-features = false }
1010
itertools = "0.12"
1111
rustc-semver = "1.1"
12+
# FIXME(f16_f128): remove when no longer needed for parsing
13+
rustc_apfloat = "0.2.0"
1214

1315
[features]
1416
deny-warnings = ["clippy_config/deny-warnings"]

src/tools/clippy/clippy_utils/src/consts.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::source::{get_source_text, walk_span_to_context};
44
use crate::{clip, is_direct_expn_of, sext, unsext};
55

6+
use rustc_apfloat::ieee::{Half, Quad};
7+
use rustc_apfloat::Float;
68
use rustc_ast::ast::{self, LitFloatType, LitKind};
79
use rustc_data_structures::sync::Lrc;
810
use rustc_hir::def::{DefKind, Res};
@@ -33,10 +35,14 @@ pub enum Constant<'tcx> {
3335
Char(char),
3436
/// An integer's bit representation.
3537
Int(u128),
38+
/// An `f16`.
39+
F16(f16),
3640
/// An `f32`.
3741
F32(f32),
3842
/// An `f64`.
3943
F64(f64),
44+
/// An `f128`.
45+
F128(f128),
4046
/// `true` or `false`.
4147
Bool(bool),
4248
/// An array of constants.
@@ -161,12 +167,19 @@ impl<'tcx> Hash for Constant<'tcx> {
161167
Self::Int(i) => {
162168
i.hash(state);
163169
},
170+
Self::F16(f) => {
171+
// FIXME(f16_f128): once conversions to/from `f128` are available on all platforms,
172+
f.to_bits().hash(state);
173+
},
164174
Self::F32(f) => {
165175
f64::from(f).to_bits().hash(state);
166176
},
167177
Self::F64(f) => {
168178
f.to_bits().hash(state);
169179
},
180+
Self::F128(f) => {
181+
f.to_bits().hash(state);
182+
},
170183
Self::Bool(b) => {
171184
b.hash(state);
172185
},
@@ -268,6 +281,16 @@ impl<'tcx> Constant<'tcx> {
268281
}
269282
self
270283
}
284+
285+
fn parse_f16(s: &str) -> Self {
286+
let f: Half = s.parse().unwrap();
287+
Self::F16(f16::from_bits(f.to_bits().try_into().unwrap()))
288+
}
289+
290+
fn parse_f128(s: &str) -> Self {
291+
let f: Quad = s.parse().unwrap();
292+
Self::F128(f128::from_bits(f.to_bits()))
293+
}
271294
}
272295

273296
/// Parses a `LitKind` to a `Constant`.
@@ -279,16 +302,17 @@ pub fn lit_to_mir_constant<'tcx>(lit: &LitKind, ty: Option<Ty<'tcx>>) -> Constan
279302
LitKind::Char(c) => Constant::Char(c),
280303
LitKind::Int(n, _) => Constant::Int(n.get()),
281304
LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
282-
ast::FloatTy::F16 => unimplemented!("f16_f128"),
305+
// FIXME(f16_f128): just use `parse()` directly when available for `f16`/`f128`
306+
ast::FloatTy::F16 => Constant::parse_f16(is.as_str()),
283307
ast::FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
284308
ast::FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
285-
ast::FloatTy::F128 => unimplemented!("f16_f128"),
309+
ast::FloatTy::F128 => Constant::parse_f128(is.as_str()),
286310
},
287311
LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind() {
288-
ty::Float(FloatTy::F16) => unimplemented!("f16_f128"),
312+
ty::Float(FloatTy::F16) => Constant::parse_f16(is.as_str()),
289313
ty::Float(FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()),
290314
ty::Float(FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()),
291-
ty::Float(FloatTy::F128) => unimplemented!("f16_f128"),
315+
ty::Float(FloatTy::F128) => Constant::parse_f128(is.as_str()),
292316
_ => bug!(),
293317
},
294318
LitKind::Bool(b) => Constant::Bool(b),
@@ -625,15 +649,19 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
625649

626650
match (lhs, index) {
627651
(Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec.get(index as usize) {
652+
Some(Constant::F16(x)) => Some(Constant::F16(*x)),
628653
Some(Constant::F32(x)) => Some(Constant::F32(*x)),
629654
Some(Constant::F64(x)) => Some(Constant::F64(*x)),
655+
Some(Constant::F128(x)) => Some(Constant::F128(*x)),
630656
_ => None,
631657
},
632658
(Some(Constant::Vec(vec)), _) => {
633659
if !vec.is_empty() && vec.iter().all(|x| *x == vec[0]) {
634660
match vec.first() {
661+
Some(Constant::F16(x)) => Some(Constant::F16(*x)),
635662
Some(Constant::F32(x)) => Some(Constant::F32(*x)),
636663
Some(Constant::F64(x)) => Some(Constant::F64(*x)),
664+
Some(Constant::F128(x)) => Some(Constant::F128(*x)),
637665
_ => None,
638666
}
639667
} else {
@@ -760,6 +788,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
760788
},
761789
_ => None,
762790
},
791+
// FIXME(f16_f128): add these types when binary operations are available on all platforms
763792
(Constant::F32(l), Some(Constant::F32(r))) => match op.node {
764793
BinOpKind::Add => Some(Constant::F32(l + r)),
765794
BinOpKind::Sub => Some(Constant::F32(l - r)),
@@ -813,8 +842,10 @@ pub fn mir_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) ->
813842
ty::Adt(adt_def, _) if adt_def.is_struct() => Some(Constant::Adt(result)),
814843
ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
815844
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
845+
ty::Float(FloatTy::F16) => Some(Constant::F16(f16::from_bits(int.into()))),
816846
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
817847
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
848+
ty::Float(FloatTy::F128) => Some(Constant::F128(f128::from_bits(int.into()))),
818849
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
819850
_ => None,
820851
},
@@ -835,10 +866,10 @@ pub fn mir_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) ->
835866
let range = alloc_range(offset + size * idx, size);
836867
let val = alloc.read_scalar(&lcx.tcx, range, /* read_provenance */ false).ok()?;
837868
res.push(match flt {
838-
FloatTy::F16 => unimplemented!("f16_f128"),
869+
FloatTy::F16 => Constant::F16(f16::from_bits(val.to_u16().ok()?)),
839870
FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().ok()?)),
840871
FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().ok()?)),
841-
FloatTy::F128 => unimplemented!("f16_f128"),
872+
FloatTy::F128 => Constant::F128(f128::from_bits(val.to_u128().ok()?)),
842873
});
843874
}
844875
Some(Constant::Vec(res))

src/tools/clippy/clippy_utils/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![feature(array_chunks)]
22
#![feature(box_patterns)]
33
#![feature(control_flow_enum)]
4+
#![feature(f128)]
5+
#![feature(f16)]
46
#![feature(if_let_guard)]
57
#![feature(let_chains)]
68
#![feature(lint_reasons)]

src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040

4141
a.sort_unstable();
4242

43+
// FIXME(f16_f128): add a clamp test once the function is available
4344
let _ = 2.0f32.clamp(3.0f32, 4.0f32);
4445
let _ = 2.0f64.clamp(3.0f64, 4.0f64);
4546

src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,61 @@ LL | a.sort_unstable();
2828
| ^^^^^^^^^^^^^^^^^
2929

3030
error: use of a disallowed method `f32::clamp`
31-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:43:13
31+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:44:13
3232
|
3333
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
3434
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3535

3636
error: use of a disallowed method `regex::Regex::new`
37-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:46:61
37+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:47:61
3838
|
3939
LL | let indirect: fn(&str) -> Result<Regex, regex::Error> = Regex::new;
4040
| ^^^^^^^^^^
4141

4242
error: use of a disallowed method `f32::clamp`
43-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:49:28
43+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:50:28
4444
|
4545
LL | let in_call = Box::new(f32::clamp);
4646
| ^^^^^^^^^^
4747

4848
error: use of a disallowed method `regex::Regex::new`
49-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:50:53
49+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:51:53
5050
|
5151
LL | let in_method_call = ["^", "$"].into_iter().map(Regex::new);
5252
| ^^^^^^^^^^
5353

5454
error: use of a disallowed method `futures::stream::select_all`
55-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:53:31
55+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:54:31
5656
|
5757
LL | let same_name_as_module = select_all(vec![empty::<()>()]);
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959

6060
error: use of a disallowed method `conf_disallowed_methods::local_fn`
61-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:55:5
61+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5
6262
|
6363
LL | local_fn();
6464
| ^^^^^^^^^^
6565

6666
error: use of a disallowed method `conf_disallowed_methods::local_mod::f`
67-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5
67+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:5
6868
|
6969
LL | local_mod::f();
7070
| ^^^^^^^^^^^^^^
7171

7272
error: use of a disallowed method `conf_disallowed_methods::Struct::method`
73-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:58:5
73+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:5
7474
|
7575
LL | s.method();
7676
| ^^^^^^^^^^
7777

7878
error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method`
79-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:5
79+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:5
8080
|
8181
LL | s.provided_method();
8282
| ^^^^^^^^^^^^^^^^^^^
8383

8484
error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method`
85-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:5
85+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:61:5
8686
|
8787
LL | s.implemented_method();
8888
| ^^^^^^^^^^^^^^^^^^^^^^

src/tools/clippy/tests/ui/arithmetic_side_effects.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
unconditional_panic
1212
)]
1313
#![feature(const_mut_refs)]
14+
#![feature(f128)]
15+
#![feature(f16)]
1416
#![warn(clippy::arithmetic_side_effects)]
1517

1618
extern crate proc_macro_derive;
@@ -162,8 +164,10 @@ pub fn association_with_structures_should_not_trigger_the_lint() {
162164
}
163165

164166
pub fn hard_coded_allowed() {
167+
let _ = 1f16 + 1f16;
165168
let _ = 1f32 + 1f32;
166169
let _ = 1f64 + 1f64;
170+
let _ = 1f128 + 1f128;
167171

168172
let _ = Saturating(0u32) + Saturating(0u32);
169173
let _ = String::new() + "";

0 commit comments

Comments
 (0)