Skip to content

Commit 97b84e4

Browse files
committed
add tests for niches in pointers
1 parent 7e565cc commit 97b84e4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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, mem};
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+
assert!(ptr.is_some());
20+
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
21+
assert_eq!(raw, 90);
22+
23+
let ptr = Some(Some(val));
24+
assert!(ptr.is_some());
25+
assert!(ptr.unwrap().is_some());
26+
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
27+
assert_eq!(raw, 90);
28+
29+
let ptr: Option<PointerWithRange> = None;
30+
assert!(ptr.is_none());
31+
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
32+
assert!(!(1..=100).contains(&raw));
33+
34+
let ptr: Option<Option<PointerWithRange>> = None;
35+
assert!(ptr.is_none());
36+
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
37+
assert!(!(1..=100).contains(&raw));
38+
}

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

+7
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,10 @@ 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+
240245
pub fn main() {
241246
assert_eq!(size_of::<u8>(), 1 as usize);
242247
assert_eq!(size_of::<u32>(), 4 as usize);
@@ -354,4 +359,6 @@ pub fn main() {
354359
assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr());
355360

356361

362+
assert_eq!(size_of::<Option<PointerWithRange>>(), size_of::<PointerWithRange>());
363+
assert_eq!(size_of::<Option<Option<PointerWithRange>>>(), size_of::<PointerWithRange>());
357364
}

0 commit comments

Comments
 (0)