-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
invalid_reference_casting false positive: dynamically sized types #121074
Comments
I believe this is a false positive. rust-lang/rust#121074 error: casting references to a bigger memory layout than the backing allocation is undefined behavior, even if the reference is unused --> src/arena.rs:81:30 | 81 | let arena = unsafe { &*(&**arena as *const dyn Send as *const Arena<T>) }; | ^^^^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | backing allocation comes from here | = note: casting from `dyn Send` (0 bytes) to `Arena<T>` (56 bytes) = note: `#[deny(invalid_reference_casting)]` on by default
The There should probably be an rust/compiler/rustc_lint/src/reference_casting.rs Lines 193 to 195 in cc1c099
|
My code is also regressing in this: pub struct Opaque(());
pub unsafe fn foo(opaque: *const Opaque) {
let foo = &*((&*opaque) as *const Opaque as *const u8);
} Gives the error on nightly
This doesn't involve DST though. But the pattern is the same as we just go through a |
@ogoffart Your sample code is as far as I can tell UB and Miri seems to agree with the lint.
Note, removing the inner dereference If you want to continue this conversation, let's create a new issue as to not diverge this specific issue. |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-high |
Rollup merge of rust-lang#121104 - Urgau:bigger_layout-fix-fp, r=compiler-errors Ignore unsized types when trying to determine the size of the original type Fixes rust-lang#121074 a regression from rust-lang#118983
I believe this code is well defined.
It runs successfully in Miri with
RUSTFLAGS='-Zcrate-attr=allow(invalid_reference_casting)' cargo +nightly miri run
.However, since #118983 (nightly-2024-02-14), the following deny-by-default lint is triggered in
cargo check
:Rustc thinks the
dyn Send
is 0 bytes, whereas for the purpose of the memory model as I understand it, it has a runtime size that is bigger than 0 bytes.The text was updated successfully, but these errors were encountered: