Fix a fp in transmute_ptr_to_ptr

Avoid firing the lint when `transmute` in const contexts
as dereferencing raw pointers in consts is unstable. cc #5959
This commit is contained in:
rail 2020-09-01 11:21:48 +12:00
parent 67e18c2d5c
commit afeb917fca
2 changed files with 12 additions and 2 deletions

View File

@ -331,8 +331,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id(); if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::TRANSMUTE); if match_def_path(cx, def_id, &paths::TRANSMUTE);
then { then {
// Avoid suggesting from/to bits in const contexts. // Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`. // See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
let const_context = in_constant(cx, e.hir_id); let const_context = in_constant(cx, e.hir_id);
let from_ty = cx.typeck_results().expr_ty(&args[0]); let from_ty = cx.typeck_results().expr_ty(&args[0]);
@ -486,7 +487,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
Applicability::Unspecified, Applicability::Unspecified,
); );
} else { } else {
if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) { if (cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty))
&& !const_context {
span_lint_and_then( span_lint_and_then(
cx, cx,
TRANSMUTE_PTR_TO_PTR, TRANSMUTE_PTR_TO_PTR,

View File

@ -51,4 +51,12 @@ fn transmute_ptr_to_ptr() {
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { std::mem::transmute(&GenericParam { t: &lp }) }; let _: &GenericParam<&LifetimeParam<'static>> = unsafe { std::mem::transmute(&GenericParam { t: &lp }) };
} }
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
const _: &() = {
struct ZST;
let zst = &ZST;
unsafe { std::mem::transmute::<&'static ZST, &'static ()>(zst) }
};
fn main() {} fn main() {}