3 files changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ struct Newtype(Opaque);
13
13
14
14
struct S {
15
15
i : i32 ,
16
- a : Opaque ,
16
+ j : i32 ,
17
+ a : Newtype ,
17
18
}
18
19
19
20
const NEWTYPE : ( ) = unsafe {
@@ -28,6 +29,10 @@ const OFFSET: () = unsafe {
28
29
let buf = [ 0i32 ; 4 ] ;
29
30
let x: & S = & * ( & buf as * const _ as * const S ) ;
30
31
32
+ // Accessing sized fields is perfectly fine, even at non-zero offsets.
33
+ let field = & x. i ;
34
+ let field = & x. j ;
35
+
31
36
// This needs to compute the field offset, but we don't know the type's alignment, so this
32
37
// fails.
33
38
let field = & x. a ;
Original file line number Diff line number Diff line change 1
1
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
3
3
|
4
4
LL | let field = &x.a;
5
5
| ^^^^ `extern type` does not have a known offset
Original file line number Diff line number Diff line change @@ -12,16 +12,22 @@ struct Newtype(Opaque);
12
12
13
13
struct S {
14
14
i : i32 ,
15
- a : Opaque ,
15
+ j : i32 ,
16
+ a : Newtype ,
16
17
}
17
18
18
19
fn main ( ) {
20
+ let buf = [ 0i32 ; 4 ] ;
21
+
22
+ let x: & Newtype = unsafe { & * ( & buf as * const _ as * const Newtype ) } ;
19
23
// Projecting to the newtype works, because it is always at offset 0.
20
- let x: & Newtype = unsafe { & * ( 1usize as * const Newtype ) } ;
21
24
let field = & x. 0 ;
22
25
26
+ let x: & S = unsafe { & * ( & buf as * const _ as * const S ) } ;
27
+ // Accessing sized fields is perfectly fine, even at non-zero offsets.
28
+ let field = & x. i ;
29
+ let field = & x. j ;
23
30
// This needs to compute the field offset, but we don't know the type's alignment,
24
31
// so this panics.
25
- let x: & S = unsafe { & * ( 1usize as * const S ) } ;
26
32
let field = & x. a ;
27
33
}
0 commit comments