make it more clear what comments refer to; avoid dangling unaligned references

Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
This commit is contained in:
Ralf Jung 2023-12-11 13:28:08 +01:00
parent b9c9b3e7a2
commit df227f78c6
2 changed files with 11 additions and 5 deletions

View File

@ -17,15 +17,21 @@ struct S {
}
const NEWTYPE: () = unsafe {
let buf = [0i32; 4];
let x: &Newtype = &*(&buf as *const _ as *const Newtype);
// Projecting to the newtype works, because it is always at offset 0.
let x: &Newtype = unsafe { &*(1usize as *const Newtype) };
let field = &x.0;
};
const OFFSET: () = unsafe {
// This needs to compute the field offset, but we don't know the type's alignment, so this fail.
let x: &S = unsafe { &*(1usize as *const S) };
let field = &x.a; //~ERROR: evaluation of constant value failed
let buf = [0i32; 4];
let x: &S = &*(&buf as *const _ as *const S);
// This needs to compute the field offset, but we don't know the type's alignment, so this
// fails.
let field = &x.a;
//~^ ERROR: evaluation of constant value failed
//~| does not have a known offset
};

View File

@ -1,5 +1,5 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-91827-extern-types-field-offset.rs:28:17
--> $DIR/issue-91827-extern-types-field-offset.rs:33:17
|
LL | let field = &x.a;
| ^^^^ `extern type` does not have a known offset