2 use core::intrinsics::assume;
5 fn test_typeid_sized_types() {
9 assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
10 assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
11 assert!(TypeId::of::<X>() != TypeId::of::<Y>());
15 fn test_typeid_unsized_types() {
18 struct Y(dyn Z + 'static);
20 assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
21 assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
22 assert!(TypeId::of::<X>() != TypeId::of::<Y>());
25 // Check that `const_assume` feature allow `assume` intrinsic
26 // to be used in const contexts.
28 fn test_assume_can_be_in_const_contexts() {
29 const unsafe fn foo(x: usize, y: usize) -> usize {
30 // SAFETY: the entire function is not safe,
31 // but it is just an example not used elsewhere.
32 unsafe { assume(y != 0) };
35 let rs = unsafe { foo(42, 97) };
40 const fn test_write_bytes_in_const_contexts() {
41 use core::intrinsics::write_bytes;
43 const TEST: [u32; 3] = {
44 let mut arr = [1u32, 2, 3];
46 write_bytes(arr.as_mut_ptr(), 0, 2);
51 assert!(TEST[0] == 0);
52 assert!(TEST[1] == 0);
53 assert!(TEST[2] == 3);
55 const TEST2: [u32; 3] = {
56 let mut arr = [1u32, 2, 3];
58 write_bytes(arr.as_mut_ptr(), 1, 2);
63 assert!(TEST2[0] == 16843009);
64 assert!(TEST2[1] == 16843009);
65 assert!(TEST2[2] == 3);
69 fn test_hints_in_const_contexts() {
70 use core::intrinsics::{likely, unlikely};
72 // In const contexts, they just return their argument.
74 assert!(true == likely(true));
75 assert!(false == likely(false));
76 assert!(true == unlikely(true));
77 assert!(false == unlikely(false));
78 assert!(42u32 == core::intrinsics::black_box(42u32));
79 assert!(42u32 == core::hint::black_box(42u32));
84 fn test_const_allocate_at_runtime() {
85 use core::intrinsics::const_allocate;
87 assert!(const_allocate(4, 4).is_null());
92 fn test_const_deallocate_at_runtime() {
93 use core::intrinsics::const_deallocate;
94 const X: &u32 = &42u32;
97 const_deallocate(X as *const _ as *mut u8, 4, 4); // nop
98 const_deallocate(x as *const _ as *mut u8, 4, 4); // nop
99 const_deallocate(core::ptr::null_mut(), 1, 1); // nop