also test projecting to some sized fields at non-zero offset in structs with an extern type tail

This commit is contained in:
Ralf Jung 2023-12-11 13:45:03 +01:00
parent a47416beb5
commit edcb7aba6b
3 changed files with 16 additions and 5 deletions

View File

@ -13,7 +13,8 @@
struct S { struct S {
i: i32, i: i32,
a: Opaque, j: i32,
a: Newtype,
} }
const NEWTYPE: () = unsafe { const NEWTYPE: () = unsafe {
@ -28,6 +29,10 @@ struct S {
let buf = [0i32; 4]; let buf = [0i32; 4];
let x: &S = &*(&buf as *const _ as *const S); let x: &S = &*(&buf as *const _ as *const S);
// Accessing sized fields is perfectly fine, even at non-zero offsets.
let field = &x.i;
let field = &x.j;
// This needs to compute the field offset, but we don't know the type's alignment, so this // This needs to compute the field offset, but we don't know the type's alignment, so this
// fails. // fails.
let field = &x.a; let field = &x.a;

View File

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

View File

@ -12,16 +12,22 @@
struct S { struct S {
i: i32, i: i32,
a: Opaque, j: i32,
a: Newtype,
} }
fn main() { fn main() {
let buf = [0i32; 4];
let x: &Newtype = unsafe { &*(&buf as *const _ as *const Newtype) };
// Projecting to the newtype works, because it is always at offset 0. // Projecting to the newtype works, because it is always at offset 0.
let x: &Newtype = unsafe { &*(1usize as *const Newtype) };
let field = &x.0; let field = &x.0;
let x: &S = unsafe { &*(&buf as *const _ as *const S) };
// Accessing sized fields is perfectly fine, even at non-zero offsets.
let field = &x.i;
let field = &x.j;
// This needs to compute the field offset, but we don't know the type's alignment, // This needs to compute the field offset, but we don't know the type's alignment,
// so this panics. // so this panics.
let x: &S = unsafe { &*(1usize as *const S) };
let field = &x.a; let field = &x.a;
} }