From 8aca71f00c4b069347706b6a33547ca68fcbd26f Mon Sep 17 00:00:00 2001 From: Urgau Date: Wed, 14 Feb 2024 19:12:19 +0100 Subject: [PATCH] Ignore unsized types when trying to determine the size of a type --- compiler/rustc_lint/src/reference_casting.rs | 6 ++++++ tests/ui/lint/reference_casting.rs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/compiler/rustc_lint/src/reference_casting.rs b/compiler/rustc_lint/src/reference_casting.rs index 519ab8bd50fe2..c09e5ebfdb742 100644 --- a/compiler/rustc_lint/src/reference_casting.rs +++ b/compiler/rustc_lint/src/reference_casting.rs @@ -194,6 +194,12 @@ fn is_cast_to_bigger_memory_layout<'tcx>( return None; }; + // if the type is not Sized we cannot know by definition it's size + // so we bail out + if !inner_start_ty.is_sized(cx.tcx, cx.param_env) { + return None; + } + // try to find the underlying allocation let e_alloc = cx.expr_or_init(e); let e_alloc = diff --git a/tests/ui/lint/reference_casting.rs b/tests/ui/lint/reference_casting.rs index 63541943d65c1..e5d84e464fdc4 100644 --- a/tests/ui/lint/reference_casting.rs +++ b/tests/ui/lint/reference_casting.rs @@ -239,6 +239,11 @@ unsafe fn bigger_layout() { //~^ ERROR casting references to a bigger memory layout } + { + let x: Box = Box::new(0i32); + let _z = unsafe { &*(&*x as *const dyn Send as *const i32) }; + } + unsafe fn from_ref(this: &i32) -> &i64 { &*(this as *const i32 as *const i64) }