]> git.proxmox.com Git - rustc.git/blame - tests/ui/consts/const-eval/issue-91827-extern-types.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / consts / const-eval / issue-91827-extern-types.rs
CommitLineData
a2a8927a
XL
1// run-pass
2//
3// Test that we can handle unsized types with an extern type tail part.
4// Regression test for issue #91827.
5
a2a8927a
XL
6#![feature(extern_types)]
7
8use std::ptr::addr_of;
9
10extern "C" {
11 type Opaque;
12}
13
14unsafe impl Sync for Opaque {}
15
16#[repr(C)]
17pub struct List<T> {
18 len: usize,
19 data: [T; 0],
20 tail: Opaque,
21}
22
23#[repr(C)]
24pub struct ListImpl<T, const N: usize> {
25 len: usize,
26 data: [T; N],
27}
28
29impl<T> List<T> {
30 const fn as_slice(&self) -> &[T] {
31 unsafe { std::slice::from_raw_parts(self.data.as_ptr(), self.len) }
32 }
33}
34
35impl<T, const N: usize> ListImpl<T, N> {
36 const fn as_list(&self) -> &List<T> {
37 unsafe { std::mem::transmute(self) }
38 }
39}
40
41pub static A: ListImpl<u128, 3> = ListImpl {
42 len: 3,
43 data: [5, 6, 7],
44};
45pub static A_REF: &'static List<u128> = A.as_list();
46pub static A_TAIL_OFFSET: isize = tail_offset(A.as_list());
47
48const fn tail_offset<T>(list: &List<T>) -> isize {
49 unsafe { (addr_of!(list.tail) as *const u8).offset_from(list as *const List<T> as *const u8) }
50}
51
52fn main() {
53 assert_eq!(A_REF.as_slice(), &[5, 6, 7]);
54 // Check that interpreter and code generation agree about the position of the tail field.
55 assert_eq!(A_TAIL_OFFSET, tail_offset(A_REF));
56}