From df227f78c68999e4f453767a17eae6434f8ac82a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 11 Dec 2023 13:28:08 +0100 Subject: [PATCH] make it more clear what comments refer to; avoid dangling unaligned references Co-authored-by: Waffle Maybe --- .../issue-91827-extern-types-field-offset.rs | 14 ++++++++++---- .../issue-91827-extern-types-field-offset.stderr | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs b/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs index 7384e71db3a7a..0a6d31204b774 100644 --- a/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs +++ b/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs @@ -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 }; diff --git a/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr b/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr index 645ad8121bcd4..a45c548584e08 100644 --- a/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr +++ b/tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr @@ -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