add tests for niches in pointers

This commit is contained in:
The 8472 2024-11-30 15:07:06 +01:00
parent 7e565cce6a
commit 97b84e40cb
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,38 @@
//@ run-pass
//! Check that we can codegen setting and getting discriminants, including non-null niches,
//! for enums with a pointer-like ABI. This used to crash llvm.
#![feature(rustc_attrs)]
use std::{ptr, mem};
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_layout_scalar_valid_range_end(100)]
#[derive(Copy, Clone)]
struct PointerWithRange(#[allow(dead_code)] *const u8);
fn main() {
let val = unsafe { PointerWithRange(ptr::without_provenance(90)) };
let ptr = Some(val);
assert!(ptr.is_some());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert_eq!(raw, 90);
let ptr = Some(Some(val));
assert!(ptr.is_some());
assert!(ptr.unwrap().is_some());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert_eq!(raw, 90);
let ptr: Option<PointerWithRange> = None;
assert!(ptr.is_none());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert!(!(1..=100).contains(&raw));
let ptr: Option<Option<PointerWithRange>> = None;
assert!(ptr.is_none());
let raw = unsafe { mem::transmute::<_, usize>(ptr) };
assert!(!(1..=100).contains(&raw));
}

View File

@ -5,6 +5,7 @@
#![allow(dead_code)] #![allow(dead_code)]
#![feature(never_type)] #![feature(never_type)]
#![feature(pointer_is_aligned_to)] #![feature(pointer_is_aligned_to)]
#![feature(rustc_attrs)]
use std::mem::size_of; use std::mem::size_of;
use std::num::NonZero; use std::num::NonZero;
@ -237,6 +238,10 @@ struct VecDummy {
len: usize, len: usize,
} }
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_layout_scalar_valid_range_end(100)]
struct PointerWithRange(#[allow(dead_code)] *const u8);
pub fn main() { pub fn main() {
assert_eq!(size_of::<u8>(), 1 as usize); assert_eq!(size_of::<u8>(), 1 as usize);
assert_eq!(size_of::<u32>(), 4 as usize); assert_eq!(size_of::<u32>(), 4 as usize);
@ -354,4 +359,6 @@ pub fn main() {
assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr()); assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr());
assert_eq!(size_of::<Option<PointerWithRange>>(), size_of::<PointerWithRange>());
assert_eq!(size_of::<Option<Option<PointerWithRange>>>(), size_of::<PointerWithRange>());
} }