Skip to content

Commit

Permalink
make it more clear what comments refer to; avoid dangling unaligned r…
Browse files Browse the repository at this point in the history
…eferences

Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
  • Loading branch information
RalfJung and WaffleLapkin committed Dec 12, 2023
1 parent b9c9b3e commit df227f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit df227f7

Please sign in to comment.