Skip to content

Commit

Permalink
Ignore unsized types when trying to determine the size of a type
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Feb 14, 2024
1 parent 502ce82 commit 8aca71f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
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

0 comments on commit 8aca71f

Please sign in to comment.