Skip to content

add signed ints to unn- transmutes to ensure feature parity #140804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
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
27 changes: 27 additions & 0 deletions compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,45 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
},
// char → u32
(Char, Uint(UintTy::U32)) => err(format!("u32::from({arg})")),
// char (→ u32) → i32
(Char, Int(IntTy::I32)) => err(format!("u32::from({arg}).cast_signed()")),
// u32 → char
(Uint(UintTy::U32), Char) => Error {
sugg: format!("char::from_u32_unchecked({arg})"),
help: Some("consider `char::from_u32(…).unwrap()`"),
span,
},
// i32 → char
(Int(IntTy::I32), Char) => Error {
sugg: format!("char::from_u32_unchecked(i32::cast_unsigned({arg}))"),
help: Some("consider `char::from_u32(i32::cast_unsigned(…)).unwrap()`"),
span,
},
// uNN → iNN
(Uint(ty), Int(_)) => err(format!("{}::cast_signed({arg})", ty.name_str())),
// iNN → uNN
(Int(ty), Uint(_)) => err(format!("{}::cast_unsigned({arg})", ty.name_str())),
// fNN → xsize
(Float(ty), Uint(UintTy::Usize)) => {
err(format!("{}::to_bits({arg}) as usize", ty.name_str()))
}
(Float(ty), Int(IntTy::Isize)) => {
err(format!("{}::to_bits({arg}) as isize", ty.name_str()))
}
// fNN (→ uNN) → iNN
(Float(ty), Int(..)) => err(format!("{}::to_bits({arg}).cast_signed()", ty.name_str())),
// fNN → uNN
(Float(ty), Uint(..)) => err(format!("{}::to_bits({arg})", ty.name_str())),
// xsize → fNN
(Uint(UintTy::Usize) | Int(IntTy::Isize), Float(ty)) => {
err(format!("{}::from_bits({arg} as _)", ty.name_str(),))
}
// iNN (→ uNN) → fNN
(Int(int_ty), Float(ty)) => err(format!(
"{}::from_bits({}::cast_unsigned({arg}))",
ty.name_str(),
int_ty.name_str()
)),
// uNN → fNN
(Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())),
// bool → { x8 }
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/pass/shims/x86/intrinsics-x86-sse2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// We're testing x86 target specific features
//@only-target: x86_64 i686
#![allow(unnecessary_transmutes)]
Copy link
Member

Choose a reason for hiding this comment

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

Please leave an empty line between module-level attributes and the module contents.


