Auto merge of #12403 - samueltardieu:issue-12402, r=blyxyas
Pointers cannot be converted to integers at compile time Fix #12402 changelog: [`transmutes_expressible_as_ptr_casts`]: do not suggest invalid const casts
This commit is contained in:
commit
c2dd413c79
4 changed files with 17 additions and 1 deletions
|
@ -592,7 +592,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
|
||||||
| (eager_transmute::check(cx, e, arg, from_ty, to_ty));
|
| (eager_transmute::check(cx, e, arg, from_ty, to_ty));
|
||||||
|
|
||||||
if !linted {
|
if !linted {
|
||||||
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, from_ty_adjusted, to_ty, arg);
|
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, from_ty_adjusted, to_ty, arg, const_context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,10 +18,12 @@ pub(super) fn check<'tcx>(
|
||||||
from_ty_adjusted: bool,
|
from_ty_adjusted: bool,
|
||||||
to_ty: Ty<'tcx>,
|
to_ty: Ty<'tcx>,
|
||||||
arg: &'tcx Expr<'_>,
|
arg: &'tcx Expr<'_>,
|
||||||
|
const_context: bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
|
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
|
||||||
let mut app = Applicability::MachineApplicable;
|
let mut app = Applicability::MachineApplicable;
|
||||||
let mut sugg = match check_cast(cx, e, from_ty, to_ty) {
|
let mut sugg = match check_cast(cx, e, from_ty, to_ty) {
|
||||||
|
Some(FnPtrAddrCast | PtrAddrCast) if const_context => return false,
|
||||||
Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => {
|
Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => {
|
||||||
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app)
|
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app)
|
||||||
.as_ty(to_ty.to_string())
|
.as_ty(to_ty.to_string())
|
||||||
|
|
|
@ -82,3 +82,10 @@ fn issue_10449() {
|
||||||
|
|
||||||
let _x: u8 = unsafe { *(f as *const u8) };
|
let _x: u8 = unsafe { *(f as *const u8) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pointers cannot be cast to integers in const contexts
|
||||||
|
const fn issue_12402<P>(ptr: *const P) {
|
||||||
|
unsafe { transmute::<*const i32, usize>(&42i32) };
|
||||||
|
unsafe { transmute::<fn(*const P), usize>(issue_12402) };
|
||||||
|
let _ = unsafe { transmute::<_, usize>(ptr) };
|
||||||
|
}
|
||||||
|
|
|
@ -82,3 +82,10 @@ fn issue_10449() {
|
||||||
|
|
||||||
let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
|
let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pointers cannot be cast to integers in const contexts
|
||||||
|
const fn issue_12402<P>(ptr: *const P) {
|
||||||
|
unsafe { transmute::<*const i32, usize>(&42i32) };
|
||||||
|
unsafe { transmute::<fn(*const P), usize>(issue_12402) };
|
||||||
|
let _ = unsafe { transmute::<_, usize>(ptr) };
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue