Skip to content

Commit b029a86

Browse files
committed
Auto merge of #8648 - Jarcho:transmute_collection_7706, r=xFrednet
Fix `unsound_collection_transmute` fixes #7706 changelog: Better check size and alignment requirements in `unsound_collection_transmute`
2 parents a63308b + d5e887c commit b029a86

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

clippy_lints/src/transmute/utils.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use clippy_utils::last_path_segment;
22
use clippy_utils::source::snippet;
3-
use clippy_utils::ty::is_normalizable;
43
use if_chain::if_chain;
54
use rustc_hir::{Expr, GenericArg, QPath, TyKind};
65
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, cast::CastKind, Ty};
6+
use rustc_middle::ty::{cast::CastKind, Ty};
87
use rustc_span::DUMMY_SP;
98
use rustc_typeck::check::{cast::CastCheck, FnCtxt, Inherited};
109

@@ -34,15 +33,12 @@ pub(super) fn get_type_snippet(cx: &LateContext<'_>, path: &QPath<'_>, to_ref_ty
3433
// check if the component types of the transmuted collection and the result have different ABI,
3534
// size or alignment
3635
pub(super) fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
37-
let empty_param_env = ty::ParamEnv::empty();
38-
// check if `from` and `to` are normalizable to avoid ICE (#4968)
39-
if !(is_normalizable(cx, empty_param_env, from) && is_normalizable(cx, empty_param_env, to)) {
40-
return false;
41-
}
42-
let from_ty_layout = cx.tcx.layout_of(empty_param_env.and(from));
43-
let to_ty_layout = cx.tcx.layout_of(empty_param_env.and(to));
44-
if let (Ok(from_layout), Ok(to_layout)) = (from_ty_layout, to_ty_layout) {
45-
from_layout.size != to_layout.size || from_layout.align != to_layout.align || from_layout.abi != to_layout.abi
36+
if let Ok(from) = cx.tcx.try_normalize_erasing_regions(cx.param_env, from)
37+
&& let Ok(to) = cx.tcx.try_normalize_erasing_regions(cx.param_env, to)
38+
&& let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from))
39+
&& let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to))
40+
{
41+
from_layout.size != to_layout.size || from_layout.align.abi != to_layout.align.abi
4642
} else {
4743
// no idea about layout, so don't lint
4844
false

tests/ui/transmute_collection.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::unsound_collection_transmute)]
22

33
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
4-
use std::mem::transmute;
4+
use std::mem::{transmute, MaybeUninit};
55

66
fn main() {
77
unsafe {
@@ -43,5 +43,8 @@ fn main() {
4343
// wrong layout
4444
let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u8, [u8; 4]>::new());
4545
let _ = transmute::<_, HashMap<u32, u32>>(HashMap::<[u8; 4], u32>::new());
46+
47+
let _ = transmute::<_, Vec<u8>>(Vec::<MaybeUninit<u8>>::new());
48+
let _ = transmute::<_, Vec<*mut u32>>(Vec::<Box<u32>>::new());
4649
}
4750
}

0 commit comments

Comments
 (0)