Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore unsized types when trying to determine the size of the original type #121104

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down