-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reject projecting to fields whose offset we cannot compute
- Loading branch information
Showing
8 changed files
with
103 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Test that we can handle unsized types with an extern type tail part. | ||
// Regression test for issue #91827. | ||
|
||
#![feature(extern_types)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
extern "C" { | ||
type Opaque; | ||
} | ||
|
||
struct Newtype(Opaque); | ||
|
||
struct S { | ||
i: i32, | ||
a: Opaque, | ||
} | ||
|
||
const NEWTYPE: () = unsafe { | ||
// 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 | ||
//~| does not have a known offset | ||
}; | ||
|
||
fn main() {} |
9 changes: 9 additions & 0 deletions
9
tests/ui/consts/const-eval/issue-91827-extern-types-field-offset.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/issue-91827-extern-types-field-offset.rs:28:17 | ||
| | ||
LL | let field = &x.a; | ||
| ^^^^ `extern type` does not have a known offset | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
tests/ui/consts/const-eval/issue-91827-extern-types.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0080]: could not evaluate static initializer | ||
--> $DIR/issue-91827-extern-types.rs:50:15 | ||
| | ||
LL | unsafe { (addr_of!(list.tail) as *const u8).offset_from(list as *const List<T> as *const u8) } | ||
| ^^^^^^^^^^^^^^^^^^^ `extern type` does not have known offset | ||
| | ||
note: inside `tail_offset::<u128>` | ||
--> $DIR/issue-91827-extern-types.rs:50:15 | ||
| | ||
LL | unsafe { (addr_of!(list.tail) as *const u8).offset_from(list as *const List<T> as *const u8) } | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
note: inside `A_TAIL_OFFSET` | ||
--> $DIR/issue-91827-extern-types.rs:47:35 | ||
| | ||
LL | pub static A_TAIL_OFFSET: isize = tail_offset(A.as_list()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: this error originates in the macro `addr_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// run-fail | ||
// check-run-results | ||
// normalize-stderr-test "panicking\.rs:\d+:\d+:" -> "panicking.rs:" | ||
#![feature(extern_types)] | ||
|
||
extern "C" { | ||
type Opaque; | ||
} | ||
|
||
struct Newtype(Opaque); | ||
|
||
struct S { | ||
i: i32, | ||
a: Opaque, | ||
} | ||
|
||
fn main() { | ||
// Projecting to the newtype works, because it is always at offset 0. | ||
let x: &Newtype = unsafe { &*(1usize as *const Newtype) }; | ||
let field = &x.0; | ||
|
||
// This needs to compute the field offset, but we don't know the type's alignment, | ||
// so this panics. | ||
let x: &S = unsafe { &*(1usize as *const S) }; | ||
let field = &x.a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
thread 'main' panicked at library/core/src/panicking.rs: | ||
attempted to compute the size or alignment of extern type `Opaque` | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | ||
thread caused non-unwinding panic. aborting. |