Skip to content

Commit 11a83ee

Browse files
authored
Unrolled build for rust-lang#140435
Rollup merge of rust-lang#140435 - bend-n:use_ux_from_instead_of_bool_as_ux_unnecessary_transmutes, r=compiler-errors use uX::from instead of _ as uX in non - const contexts changes `transmute(bool) -> integer` to `integer::from` as opposed to `bool as integer`. rust-lang#136083 (comment) `@rustbot` label L-unnecessary_transmutes
2 parents 2eef478 + 8373bb1 commit 11a83ee

File tree

4 files changed

+130
-46
lines changed

4 files changed

+130
-46
lines changed

compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
3030
function: &Operand<'tcx>,
3131
arg: String,
3232
span: Span,
33+
is_in_const: bool,
3334
) -> Option<Error> {
3435
let fn_sig = function.ty(self.body, self.tcx).fn_sig(self.tcx).skip_binder();
3536
let [input] = fn_sig.inputs() else { return None };
@@ -97,8 +98,14 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
9798
)),
9899
// uNN → fNN
99100
(Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())),
100-
// bool → { x8 }
101-
(Bool, Int(..) | Uint(..)) => err(format!("({arg}) as {}", fn_sig.output())),
101+
// bool → { x8 } in const context since `From::from` is not const yet
102+
// FIXME: is it possible to know when the parentheses arent necessary?
103+
// FIXME(const_traits): Remove this when From::from is constified?
104+
(Bool, Int(..) | Uint(..)) if is_in_const => {
105+
err(format!("({arg}) as {}", fn_sig.output()))
106+
}
107+
// " using `x8::from`
108+
(Bool, Int(..) | Uint(..)) => err(format!("{}::from({arg})", fn_sig.output())),
102109
_ => return None,
103110
})
104111
}
@@ -114,7 +121,13 @@ impl<'tcx> Visitor<'tcx> for UnnecessaryTransmuteChecker<'_, 'tcx> {
114121
&& self.tcx.is_intrinsic(func_def_id, sym::transmute)
115122
&& let span = self.body.source_info(location).span
116123
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(arg)
117-
&& let Some(lint) = self.is_unnecessary_transmute(func, snippet, span)
124+
&& let def_id = self.body.source.def_id()
125+
&& let Some(lint) = self.is_unnecessary_transmute(
126+
func,
127+
snippet,
128+
span,
129+
self.tcx.hir_body_const_context(def_id.expect_local()).is_some(),
130+
)
118131
&& let Some(hir_id) = terminator.source_info.scope.lint_root(&self.body.source_scopes)
119132
{
120133
self.tcx.emit_node_span_lint(UNNECESSARY_TRANSMUTES, hir_id, span, lint);

tests/ui/transmute/unnecessary-transmutation.fixed

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,27 @@ pub fn bytes_at_home(x: u32) -> [u8; 4] {
88
//~^ ERROR
99
}
1010

11+
pub const fn intinator_const(from: bool) -> u8 {
12+
unsafe { (from) as u8 }
13+
//~^ ERROR
14+
}
15+
16+
pub static X: u8 = unsafe { (true) as u8 };
17+
//~^ ERROR
18+
pub const Y: u8 = unsafe { (true) as u8 };
19+
//~^ ERROR
20+
21+
pub struct Z {}
22+
impl Z {
23+
pub const fn intinator_assoc(x: bool) -> u8 {
24+
unsafe { (x) as u8 }
25+
//~^ ERROR
26+
}
27+
}
28+
1129
fn main() {
30+
const { unsafe { (true) as u8 } };
31+
//~^ ERROR
1232
unsafe {
1333
let x: u16 = u16::from_ne_bytes(*b"01");
1434
//~^ ERROR
@@ -83,12 +103,12 @@ fn main() {
83103

84104
let z: bool = transmute(1u8);
85105
// clippy
86-
let z: u8 = (z) as u8;
106+
let z: u8 = u8::from(z);
87107
//~^ ERROR
88108

89109
let z: bool = transmute(1i8);
90110
// clippy
91-
let z: i8 = (z) as i8;
111+
let z: i8 = i8::from(z);
92112
//~^ ERROR
93113
}
94114
}

tests/ui/transmute/unnecessary-transmutation.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,27 @@ pub fn bytes_at_home(x: u32) -> [u8; 4] {
88
//~^ ERROR
99
}
1010

11+
pub const fn intinator_const(from: bool) -> u8 {
12+
unsafe { transmute(from) }
13+
//~^ ERROR
14+
}
15+
16+
pub static X: u8 = unsafe { transmute(true) };
17+
//~^ ERROR
18+
pub const Y: u8 = unsafe { transmute(true) };
19+
//~^ ERROR
20+
21+
pub struct Z {}
22+
impl Z {
23+
pub const fn intinator_assoc(x: bool) -> u8 {
24+
unsafe { transmute(x) }
25+
//~^ ERROR
26+
}
27+
}
28+
1129
fn main() {
30+
const { unsafe { transmute::<_, u8>(true) } };
31+
//~^ ERROR
1232
unsafe {
1333
let x: u16 = transmute(*b"01");
1434
//~^ ERROR

0 commit comments

Comments
 (0)