Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9ec4bdc

Browse files
committedAug 26, 2024·
stabilize const_float_bits_conv
1 parent 717aec0 commit 9ec4bdc

17 files changed

+231
-107
lines changed
 

‎library/core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
#![feature(const_cell_into_inner)]
123123
#![feature(const_eval_select)]
124124
#![feature(const_exact_div)]
125-
#![feature(const_float_bits_conv)]
126125
#![feature(const_float_classify)]
127126
#![feature(const_fmt_arguments_new)]
128127
#![feature(const_hash)]
@@ -166,6 +165,8 @@
166165
#![feature(coverage_attribute)]
167166
#![feature(do_not_recommend)]
168167
#![feature(duration_consts_float)]
168+
#![feature(f128_const)]
169+
#![feature(f16_const)]
169170
#![feature(internal_impls_macro)]
170171
#![feature(ip)]
171172
#![feature(is_ascii_octdigit)]

‎library/core/src/num/f128.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ impl f128 {
908908
/// ```
909909
#[inline]
910910
#[unstable(feature = "f128", issue = "116909")]
911-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
911+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
912912
#[must_use = "this returns the result of the operation, without modifying the original"]
913913
pub const fn to_bits(self) -> u128 {
914914
// SAFETY: `u128` is a plain old datatype so we can always transmute to it.
@@ -957,7 +957,7 @@ impl f128 {
957957
#[inline]
958958
#[must_use]
959959
#[unstable(feature = "f128", issue = "116909")]
960-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
960+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
961961
pub const fn from_bits(v: u128) -> Self {
962962
// It turns out the safety issues with sNaN were overblown! Hooray!
963963
// SAFETY: `u128` is a plain old datatype so we can always transmute from it.
@@ -984,7 +984,7 @@ impl f128 {
984984
/// ```
985985
#[inline]
986986
#[unstable(feature = "f128", issue = "116909")]
987-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
987+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
988988
#[must_use = "this returns the result of the operation, without modifying the original"]
989989
pub const fn to_be_bytes(self) -> [u8; 16] {
990990
self.to_bits().to_be_bytes()
@@ -1010,7 +1010,7 @@ impl f128 {
10101010
/// ```
10111011
#[inline]
10121012
#[unstable(feature = "f128", issue = "116909")]
1013-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1013+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
10141014
#[must_use = "this returns the result of the operation, without modifying the original"]
10151015
pub const fn to_le_bytes(self) -> [u8; 16] {
10161016
self.to_bits().to_le_bytes()
@@ -1047,7 +1047,7 @@ impl f128 {
10471047
/// ```
10481048
#[inline]
10491049
#[unstable(feature = "f128", issue = "116909")]
1050-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1050+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
10511051
#[must_use = "this returns the result of the operation, without modifying the original"]
10521052
pub const fn to_ne_bytes(self) -> [u8; 16] {
10531053
self.to_bits().to_ne_bytes()
@@ -1075,7 +1075,7 @@ impl f128 {
10751075
#[inline]
10761076
#[must_use]
10771077
#[unstable(feature = "f128", issue = "116909")]
1078-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1078+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
10791079
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
10801080
Self::from_bits(u128::from_be_bytes(bytes))
10811081
}
@@ -1102,7 +1102,7 @@ impl f128 {
11021102
#[inline]
11031103
#[must_use]
11041104
#[unstable(feature = "f128", issue = "116909")]
1105-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1105+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
11061106
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
11071107
Self::from_bits(u128::from_le_bytes(bytes))
11081108
}
@@ -1139,7 +1139,7 @@ impl f128 {
11391139
#[inline]
11401140
#[must_use]
11411141
#[unstable(feature = "f128", issue = "116909")]
1142-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1142+
#[rustc_const_unstable(feature = "f128_const", issue = "116909")]
11431143
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
11441144
Self::from_bits(u128::from_ne_bytes(bytes))
11451145
}

‎library/core/src/num/f16.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl f16 {
919919
/// ```
920920
#[inline]
921921
#[unstable(feature = "f16", issue = "116909")]
922-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
922+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
923923
#[must_use = "this returns the result of the operation, without modifying the original"]
924924
pub const fn to_bits(self) -> u16 {
925925
// SAFETY: `u16` is a plain old datatype so we can always transmute to it.
@@ -967,7 +967,7 @@ impl f16 {
967967
#[inline]
968968
#[must_use]
969969
#[unstable(feature = "f16", issue = "116909")]
970-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
970+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
971971
pub const fn from_bits(v: u16) -> Self {
972972
// It turns out the safety issues with sNaN were overblown! Hooray!
973973
// SAFETY: `u16` is a plain old datatype so we can always transmute from it.
@@ -993,7 +993,7 @@ impl f16 {
993993
/// ```
994994
#[inline]
995995
#[unstable(feature = "f16", issue = "116909")]
996-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
996+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
997997
#[must_use = "this returns the result of the operation, without modifying the original"]
998998
pub const fn to_be_bytes(self) -> [u8; 2] {
999999
self.to_bits().to_be_bytes()
@@ -1018,7 +1018,7 @@ impl f16 {
10181018
/// ```
10191019
#[inline]
10201020
#[unstable(feature = "f16", issue = "116909")]
1021-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1021+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
10221022
#[must_use = "this returns the result of the operation, without modifying the original"]
10231023
pub const fn to_le_bytes(self) -> [u8; 2] {
10241024
self.to_bits().to_le_bytes()
@@ -1056,7 +1056,7 @@ impl f16 {
10561056
/// ```
10571057
#[inline]
10581058
#[unstable(feature = "f16", issue = "116909")]
1059-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1059+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
10601060
#[must_use = "this returns the result of the operation, without modifying the original"]
10611061
pub const fn to_ne_bytes(self) -> [u8; 2] {
10621062
self.to_bits().to_ne_bytes()
@@ -1080,7 +1080,7 @@ impl f16 {
10801080
#[inline]
10811081
#[must_use]
10821082
#[unstable(feature = "f16", issue = "116909")]
1083-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1083+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
10841084
pub const fn from_be_bytes(bytes: [u8; 2]) -> Self {
10851085
Self::from_bits(u16::from_be_bytes(bytes))
10861086
}
@@ -1103,7 +1103,7 @@ impl f16 {
11031103
#[inline]
11041104
#[must_use]
11051105
#[unstable(feature = "f16", issue = "116909")]
1106-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1106+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
11071107
pub const fn from_le_bytes(bytes: [u8; 2]) -> Self {
11081108
Self::from_bits(u16::from_le_bytes(bytes))
11091109
}
@@ -1137,7 +1137,7 @@ impl f16 {
11371137
#[inline]
11381138
#[must_use]
11391139
#[unstable(feature = "f16", issue = "116909")]
1140-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1140+
#[rustc_const_unstable(feature = "f16_const", issue = "116909")]
11411141
pub const fn from_ne_bytes(bytes: [u8; 2]) -> Self {
11421142
Self::from_bits(u16::from_ne_bytes(bytes))
11431143
}

‎library/core/src/num/f32.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ impl f32 {
11131113
#[must_use = "this returns the result of the operation, \
11141114
without modifying the original"]
11151115
#[stable(feature = "float_bits_conv", since = "1.20.0")]
1116-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1116+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11171117
#[inline]
11181118
pub const fn to_bits(self) -> u32 {
11191119
// SAFETY: `u32` is a plain old datatype so we can always transmute to it.
@@ -1157,7 +1157,7 @@ impl f32 {
11571157
/// assert_eq!(v, 12.5);
11581158
/// ```
11591159
#[stable(feature = "float_bits_conv", since = "1.20.0")]
1160-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1160+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11611161
#[must_use]
11621162
#[inline]
11631163
pub const fn from_bits(v: u32) -> Self {
@@ -1181,7 +1181,7 @@ impl f32 {
11811181
#[must_use = "this returns the result of the operation, \
11821182
without modifying the original"]
11831183
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1184-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1184+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11851185
#[inline]
11861186
pub const fn to_be_bytes(self) -> [u8; 4] {
11871187
self.to_bits().to_be_bytes()
@@ -1202,7 +1202,7 @@ impl f32 {
12021202
#[must_use = "this returns the result of the operation, \
12031203
without modifying the original"]
12041204
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1205-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1205+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12061206
#[inline]
12071207
pub const fn to_le_bytes(self) -> [u8; 4] {
12081208
self.to_bits().to_le_bytes()
@@ -1236,7 +1236,7 @@ impl f32 {
12361236
#[must_use = "this returns the result of the operation, \
12371237
without modifying the original"]
12381238
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1239-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1239+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12401240
#[inline]
12411241
pub const fn to_ne_bytes(self) -> [u8; 4] {
12421242
self.to_bits().to_ne_bytes()
@@ -1254,7 +1254,7 @@ impl f32 {
12541254
/// assert_eq!(value, 12.5);
12551255
/// ```
12561256
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1257-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1257+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12581258
#[must_use]
12591259
#[inline]
12601260
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
@@ -1273,7 +1273,7 @@ impl f32 {
12731273
/// assert_eq!(value, 12.5);
12741274
/// ```
12751275
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1276-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1276+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12771277
#[must_use]
12781278
#[inline]
12791279
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
@@ -1303,7 +1303,7 @@ impl f32 {
13031303
/// assert_eq!(value, 12.5);
13041304
/// ```
13051305
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1306-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1306+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
13071307
#[must_use]
13081308
#[inline]
13091309
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {

‎library/core/src/num/f64.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ impl f64 {
11091109
#[must_use = "this returns the result of the operation, \
11101110
without modifying the original"]
11111111
#[stable(feature = "float_bits_conv", since = "1.20.0")]
1112-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1112+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11131113
#[inline]
11141114
pub const fn to_bits(self) -> u64 {
11151115
// SAFETY: `u64` is a plain old datatype so we can always transmute to it.
@@ -1153,7 +1153,7 @@ impl f64 {
11531153
/// assert_eq!(v, 12.5);
11541154
/// ```
11551155
#[stable(feature = "float_bits_conv", since = "1.20.0")]
1156-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1156+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11571157
#[must_use]
11581158
#[inline]
11591159
pub const fn from_bits(v: u64) -> Self {
@@ -1177,7 +1177,7 @@ impl f64 {
11771177
#[must_use = "this returns the result of the operation, \
11781178
without modifying the original"]
11791179
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1180-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1180+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
11811181
#[inline]
11821182
pub const fn to_be_bytes(self) -> [u8; 8] {
11831183
self.to_bits().to_be_bytes()
@@ -1198,7 +1198,7 @@ impl f64 {
11981198
#[must_use = "this returns the result of the operation, \
11991199
without modifying the original"]
12001200
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1201-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1201+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12021202
#[inline]
12031203
pub const fn to_le_bytes(self) -> [u8; 8] {
12041204
self.to_bits().to_le_bytes()
@@ -1232,7 +1232,7 @@ impl f64 {
12321232
#[must_use = "this returns the result of the operation, \
12331233
without modifying the original"]
12341234
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1235-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1235+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12361236
#[inline]
12371237
pub const fn to_ne_bytes(self) -> [u8; 8] {
12381238
self.to_bits().to_ne_bytes()
@@ -1250,7 +1250,7 @@ impl f64 {
12501250
/// assert_eq!(value, 12.5);
12511251
/// ```
12521252
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1253-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1253+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12541254
#[must_use]
12551255
#[inline]
12561256
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
@@ -1269,7 +1269,7 @@ impl f64 {
12691269
/// assert_eq!(value, 12.5);
12701270
/// ```
12711271
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1272-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1272+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
12731273
#[must_use]
12741274
#[inline]
12751275
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
@@ -1299,7 +1299,7 @@ impl f64 {
12991299
/// assert_eq!(value, 12.5);
13001300
/// ```
13011301
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
1302-
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1302+
#[rustc_const_stable(feature = "const_float_bits_conv", since = "CURRENT_RUSTC_VERSION")]
13031303
#[must_use]
13041304
#[inline]
13051305
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {

‎src/tools/clippy/clippy_lints/src/transmute/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,10 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
619619
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
620620
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg, &self.msrv)
621621
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
622-
| transmute_int_to_float::check(cx, e, from_ty, to_ty, arg, const_context)
622+
| transmute_int_to_float::check(cx, e, from_ty, to_ty, arg)
623623
| transmute_int_to_non_zero::check(cx, e, from_ty, to_ty, arg)
624-
| transmute_float_to_int::check(cx, e, from_ty, to_ty, arg, const_context)
625-
| transmute_num_to_bytes::check(cx, e, from_ty, to_ty, arg, const_context)
624+
| transmute_float_to_int::check(cx, e, from_ty, to_ty, arg)
625+
| transmute_num_to_bytes::check(cx, e, from_ty, to_ty, arg)
626626
| (unsound_collection_transmute::check(cx, e, from_ty, to_ty)
627627
|| transmute_undefined_repr::check(cx, e, from_ty, to_ty))
628628
| (eager_transmute::check(cx, e, arg, from_ty, to_ty));

‎src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ pub(super) fn check<'tcx>(
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
1717
mut arg: &'tcx Expr<'_>,
18-
const_context: bool,
1918
) -> bool {
2019
match (&from_ty.kind(), &to_ty.kind()) {
21-
(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {
20+
(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) => {
2221
span_lint_and_then(
2322
cx,
2423
TRANSMUTE_FLOAT_TO_INT,

‎src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_float.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ pub(super) fn check<'tcx>(
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
1616
arg: &'tcx Expr<'_>,
17-
const_context: bool,
1817
) -> bool {
1918
match (&from_ty.kind(), &to_ty.kind()) {
20-
(ty::Int(_) | ty::Uint(_), ty::Float(_)) if !const_context => {
19+
(ty::Int(_) | ty::Uint(_), ty::Float(_)) => {
2120
span_lint_and_then(
2221
cx,
2322
TRANSMUTE_INT_TO_FLOAT,

‎src/tools/clippy/clippy_lints/src/transmute/transmute_num_to_bytes.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@ pub(super) fn check<'tcx>(
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
1616
arg: &'tcx Expr<'_>,
17-
const_context: bool,
1817
) -> bool {
1918
match (&from_ty.kind(), &to_ty.kind()) {
2019
(ty::Int(_) | ty::Uint(_) | ty::Float(_), ty::Array(arr_ty, _)) => {
2120
if !matches!(arr_ty.kind(), ty::Uint(UintTy::U8)) {
2221
return false;
2322
}
24-
if matches!(from_ty.kind(), ty::Float(_)) && const_context {
25-
// TODO: Remove when const_float_bits_conv is stabilized
26-
// rust#72447
27-
return false;
28-
}
2923

3024
span_lint_and_then(
3125
cx,

‎src/tools/clippy/tests/ui/transmute.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,32 @@ mod int_to_float {
140140

141141
mod issue_5747 {
142142
const VALUE16: f16 = unsafe { std::mem::transmute(0_u16) };
143+
//~^ ERROR: transmute from a `u16` to a `f16`
143144
const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) };
145+
//~^ ERROR: transmute from a `u32` to a `f32`
144146
const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) };
147+
//~^ ERROR: transmute from a `i64` to a `f64`
145148
const VALUE128: f128 = unsafe { std::mem::transmute(0_i128) };
149+
//~^ ERROR: transmute from a `i128` to a `f128`
146150

147151
const fn from_bits_16(v: i16) -> f16 {
148152
unsafe { std::mem::transmute(v) }
153+
//~^ ERROR: transmute from a `i16` to a `f16`
149154
}
150155

151156
const fn from_bits_32(v: i32) -> f32 {
152157
unsafe { std::mem::transmute(v) }
158+
//~^ ERROR: transmute from a `i32` to a `f32`
153159
}
154160

155161
const fn from_bits_64(v: u64) -> f64 {
156162
unsafe { std::mem::transmute(v) }
163+
//~^ ERROR: transmute from a `u64` to a `f64`
157164
}
158165

159166
const fn from_bits_128(v: u128) -> f128 {
160167
unsafe { std::mem::transmute(v) }
168+
//~^ ERROR: transmute from a `u128` to a `f128`
161169
}
162170
}
163171
}
@@ -205,9 +213,13 @@ mod num_to_bytes {
205213
//~^ ERROR: transmute from a `i128` to a `[u8; 16]`
206214

207215
let _: [u8; 2] = std::mem::transmute(0.0f16);
216+
//~^ ERROR: transmute from a `f16` to a `[u8; 2]`
208217
let _: [u8; 4] = std::mem::transmute(0.0f32);
218+
//~^ ERROR: transmute from a `f32` to a `[u8; 4]`
209219
let _: [u8; 8] = std::mem::transmute(0.0f64);
220+
//~^ ERROR: transmute from a `f64` to a `[u8; 8]`
210221
let _: [u8; 16] = std::mem::transmute(0.0f128);
222+
//~^ ERROR: transmute from a `f128` to a `[u8; 16]`
211223
}
212224
}
213225
}

‎src/tools/clippy/tests/ui/transmute.stderr

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,56 @@ error: transmute from a `i128` to a `f128`
148148
LL | let _: f128 = unsafe { std::mem::transmute(0_i128) };
149149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f128::from_bits(0_i128 as u128)`
150150

151+
error: transmute from a `u16` to a `f16`
152+
--> tests/ui/transmute.rs:142:39
153+
|
154+
LL | const VALUE16: f16 = unsafe { std::mem::transmute(0_u16) };
155+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f16::from_bits(0_u16)`
156+
157+
error: transmute from a `u32` to a `f32`
158+
--> tests/ui/transmute.rs:144:39
159+
|
160+
LL | const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) };
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_u32)`
162+
163+
error: transmute from a `i64` to a `f64`
164+
--> tests/ui/transmute.rs:146:39
165+
|
166+
LL | const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) };
167+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_i64 as u64)`
168+
169+
error: transmute from a `i128` to a `f128`
170+
--> tests/ui/transmute.rs:148:41
171+
|
172+
LL | const VALUE128: f128 = unsafe { std::mem::transmute(0_i128) };
173+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f128::from_bits(0_i128 as u128)`
174+
175+
error: transmute from a `i16` to a `f16`
176+
--> tests/ui/transmute.rs:152:22
177+
|
178+
LL | unsafe { std::mem::transmute(v) }
179+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f16::from_bits(v as u16)`
180+
181+
error: transmute from a `i32` to a `f32`
182+
--> tests/ui/transmute.rs:157:22
183+
|
184+
LL | unsafe { std::mem::transmute(v) }
185+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(v as u32)`
186+
187+
error: transmute from a `u64` to a `f64`
188+
--> tests/ui/transmute.rs:162:22
189+
|
190+
LL | unsafe { std::mem::transmute(v) }
191+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(v)`
192+
193+
error: transmute from a `u128` to a `f128`
194+
--> tests/ui/transmute.rs:167:22
195+
|
196+
LL | unsafe { std::mem::transmute(v) }
197+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f128::from_bits(v)`
198+
151199
error: transmute from a `u8` to a `[u8; 1]`
152-
--> tests/ui/transmute.rs:168:30
200+
--> tests/ui/transmute.rs:176:30
153201
|
154202
LL | let _: [u8; 1] = std::mem::transmute(0u8);
155203
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u8.to_ne_bytes()`
@@ -158,97 +206,121 @@ LL | let _: [u8; 1] = std::mem::transmute(0u8);
158206
= help: to override `-D warnings` add `#[allow(clippy::transmute_num_to_bytes)]`
159207

160208
error: transmute from a `u32` to a `[u8; 4]`
161-
--> tests/ui/transmute.rs:171:30
209+
--> tests/ui/transmute.rs:179:30
162210
|
163211
LL | let _: [u8; 4] = std::mem::transmute(0u32);
164212
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u32.to_ne_bytes()`
165213

166214
error: transmute from a `u128` to a `[u8; 16]`
167-
--> tests/ui/transmute.rs:173:31
215+
--> tests/ui/transmute.rs:181:31
168216
|
169217
LL | let _: [u8; 16] = std::mem::transmute(0u128);
170218
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u128.to_ne_bytes()`
171219

172220
error: transmute from a `i8` to a `[u8; 1]`
173-
--> tests/ui/transmute.rs:175:30
221+
--> tests/ui/transmute.rs:183:30
174222
|
175223
LL | let _: [u8; 1] = std::mem::transmute(0i8);
176224
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i8.to_ne_bytes()`
177225

178226
error: transmute from a `i32` to a `[u8; 4]`
179-
--> tests/ui/transmute.rs:177:30
227+
--> tests/ui/transmute.rs:185:30
180228
|
181229
LL | let _: [u8; 4] = std::mem::transmute(0i32);
182230
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i32.to_ne_bytes()`
183231

184232
error: transmute from a `i128` to a `[u8; 16]`
185-
--> tests/ui/transmute.rs:179:31
233+
--> tests/ui/transmute.rs:187:31
186234
|
187235
LL | let _: [u8; 16] = std::mem::transmute(0i128);
188236
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i128.to_ne_bytes()`
189237

190238
error: transmute from a `f16` to a `[u8; 2]`
191-
--> tests/ui/transmute.rs:182:30
239+
--> tests/ui/transmute.rs:190:30
192240
|
193241
LL | let _: [u8; 2] = std::mem::transmute(0.0f16);
194242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f16.to_ne_bytes()`
195243

196244
error: transmute from a `f32` to a `[u8; 4]`
197-
--> tests/ui/transmute.rs:184:30
245+
--> tests/ui/transmute.rs:192:30
198246
|
199247
LL | let _: [u8; 4] = std::mem::transmute(0.0f32);
200248
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f32.to_ne_bytes()`
201249

202250
error: transmute from a `f64` to a `[u8; 8]`
203-
--> tests/ui/transmute.rs:186:30
251+
--> tests/ui/transmute.rs:194:30
204252
|
205253
LL | let _: [u8; 8] = std::mem::transmute(0.0f64);
206254
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f64.to_ne_bytes()`
207255

208256
error: transmute from a `f128` to a `[u8; 16]`
209-
--> tests/ui/transmute.rs:188:31
257+
--> tests/ui/transmute.rs:196:31
210258
|
211259
LL | let _: [u8; 16] = std::mem::transmute(0.0f128);
212260
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f128.to_ne_bytes()`
213261

214262
error: transmute from a `u8` to a `[u8; 1]`
215-
--> tests/ui/transmute.rs:194:30
263+
--> tests/ui/transmute.rs:202:30
216264
|
217265
LL | let _: [u8; 1] = std::mem::transmute(0u8);
218266
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u8.to_ne_bytes()`
219267

220268
error: transmute from a `u32` to a `[u8; 4]`
221-
--> tests/ui/transmute.rs:196:30
269+
--> tests/ui/transmute.rs:204:30
222270
|
223271
LL | let _: [u8; 4] = std::mem::transmute(0u32);
224272
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u32.to_ne_bytes()`
225273

226274
error: transmute from a `u128` to a `[u8; 16]`
227-
--> tests/ui/transmute.rs:198:31
275+
--> tests/ui/transmute.rs:206:31
228276
|
229277
LL | let _: [u8; 16] = std::mem::transmute(0u128);
230278
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u128.to_ne_bytes()`
231279

232280
error: transmute from a `i8` to a `[u8; 1]`
233-
--> tests/ui/transmute.rs:200:30
281+
--> tests/ui/transmute.rs:208:30
234282
|
235283
LL | let _: [u8; 1] = std::mem::transmute(0i8);
236284
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i8.to_ne_bytes()`
237285

238286
error: transmute from a `i32` to a `[u8; 4]`
239-
--> tests/ui/transmute.rs:202:30
287+
--> tests/ui/transmute.rs:210:30
240288
|
241289
LL | let _: [u8; 4] = std::mem::transmute(0i32);
242290
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i32.to_ne_bytes()`
243291

244292
error: transmute from a `i128` to a `[u8; 16]`
245-
--> tests/ui/transmute.rs:204:31
293+
--> tests/ui/transmute.rs:212:31
246294
|
247295
LL | let _: [u8; 16] = std::mem::transmute(0i128);
248296
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i128.to_ne_bytes()`
249297

298+
error: transmute from a `f16` to a `[u8; 2]`
299+
--> tests/ui/transmute.rs:215:30
300+
|
301+
LL | let _: [u8; 2] = std::mem::transmute(0.0f16);
302+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f16.to_ne_bytes()`
303+
304+
error: transmute from a `f32` to a `[u8; 4]`
305+
--> tests/ui/transmute.rs:217:30
306+
|
307+
LL | let _: [u8; 4] = std::mem::transmute(0.0f32);
308+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f32.to_ne_bytes()`
309+
310+
error: transmute from a `f64` to a `[u8; 8]`
311+
--> tests/ui/transmute.rs:219:30
312+
|
313+
LL | let _: [u8; 8] = std::mem::transmute(0.0f64);
314+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f64.to_ne_bytes()`
315+
316+
error: transmute from a `f128` to a `[u8; 16]`
317+
--> tests/ui/transmute.rs:221:31
318+
|
319+
LL | let _: [u8; 16] = std::mem::transmute(0.0f128);
320+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f128.to_ne_bytes()`
321+
250322
error: transmute from a `&[u8]` to a `&str`
251-
--> tests/ui/transmute.rs:218:28
323+
--> tests/ui/transmute.rs:230:28
252324
|
253325
LL | let _: &str = unsafe { std::mem::transmute(B) };
254326
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(B).unwrap()`
@@ -257,16 +329,16 @@ LL | let _: &str = unsafe { std::mem::transmute(B) };
257329
= help: to override `-D warnings` add `#[allow(clippy::transmute_bytes_to_str)]`
258330

259331
error: transmute from a `&mut [u8]` to a `&mut str`
260-
--> tests/ui/transmute.rs:221:32
332+
--> tests/ui/transmute.rs:233:32
261333
|
262334
LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
263335
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
264336

265337
error: transmute from a `&[u8]` to a `&str`
266-
--> tests/ui/transmute.rs:223:30
338+
--> tests/ui/transmute.rs:235:30
267339
|
268340
LL | const _: &str = unsafe { std::mem::transmute(B) };
269341
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_unchecked(B)`
270342

271-
error: aborting due to 42 previous errors
343+
error: aborting due to 54 previous errors
272344

‎src/tools/clippy/tests/ui/transmute_float_to_int.fixed

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::transmute_float_to_int)]
22
#![allow(clippy::missing_transmute_annotations)]
3-
#![feature(f128)]
4-
#![feature(f16)]
3+
#![feature(f128, f128_const)]
4+
#![feature(f16, f16_const)]
55

66
fn float_to_int() {
77
let _: u32 = unsafe { 1f32.to_bits() };
@@ -20,25 +20,33 @@ fn float_to_int() {
2020
}
2121

2222
mod issue_5747 {
23-
const VALUE16: i16 = unsafe { std::mem::transmute(1f16) };
24-
const VALUE32: i32 = unsafe { std::mem::transmute(1f32) };
25-
const VALUE64: u64 = unsafe { std::mem::transmute(1f64) };
26-
const VALUE128: u128 = unsafe { std::mem::transmute(1f128) };
23+
const VALUE16: i16 = unsafe { 1f16.to_bits() as i16 };
24+
//~^ ERROR: transmute from a `f16` to a `i16`
25+
const VALUE32: i32 = unsafe { 1f32.to_bits() as i32 };
26+
//~^ ERROR: transmute from a `f32` to a `i32`
27+
const VALUE64: u64 = unsafe { 1f64.to_bits() };
28+
//~^ ERROR: transmute from a `f64` to a `u64`
29+
const VALUE128: u128 = unsafe { 1f128.to_bits() };
30+
//~^ ERROR: transmute from a `f128` to a `u128`
2731

2832
const fn to_bits_16(v: f16) -> u16 {
29-
unsafe { std::mem::transmute(v) }
33+
unsafe { v.to_bits() }
34+
//~^ ERROR: transmute from a `f16` to a `u16`
3035
}
3136

3237
const fn to_bits_32(v: f32) -> u32 {
33-
unsafe { std::mem::transmute(v) }
38+
unsafe { v.to_bits() }
39+
//~^ ERROR: transmute from a `f32` to a `u32`
3440
}
3541

3642
const fn to_bits_64(v: f64) -> i64 {
37-
unsafe { std::mem::transmute(v) }
43+
unsafe { v.to_bits() as i64 }
44+
//~^ ERROR: transmute from a `f64` to a `i64`
3845
}
3946

4047
const fn to_bits_128(v: f128) -> i128 {
41-
unsafe { std::mem::transmute(v) }
48+
unsafe { v.to_bits() as i128 }
49+
//~^ ERROR: transmute from a `f128` to a `i128`
4250
}
4351
}
4452

‎src/tools/clippy/tests/ui/transmute_float_to_int.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::transmute_float_to_int)]
22
#![allow(clippy::missing_transmute_annotations)]
3-
#![feature(f128)]
4-
#![feature(f16)]
3+
#![feature(f128, f128_const)]
4+
#![feature(f16, f16_const)]
55

66
fn float_to_int() {
77
let _: u32 = unsafe { std::mem::transmute(1f32) };
@@ -21,24 +21,32 @@ fn float_to_int() {
2121

2222
mod issue_5747 {
2323
const VALUE16: i16 = unsafe { std::mem::transmute(1f16) };
24+
//~^ ERROR: transmute from a `f16` to a `i16`
2425
const VALUE32: i32 = unsafe { std::mem::transmute(1f32) };
26+
//~^ ERROR: transmute from a `f32` to a `i32`
2527
const VALUE64: u64 = unsafe { std::mem::transmute(1f64) };
28+
//~^ ERROR: transmute from a `f64` to a `u64`
2629
const VALUE128: u128 = unsafe { std::mem::transmute(1f128) };
30+
//~^ ERROR: transmute from a `f128` to a `u128`
2731

2832
const fn to_bits_16(v: f16) -> u16 {
2933
unsafe { std::mem::transmute(v) }
34+
//~^ ERROR: transmute from a `f16` to a `u16`
3035
}
3136

3237
const fn to_bits_32(v: f32) -> u32 {
3338
unsafe { std::mem::transmute(v) }
39+
//~^ ERROR: transmute from a `f32` to a `u32`
3440
}
3541

3642
const fn to_bits_64(v: f64) -> i64 {
3743
unsafe { std::mem::transmute(v) }
44+
//~^ ERROR: transmute from a `f64` to a `i64`
3845
}
3946

4047
const fn to_bits_128(v: f128) -> i128 {
4148
unsafe { std::mem::transmute(v) }
49+
//~^ ERROR: transmute from a `f128` to a `i128`
4250
}
4351
}
4452

‎src/tools/clippy/tests/ui/transmute_float_to_int.stderr

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,53 @@ error: transmute from a `f64` to a `u64`
3737
LL | let _: u64 = unsafe { std::mem::transmute(-1.0) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-1.0f64).to_bits()`
3939

40-
error: aborting due to 6 previous errors
40+
error: transmute from a `f16` to a `i16`
41+
--> tests/ui/transmute_float_to_int.rs:23:35
42+
|
43+
LL | const VALUE16: i16 = unsafe { std::mem::transmute(1f16) };
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f16.to_bits() as i16`
45+
46+
error: transmute from a `f32` to a `i32`
47+
--> tests/ui/transmute_float_to_int.rs:25:35
48+
|
49+
LL | const VALUE32: i32 = unsafe { std::mem::transmute(1f32) };
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f32.to_bits() as i32`
51+
52+
error: transmute from a `f64` to a `u64`
53+
--> tests/ui/transmute_float_to_int.rs:27:35
54+
|
55+
LL | const VALUE64: u64 = unsafe { std::mem::transmute(1f64) };
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f64.to_bits()`
57+
58+
error: transmute from a `f128` to a `u128`
59+
--> tests/ui/transmute_float_to_int.rs:29:37
60+
|
61+
LL | const VALUE128: u128 = unsafe { std::mem::transmute(1f128) };
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f128.to_bits()`
63+
64+
error: transmute from a `f16` to a `u16`
65+
--> tests/ui/transmute_float_to_int.rs:33:18
66+
|
67+
LL | unsafe { std::mem::transmute(v) }
68+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `v.to_bits()`
69+
70+
error: transmute from a `f32` to a `u32`
71+
--> tests/ui/transmute_float_to_int.rs:38:18
72+
|
73+
LL | unsafe { std::mem::transmute(v) }
74+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `v.to_bits()`
75+
76+
error: transmute from a `f64` to a `i64`
77+
--> tests/ui/transmute_float_to_int.rs:43:18
78+
|
79+
LL | unsafe { std::mem::transmute(v) }
80+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `v.to_bits() as i64`
81+
82+
error: transmute from a `f128` to a `i128`
83+
--> tests/ui/transmute_float_to_int.rs:48:18
84+
|
85+
LL | unsafe { std::mem::transmute(v) }
86+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `v.to_bits() as i128`
87+
88+
error: aborting due to 14 previous errors
4189

‎tests/ui/consts/const-float-bits-conv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ compile-flags: -Zmir-opt-level=0
22
//@ run-pass
33

4-
#![feature(const_float_bits_conv)]
54
#![feature(const_float_classify)]
65
#![allow(unused_macro_rules)]
76

@@ -91,4 +90,5 @@ fn f64() {
9190
fn main() {
9291
f32();
9392
f64();
93+
// FIXME(f16_f128): also test f16 and f128
9494
}

‎tests/ui/consts/const-float-classify.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
//@ compile-flags: -Zmir-opt-level=0 -Znext-solver
2-
//@ known-bug: #110395
3-
// FIXME(effects) run-pass
2+
//@ run-pass
43

5-
#![feature(const_float_bits_conv)]
64
#![feature(const_float_classify)]
7-
#![feature(const_trait_impl, effects)]
8-
#![allow(incomplete_features)]
95

106
// Don't promote
117
const fn nop<T>(x: T) -> T { x }
128

13-
impl const PartialEq<NonDet> for bool {
14-
fn eq(&self, _: &NonDet) -> bool {
15-
true
16-
}
17-
}
18-
199
macro_rules! const_assert {
10+
($a:expr, NonDet) => {
11+
{
12+
const _: () = { let _val = $a; };
13+
let _val = nop($a);
14+
}
15+
};
2016
($a:expr, $b:expr) => {
2117
{
2218
const _: () = assert!($a == $b);
@@ -52,12 +48,9 @@ macro_rules! suite_inner {
5248
( $ty:ident [$( $fn:ident ),*]) => {};
5349
}
5450

55-
#[derive(Debug)]
56-
struct NonDet;
57-
58-
// The result of the `is_sign` methods are not checked for correctness, since LLVM does not
51+
// The result of the `is_sign` methods are not checked for correctness, since we do not
5952
// guarantee anything about the signedness of NaNs. See
60-
// https://github.com/rust-lang/rust/issues/55131.
53+
// https://rust-lang.github.io/rfcs/3514-float-semantics.html.
6154

6255
suite! {
6356
[is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -74,4 +67,5 @@ suite! {
7467
fn main() {
7568
f32();
7669
f64();
70+
// FIXME(f16_f128): also test f16 and f128
7771
}

‎tests/ui/consts/const-float-classify.stderr

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.