Skip to content

Commit 289044f

Browse files
committed
also test projecting to some sized fields at non-zero offset in structs with an extern type tail
1 parent 5f5e6b9 commit 289044f

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Diff for: tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ struct Newtype(Opaque);
1313

1414
struct S {
1515
i: i32,
16-
a: Opaque,
16+
j: i32,
17+
a: Newtype,
1718
}
1819

1920
const NEWTYPE: () = unsafe {
@@ -28,6 +29,10 @@ const OFFSET: () = unsafe {
2829
let buf = [0i32; 4];
2930
let x: &S = &*(&buf as *const _ as *const S);
3031

32+
// Accessing sized fields is perfectly fine, even at non-zero offsets.
33+
let field = &x.i;
34+
let field = &x.j;
35+
3136
// This needs to compute the field offset, but we don't know the type's alignment, so this
3237
// fails.
3338
let field = &x.a;

Diff for: tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr

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

Diff for: tests/ui/extern/extern-types-field-offset.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ struct Newtype(Opaque);
1111

1212
struct S {
1313
i: i32,
14-
a: Opaque,
14+
j: i32,
15+
a: Newtype,
1516
}
1617

1718
fn main() {
19+
let buf = [0i32; 4];
20+
21+
let x: &Newtype = unsafe { &*(&buf as *const _ as *const Newtype) };
1822
// Projecting to the newtype works, because it is always at offset 0.
19-
let x: &Newtype = unsafe { &*(1usize as *const Newtype) };
2023
let field = &x.0;
2124

25+
let x: &S = unsafe { &*(&buf as *const _ as *const S) };
26+
// Accessing sized fields is perfectly fine, even at non-zero offsets.
27+
let field = &x.i;
28+
let field = &x.j;
2229
// This needs to compute the field offset, but we don't know the type's alignment,
2330
// so this panics.
24-
let x: &S = unsafe { &*(1usize as *const S) };
2531
let field = &x.a;
2632
}

0 commit comments

Comments
 (0)