2022-02-10 17:40:06 +00:00
|
|
|
use super::TRANSMUTE_UNDEFINED_REPR;
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-02-26 13:26:21 +00:00
|
|
|
use clippy_utils::ty::is_c_void;
|
2022-02-10 17:40:06 +00:00
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
2022-03-16 16:54:32 +00:00
|
|
|
use rustc_middle::ty::subst::{Subst, SubstsRef};
|
2022-03-16 02:31:07 +00:00
|
|
|
use rustc_middle::ty::{self, IntTy, Ty, TypeAndMut, UintTy};
|
2022-02-10 17:40:06 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_lines)]
|
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
e: &'tcx Expr<'_>,
|
|
|
|
from_ty_orig: Ty<'tcx>,
|
|
|
|
to_ty_orig: Ty<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
let mut from_ty = cx.tcx.erase_regions(from_ty_orig);
|
|
|
|
let mut to_ty = cx.tcx.erase_regions(to_ty_orig);
|
|
|
|
|
|
|
|
while from_ty != to_ty {
|
|
|
|
match reduce_refs(cx, e.span, from_ty, to_ty) {
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTys::FromFatPtr {
|
|
|
|
unsized_ty,
|
|
|
|
to_ty: to_sub_ty,
|
|
|
|
} => match reduce_ty(cx, to_sub_ty) {
|
2022-03-16 02:33:26 +00:00
|
|
|
ReducedTy::TypeErasure => break,
|
2022-03-16 02:31:07 +00:00
|
|
|
ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break,
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTy::Ref(to_sub_ty) => {
|
|
|
|
from_ty = unsized_ty;
|
|
|
|
to_ty = to_sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute from `{}` which has an undefined layout", from_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if from_ty_orig.peel_refs() != unsized_ty {
|
|
|
|
diag.note(&format!("the contained type `&{}` has an undefined layout", unsized_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTys::ToFatPtr {
|
|
|
|
unsized_ty,
|
|
|
|
from_ty: from_sub_ty,
|
|
|
|
} => match reduce_ty(cx, from_sub_ty) {
|
2022-03-16 02:33:26 +00:00
|
|
|
ReducedTy::TypeErasure => break,
|
2022-03-16 02:31:07 +00:00
|
|
|
ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break,
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTy::Ref(from_sub_ty) => {
|
|
|
|
from_ty = from_sub_ty;
|
|
|
|
to_ty = unsized_ty;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute to `{}` which has an undefined layout", to_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if to_ty_orig.peel_refs() != unsized_ty {
|
|
|
|
diag.note(&format!("the contained type `&{}` has an undefined layout", unsized_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
|
|
|
ReducedTys::ToPtr {
|
|
|
|
from_ty: from_sub_ty,
|
|
|
|
to_ty: to_sub_ty,
|
|
|
|
} => match reduce_ty(cx, from_sub_ty) {
|
|
|
|
ReducedTy::UnorderedFields(from_ty) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute from `{}` which has an undefined layout", from_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if from_ty_orig.peel_refs() != from_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", from_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ReducedTy::Ref(from_sub_ty) => {
|
|
|
|
from_ty = from_sub_ty;
|
|
|
|
to_ty = to_sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => break,
|
|
|
|
},
|
|
|
|
ReducedTys::FromPtr {
|
|
|
|
from_ty: from_sub_ty,
|
|
|
|
to_ty: to_sub_ty,
|
|
|
|
} => match reduce_ty(cx, to_sub_ty) {
|
|
|
|
ReducedTy::UnorderedFields(to_ty) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute to `{}` which has an undefined layout", to_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if to_ty_orig.peel_refs() != to_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", to_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ReducedTy::Ref(to_sub_ty) => {
|
|
|
|
from_ty = from_sub_ty;
|
|
|
|
to_ty = to_sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => break,
|
|
|
|
},
|
|
|
|
ReducedTys::Other {
|
|
|
|
from_ty: from_sub_ty,
|
|
|
|
to_ty: to_sub_ty,
|
|
|
|
} => match (reduce_ty(cx, from_sub_ty), reduce_ty(cx, to_sub_ty)) {
|
2022-03-16 02:33:26 +00:00
|
|
|
(ReducedTy::TypeErasure, _) | (_, ReducedTy::TypeErasure) => return false,
|
2022-02-10 17:40:06 +00:00
|
|
|
(ReducedTy::UnorderedFields(from_ty), ReducedTy::UnorderedFields(to_ty)) if from_ty != to_ty => {
|
2022-03-16 16:54:32 +00:00
|
|
|
let same_adt_did = if let (ty::Adt(from_def, from_subs), ty::Adt(to_def, to_subs))
|
|
|
|
= (from_ty.kind(), to_ty.kind())
|
|
|
|
&& from_def == to_def
|
|
|
|
{
|
|
|
|
if same_except_params(from_subs, to_subs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Some(from_def.did())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-10 17:40:06 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!(
|
|
|
|
"transmute from `{}` to `{}`, both of which have an undefined layout",
|
|
|
|
from_ty_orig, to_ty_orig
|
|
|
|
),
|
|
|
|
|diag| {
|
2022-03-16 16:54:32 +00:00
|
|
|
if let Some(same_adt_did) = same_adt_did {
|
|
|
|
diag.note(&format!(
|
|
|
|
"two instances of the same generic type (`{}`) may have different layouts",
|
|
|
|
cx.tcx.item_name(same_adt_did)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
if from_ty_orig.peel_refs() != from_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", from_ty));
|
|
|
|
}
|
|
|
|
if to_ty_orig.peel_refs() != to_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", to_ty));
|
2022-02-10 17:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
(
|
|
|
|
ReducedTy::UnorderedFields(from_ty),
|
|
|
|
ReducedTy::Other(_) | ReducedTy::OrderedFields(_) | ReducedTy::Ref(_),
|
|
|
|
) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute from `{}` which has an undefined layout", from_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if from_ty_orig.peel_refs() != from_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", from_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
(
|
|
|
|
ReducedTy::Other(_) | ReducedTy::OrderedFields(_) | ReducedTy::Ref(_),
|
|
|
|
ReducedTy::UnorderedFields(to_ty),
|
|
|
|
) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
TRANSMUTE_UNDEFINED_REPR,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute into `{}` which has an undefined layout", to_ty_orig),
|
|
|
|
|diag| {
|
|
|
|
if to_ty_orig.peel_refs() != to_ty {
|
|
|
|
diag.note(&format!("the contained type `{}` has an undefined layout", to_ty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
(ReducedTy::Ref(from_sub_ty), ReducedTy::Ref(to_sub_ty)) => {
|
|
|
|
from_ty = from_sub_ty;
|
|
|
|
to_ty = to_sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
(
|
2022-03-16 16:54:32 +00:00
|
|
|
ReducedTy::OrderedFields(_) | ReducedTy::Ref(_) | ReducedTy::Other(_) | ReducedTy::Param,
|
|
|
|
ReducedTy::OrderedFields(_) | ReducedTy::Ref(_) | ReducedTy::Other(_) | ReducedTy::Param,
|
2022-02-10 17:40:06 +00:00
|
|
|
)
|
2022-03-16 16:54:32 +00:00
|
|
|
| (
|
|
|
|
ReducedTy::UnorderedFields(_) | ReducedTy::Param,
|
|
|
|
ReducedTy::UnorderedFields(_) | ReducedTy::Param,
|
|
|
|
) => break,
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReducedTys<'tcx> {
|
2022-02-26 13:26:21 +00:00
|
|
|
FromFatPtr { unsized_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
|
|
|
ToFatPtr { unsized_ty: Ty<'tcx>, from_ty: Ty<'tcx> },
|
2022-02-10 17:40:06 +00:00
|
|
|
ToPtr { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
|
|
|
FromPtr { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
|
|
|
Other { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:26:21 +00:00
|
|
|
/// Remove references so long as both types are references.
|
2022-02-10 17:40:06 +00:00
|
|
|
fn reduce_refs<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
mut from_ty: Ty<'tcx>,
|
|
|
|
mut to_ty: Ty<'tcx>,
|
|
|
|
) -> ReducedTys<'tcx> {
|
|
|
|
loop {
|
|
|
|
return match (from_ty.kind(), to_ty.kind()) {
|
|
|
|
(
|
2022-02-26 13:26:21 +00:00
|
|
|
&(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: from_sub_ty, .. })),
|
|
|
|
&(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: to_sub_ty, .. })),
|
2022-02-10 17:40:06 +00:00
|
|
|
) => {
|
|
|
|
from_ty = from_sub_ty;
|
|
|
|
to_ty = to_sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
(&(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })), _)
|
2022-02-10 17:40:06 +00:00
|
|
|
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
|
|
|
{
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTys::FromFatPtr { unsized_ty, to_ty }
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
(_, &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })))
|
2022-02-10 17:40:06 +00:00
|
|
|
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
|
|
|
{
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTys::ToFatPtr { unsized_ty, from_ty }
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
(&(ty::Ref(_, from_ty, _) | ty::RawPtr(TypeAndMut { ty: from_ty, .. })), _) => {
|
2022-02-10 17:40:06 +00:00
|
|
|
ReducedTys::FromPtr { from_ty, to_ty }
|
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
(_, &(ty::Ref(_, to_ty, _) | ty::RawPtr(TypeAndMut { ty: to_ty, .. }))) => {
|
2022-02-10 17:40:06 +00:00
|
|
|
ReducedTys::ToPtr { from_ty, to_ty }
|
|
|
|
},
|
|
|
|
_ => ReducedTys::Other { from_ty, to_ty },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReducedTy<'tcx> {
|
2022-02-26 13:26:21 +00:00
|
|
|
/// The type can be used for type erasure.
|
|
|
|
TypeErasure,
|
|
|
|
/// The type is a struct containing either zero non-zero sized fields, or multiple non-zero
|
|
|
|
/// sized fields with a defined order.
|
2022-02-10 17:40:06 +00:00
|
|
|
OrderedFields(Ty<'tcx>),
|
2022-02-26 13:26:21 +00:00
|
|
|
/// The type is a struct containing multiple non-zero sized fields with no defined order.
|
2022-02-10 17:40:06 +00:00
|
|
|
UnorderedFields(Ty<'tcx>),
|
2022-02-26 13:26:21 +00:00
|
|
|
/// The type is a reference to the contained type.
|
2022-02-10 17:40:06 +00:00
|
|
|
Ref(Ty<'tcx>),
|
2022-03-16 16:54:32 +00:00
|
|
|
/// The type is a generic parameter.
|
|
|
|
Param,
|
2022-02-26 13:26:21 +00:00
|
|
|
/// Any other type.
|
|
|
|
Other(Ty<'tcx>),
|
2022-02-10 17:40:06 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 13:26:21 +00:00
|
|
|
/// Reduce structs containing a single non-zero sized field to it's contained type.
|
2022-02-10 17:40:06 +00:00
|
|
|
fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> {
|
|
|
|
loop {
|
|
|
|
ty = cx.tcx.try_normalize_erasing_regions(cx.param_env, ty).unwrap_or(ty);
|
|
|
|
return match *ty.kind() {
|
2022-03-16 02:33:26 +00:00
|
|
|
ty::Array(sub_ty, _) if matches!(sub_ty.kind(), ty::Int(_) | ty::Uint(_)) => ReducedTy::TypeErasure,
|
2022-02-10 17:40:06 +00:00
|
|
|
ty::Array(sub_ty, _) | ty::Slice(sub_ty) => {
|
|
|
|
ty = sub_ty;
|
|
|
|
continue;
|
|
|
|
},
|
2022-02-26 13:26:21 +00:00
|
|
|
ty::Tuple(args) if args.is_empty() => ReducedTy::TypeErasure,
|
2022-02-10 17:40:06 +00:00
|
|
|
ty::Tuple(args) => {
|
2022-03-16 01:51:07 +00:00
|
|
|
let mut iter = args.iter();
|
|
|
|
let Some(sized_ty) = iter.find(|&ty| !is_zero_sized_ty(cx, ty)) else {
|
2022-02-10 17:40:06 +00:00
|
|
|
return ReducedTy::OrderedFields(ty);
|
|
|
|
};
|
2022-03-16 01:51:07 +00:00
|
|
|
if iter.all(|ty| is_zero_sized_ty(cx, ty)) {
|
2022-02-10 17:40:06 +00:00
|
|
|
ty = sized_ty;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ReducedTy::UnorderedFields(ty)
|
|
|
|
},
|
|
|
|
ty::Adt(def, substs) if def.is_struct() => {
|
|
|
|
let mut iter = def
|
|
|
|
.non_enum_variant()
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.map(|f| cx.tcx.type_of(f.did).subst(cx.tcx, substs));
|
2022-02-26 13:26:21 +00:00
|
|
|
let Some(sized_ty) = iter.find(|&ty| !is_zero_sized_ty(cx, ty)) else {
|
|
|
|
return ReducedTy::TypeErasure;
|
2022-02-10 17:40:06 +00:00
|
|
|
};
|
|
|
|
if iter.all(|ty| is_zero_sized_ty(cx, ty)) {
|
|
|
|
ty = sized_ty;
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-04 20:28:41 +00:00
|
|
|
if def.repr().inhibit_struct_field_reordering_opt() {
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTy::OrderedFields(ty)
|
|
|
|
} else {
|
|
|
|
ReducedTy::UnorderedFields(ty)
|
|
|
|
}
|
|
|
|
},
|
2022-03-04 20:28:41 +00:00
|
|
|
ty::Adt(def, _) if def.is_enum() && (def.variants().is_empty() || is_c_void(cx, ty)) => {
|
2022-02-26 13:26:21 +00:00
|
|
|
ReducedTy::TypeErasure
|
2022-02-10 17:40:06 +00:00
|
|
|
},
|
2022-03-16 02:38:15 +00:00
|
|
|
// TODO: Check if the conversion to or from at least one of a union's fields is valid.
|
|
|
|
ty::Adt(def, _) if def.is_union() => ReducedTy::TypeErasure,
|
2022-02-26 13:26:21 +00:00
|
|
|
ty::Foreign(_) => ReducedTy::TypeErasure,
|
|
|
|
ty::Ref(_, ty, _) => ReducedTy::Ref(ty),
|
|
|
|
ty::RawPtr(ty) => ReducedTy::Ref(ty.ty),
|
2022-03-16 16:54:32 +00:00
|
|
|
ty::Param(_) => ReducedTy::Param,
|
2022-02-10 17:40:06 +00:00
|
|
|
_ => ReducedTy::Other(ty),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_zero_sized_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
2022-02-10 18:52:01 +00:00
|
|
|
if_chain! {
|
|
|
|
if let Ok(ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, ty);
|
|
|
|
if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty));
|
|
|
|
then {
|
2022-03-04 02:46:56 +00:00
|
|
|
layout.layout.size().bytes() == 0
|
2022-02-10 18:52:01 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2022-02-10 17:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 02:31:07 +00:00
|
|
|
|
|
|
|
fn is_size_pair(ty: Ty<'_>) -> bool {
|
|
|
|
if let ty::Tuple(tys) = *ty.kind()
|
|
|
|
&& let [ty1, ty2] = &**tys
|
|
|
|
{
|
|
|
|
matches!(ty1.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize))
|
|
|
|
&& matches!(ty2.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize))
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 16:54:32 +00:00
|
|
|
|
|
|
|
fn same_except_params(subs1: SubstsRef<'_>, subs2: SubstsRef<'_>) -> bool {
|
|
|
|
// TODO: check const parameters as well. Currently this will consider `Array<5>` the same as
|
|
|
|
// `Array<6>`
|
|
|
|
for (ty1, ty2) in subs1.types().zip(subs2.types()).filter(|(ty1, ty2)| ty1 != ty2) {
|
|
|
|
match (ty1.kind(), ty2.kind()) {
|
|
|
|
(ty::Param(_), _) | (_, ty::Param(_)) => (),
|
|
|
|
(ty::Adt(adt1, subs1), ty::Adt(adt2, subs2)) if adt1 == adt2 && same_except_params(subs1, subs2) => (),
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|