Skip to content

Commit 0892dbd

Browse files
committed
add tests for niches in pointers
1 parent 7e565cc commit 0892dbd

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ run-pass
2+
//! Check that we can codegen setting and getting discriminants, including non-null niches,
3+
//! for enums with a pointer-like ABI. This used to crash llvm.
4+
5+
#![feature(rustc_attrs)]
6+
use std::ptr;
7+
8+
9+
#[rustc_layout_scalar_valid_range_start(1)]
10+
#[rustc_layout_scalar_valid_range_end(100)]
11+
#[derive(Copy, Clone)]
12+
struct PointerWithRange(#[allow(dead_code)] *const u8);
13+
14+
15+
fn main() {
16+
let val = unsafe { PointerWithRange(ptr::without_provenance(90)) };
17+
18+
let ptr = Some(val);
19+
let Some(_) = ptr else { unreachable!() };
20+
let ptr = Some(Some(val));
21+
let Some(Some(_)) = ptr else { unreachable!() };
22+
let ptr: Option<Option<PointerWithRange>> = None;
23+
let None = ptr else { unreachable!() };
24+
}

tests/ui/structs-enums/type-sizes.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(dead_code)]
66
#![feature(never_type)]
77
#![feature(pointer_is_aligned_to)]
8+
#![feature(rustc_attrs)]
89

910
use std::mem::size_of;
1011
use std::num::NonZero;
@@ -237,6 +238,15 @@ struct VecDummy {
237238
len: usize,
238239
}
239240

241+
#[rustc_layout_scalar_valid_range_start(1)]
242+
#[rustc_layout_scalar_valid_range_end(100)]
243+
struct PointerWithRange(#[allow(dead_code)] *const u8);
244+
245+
const _: () = {
246+
assert!(size_of::<Option<PointerWithRange>>() == size_of::<PointerWithRange>());
247+
assert!(size_of::<Option<Option<PointerWithRange>>>() == size_of::<PointerWithRange>());
248+
};
249+
240250
pub fn main() {
241251
assert_eq!(size_of::<u8>(), 1 as usize);
242252
assert_eq!(size_of::<u32>(), 4 as usize);
@@ -354,4 +364,6 @@ pub fn main() {
354364
assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr());
355365

356366

367+
assert_eq!(size_of::<Option<PointerWithRange>>(), size_of::<PointerWithRange>());
368+
assert_eq!(size_of::<Option<Option<PointerWithRange>>>(), size_of::<PointerWithRange>());
357369
}

0 commit comments

Comments
 (0)