#[cfg(target_arch = "x86")]
use std::arch::x86::*;
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/transmute/unnecessary-transmutation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ fn main() {
//~^ ERROR
let y: char = char::from_u32_unchecked(y);
//~^ ERROR
let y: i32 = u32::from('🐱').cast_signed();
//~^ ERROR
let y: char = char::from_u32_unchecked(i32::cast_unsigned(y));
//~^ ERROR

let x: u16 = i16::cast_unsigned(8i16);
//~^ ERROR
Expand All @@ -72,6 +76,11 @@ fn main() {
let y: u64 = f64::to_bits(2.0);
//~^ ERROR

let y: f64 = f64::from_bits(i64::cast_unsigned(1i64));
//~^ ERROR
let y: i64 = f64::to_bits(1f64).cast_signed();
//~^ ERROR

let z: bool = (1u8 == 1);
//~^ ERROR
let z: u8 = (z) as u8;
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/transmute/unnecessary-transmutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ fn main() {
//~^ ERROR
let y: char = transmute(y);
//~^ ERROR
let y: i32 = transmute('🐱');
//~^ ERROR
let y: char = transmute(y);
//~^ ERROR

let x: u16 = transmute(8i16);
//~^ ERROR
Expand All @@ -72,6 +76,11 @@ fn main() {
let y: u64 = transmute(2.0);
//~^ ERROR

let y: f64 = transmute(1i64);
//~^ ERROR
let y: i64 = transmute(1f64);
//~^ ERROR

let z: bool = transmute(1u8);
//~^ ERROR
let z: u8 = transmute(z);
Expand Down
54 changes: 40 additions & 14 deletions tests/ui/transmute/unnecessary-transmutation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -154,82 +154,108 @@ LL | let y: char = transmute(y);
= help: consider `char::from_u32(…).unwrap()`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:53:22
--> $DIR/unnecessary-transmutation.rs:52:22
|
LL | let y: i32 = transmute('🐱');
| ^^^^^^^^^^^^^^^ help: replace this with: `u32::from('🐱').cast_signed()`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:54:23
|
LL | let y: char = transmute(y);
| ^^^^^^^^^^^^ help: replace this with: `char::from_u32_unchecked(i32::cast_unsigned(y))`
|
= help: consider `char::from_u32(i32::cast_unsigned(…)).unwrap()`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:57:22
|
LL | let x: u16 = transmute(8i16);
| ^^^^^^^^^^^^^^^ help: replace this with: `i16::cast_unsigned(8i16)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:55:22
--> $DIR/unnecessary-transmutation.rs:59:22
|
LL | let x: i16 = transmute(x);
| ^^^^^^^^^^^^ help: replace this with: `u16::cast_signed(x)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:57:22
--> $DIR/unnecessary-transmutation.rs:61:22
|
LL | let x: u32 = transmute(4i32);
| ^^^^^^^^^^^^^^^ help: replace this with: `i32::cast_unsigned(4i32)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:59:22
--> $DIR/unnecessary-transmutation.rs:63:22
|
LL | let x: i32 = transmute(x);
| ^^^^^^^^^^^^ help: replace this with: `u32::cast_signed(x)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:61:22
--> $DIR/unnecessary-transmutation.rs:65:22
|
LL | let x: u64 = transmute(7i64);
| ^^^^^^^^^^^^^^^ help: replace this with: `i64::cast_unsigned(7i64)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:63:22
--> $DIR/unnecessary-transmutation.rs:67:22
|
LL | let x: i64 = transmute(x);
| ^^^^^^^^^^^^ help: replace this with: `u64::cast_signed(x)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:66:22
--> $DIR/unnecessary-transmutation.rs:70:22
|
LL | let y: f32 = transmute(1u32);
| ^^^^^^^^^^^^^^^ help: replace this with: `f32::from_bits(1u32)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:68:22
--> $DIR/unnecessary-transmutation.rs:72:22
|
LL | let y: u32 = transmute(y);
| ^^^^^^^^^^^^ help: replace this with: `f32::to_bits(y)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:70:22
--> $DIR/unnecessary-transmutation.rs:74:22
|
LL | let y: f64 = transmute(3u64);
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(3u64)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:72:22
--> $DIR/unnecessary-transmutation.rs:76:22
|
LL | let y: u64 = transmute(2.0);
| ^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(2.0)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:75:23
--> $DIR/unnecessary-transmutation.rs:79:22
|
LL | let y: f64 = transmute(1i64);
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(i64::cast_unsigned(1i64))`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:81:22
|
LL | let y: i64 = transmute(1f64);
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(1f64).cast_signed()`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:84:23
|
LL | let z: bool = transmute(1u8);
| ^^^^^^^^^^^^^^ help: replace this with: `(1u8 == 1)`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:77:21
--> $DIR/unnecessary-transmutation.rs:86:21
|
LL | let z: u8 = transmute(z);
| ^^^^^^^^^^^^ help: replace this with: `(z) as u8`

error: unnecessary transmute
--> $DIR/unnecessary-transmutation.rs:82:21
--> $DIR/unnecessary-transmutation.rs:91:21
|
LL | let z: i8 = transmute(z);
| ^^^^^^^^^^^^ help: replace this with: `(z) as i8`

error: aborting due to 32 previous errors
error: aborting due to 36 previous errors

Loading