about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-02-15 14:33:01 +0100
committerGitHub <noreply@github.com>2024-02-15 14:33:01 +0100
commit12d70af446f8e382904e9d48f2a06fcaa33a5017 (patch)
tree69b8bdc8142189155595890a93ab50bba208b8ae
parent7d6c99dd066f3c84f694fc00c4865758d6ecd772 (diff)
parentddec8c5edc488e3eec27ed59a4d8e5dbaad4130f (diff)
downloadrust-12d70af446f8e382904e9d48f2a06fcaa33a5017.tar.gz
rust-12d70af446f8e382904e9d48f2a06fcaa33a5017.zip
Rollup merge of #121104 - Urgau:bigger_layout-fix-fp, r=compiler-errors
Ignore unsized types when trying to determine the size of the original type

Fixes https://github.com/rust-lang/rust/issues/121074 a regression from https://github.com/rust-lang/rust/pull/118983
-rw-r--r--compiler/rustc_lint/src/reference_casting.rs7
-rw-r--r--tests/ui/lint/reference_casting.rs5
2 files changed, 12 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/reference_casting.rs b/compiler/rustc_lint/src/reference_casting.rs
index 519ab8bd50f..f386db9d8db 100644
--- a/compiler/rustc_lint/src/reference_casting.rs
+++ b/compiler/rustc_lint/src/reference_casting.rs
@@ -207,6 +207,13 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
     }
 
     let from_layout = cx.layout_of(*inner_start_ty).ok()?;
+
+    // if the type isn't sized, we bail out, instead of potentially giving
+    // the user a meaningless warning.
+    if from_layout.is_unsized() {
+        return None;
+    }
+
     let alloc_layout = cx.layout_of(alloc_ty).ok()?;
     let to_layout = cx.layout_of(*inner_end_ty).ok()?;
 
diff --git a/tests/ui/lint/reference_casting.rs b/tests/ui/lint/reference_casting.rs
index 63541943d65..e5d84e464fd 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<dyn Send> = 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)
     }