mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
add tests for niches in pointers
This commit is contained in:
parent
7e565cce6a
commit
97b84e40cb
38
tests/ui/enum-discriminant/ptr_niche.rs
Normal file
38
tests/ui/enum-discriminant/ptr_niche.rs
Normal 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));
|
||||||
|
}
|
@ -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>());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user