Allow transmuting fat pointers to some types in transmute_undefined_repr
This commit is contained in:
parent
447a24588a
commit
7c07022c98
@ -19,7 +19,9 @@ pub(super) fn check<'tcx>(
|
|||||||
|
|
||||||
while from_ty != to_ty {
|
while from_ty != to_ty {
|
||||||
match reduce_refs(cx, e.span, from_ty, to_ty) {
|
match reduce_refs(cx, e.span, from_ty, to_ty) {
|
||||||
ReducedTys::FromFatPtr { unsized_ty, .. } => {
|
ReducedTys::FromFatPtr { unsized_ty, to_ty } => match reduce_ty(cx, to_ty) {
|
||||||
|
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
|
||||||
|
_ => {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
TRANSMUTE_UNDEFINED_REPR,
|
TRANSMUTE_UNDEFINED_REPR,
|
||||||
@ -33,7 +35,10 @@ pub(super) fn check<'tcx>(
|
|||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
ReducedTys::ToFatPtr { unsized_ty, .. } => {
|
},
|
||||||
|
ReducedTys::ToFatPtr { unsized_ty, from_ty } => match reduce_ty(cx, from_ty) {
|
||||||
|
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
|
||||||
|
_ => {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
TRANSMUTE_UNDEFINED_REPR,
|
TRANSMUTE_UNDEFINED_REPR,
|
||||||
@ -47,6 +52,7 @@ pub(super) fn check<'tcx>(
|
|||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
},
|
||||||
ReducedTys::ToPtr {
|
ReducedTys::ToPtr {
|
||||||
from_ty: from_sub_ty,
|
from_ty: from_sub_ty,
|
||||||
to_ty: to_sub_ty,
|
to_ty: to_sub_ty,
|
||||||
@ -184,8 +190,8 @@ pub(super) fn check<'tcx>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum ReducedTys<'tcx> {
|
enum ReducedTys<'tcx> {
|
||||||
FromFatPtr { unsized_ty: Ty<'tcx> },
|
FromFatPtr { unsized_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
||||||
ToFatPtr { unsized_ty: Ty<'tcx> },
|
ToFatPtr { unsized_ty: Ty<'tcx>, from_ty: Ty<'tcx> },
|
||||||
ToPtr { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
ToPtr { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
||||||
FromPtr { 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> },
|
Other { from_ty: Ty<'tcx>, to_ty: Ty<'tcx> },
|
||||||
@ -211,12 +217,12 @@ fn reduce_refs<'tcx>(
|
|||||||
(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. }), _)
|
(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. }), _)
|
||||||
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
||||||
{
|
{
|
||||||
ReducedTys::FromFatPtr { unsized_ty }
|
ReducedTys::FromFatPtr { unsized_ty, to_ty }
|
||||||
},
|
},
|
||||||
(_, ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. }))
|
(_, ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. }))
|
||||||
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
if !unsized_ty.is_sized(cx.tcx.at(span), cx.param_env) =>
|
||||||
{
|
{
|
||||||
ReducedTys::ToFatPtr { unsized_ty }
|
ReducedTys::ToFatPtr { unsized_ty, from_ty }
|
||||||
},
|
},
|
||||||
(ty::Ref(_, from_ty, _) | ty::RawPtr(TypeAndMut { ty: from_ty, .. }), _) => {
|
(ty::Ref(_, from_ty, _) | ty::RawPtr(TypeAndMut { ty: from_ty, .. }), _) => {
|
||||||
ReducedTys::FromPtr { from_ty, to_ty }
|
ReducedTys::FromPtr { from_ty, to_ty }
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#![warn(clippy::transmute_undefined_repr)]
|
#![warn(clippy::transmute_undefined_repr)]
|
||||||
#![allow(clippy::unit_arg)]
|
#![allow(clippy::unit_arg, clippy::transmute_ptr_to_ref)]
|
||||||
|
|
||||||
use core::ffi::c_void;
|
use core::ffi::c_void;
|
||||||
use core::mem::transmute;
|
use core::mem::{size_of, transmute};
|
||||||
|
|
||||||
fn value<T>() -> T {
|
fn value<T>() -> T {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
@ -72,5 +72,17 @@ fn main() {
|
|||||||
);
|
);
|
||||||
let _: *const Erase2 = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
|
let _: *const Erase2 = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
|
||||||
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase2>()); // Ok, reverse type erasure
|
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase2>()); // Ok, reverse type erasure
|
||||||
|
|
||||||
|
let _: *const () = transmute(value::<&&[u8]>()); // Ok, type erasure
|
||||||
|
let _: &&[u8] = transmute(value::<*const ()>()); // Ok, reverse type erasure
|
||||||
|
|
||||||
|
let _: *mut c_void = transmute(value::<&mut &[u8]>()); // Ok, type erasure
|
||||||
|
let _: &mut &[u8] = transmute(value::<*mut c_void>()); // Ok, reverse type erasure
|
||||||
|
|
||||||
|
let _: [u8; size_of::<&[u8]>()] = transmute(value::<&[u8]>()); // Ok, transmute to byte array
|
||||||
|
let _: &[u8] = transmute(value::<[u8; size_of::<&[u8]>()]>()); // Ok, transmute from byte array
|
||||||
|
|
||||||
|
let _: [usize; 2] = transmute(value::<&[u8]>()); // Ok, transmute to int array
|
||||||
|
let _: &[u8] = transmute(value::<[usize; 2]>()); // Ok, transmute from int array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user