From d9ee41e823328702e28e4ee487b2160f68e2ab70 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 23 Oct 2024 09:53:47 +0100 Subject: [PATCH] miri: update ABI compat checks to accept Option-like types --- tests/pass/function_calls/abi_compat.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/pass/function_calls/abi_compat.rs b/tests/pass/function_calls/abi_compat.rs index b5feac8c67..cd48bd2acc 100644 --- a/tests/pass/function_calls/abi_compat.rs +++ b/tests/pass/function_calls/abi_compat.rs @@ -1,3 +1,5 @@ +#![feature(never_type)] + use std::rc::Rc; use std::{mem, num, ptr}; @@ -12,6 +14,18 @@ fn id(x: T) -> T { x } +#[derive(Copy, Clone)] +enum Either { + Left(T), + Right(U), +} +#[derive(Copy, Clone)] +enum Either2 { + Left(T), + #[allow(unused)] + Right(U, ()), +} + fn test_abi_compat(t: T, u: U) { fn id(x: T) -> T { x @@ -81,6 +95,8 @@ fn main() { test_abi_compat(main as fn(), id:: as fn(i32) -> i32); // - 1-ZST test_abi_compat((), [0u8; 0]); + + // Guaranteed null-pointer-layout optimizations: // - Guaranteed Option null-pointer-optimizations (RFC 3391). test_abi_compat(&0u32 as *const u32, Some(&0u32)); test_abi_compat(main as fn(), Some(main as fn())); @@ -89,6 +105,7 @@ fn main() { test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1u32).unwrap()))); // - Guaranteed Result does the same as Option (RFC 3391) test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(&0u32)); + test_abi_compat(&0u32 as *const u32, Result::<_, !>::Ok(&0u32)); test_abi_compat(main as fn(), Result::<_, ()>::Ok(main as fn())); test_abi_compat(0u32, Result::<_, ()>::Ok(num::NonZeroU32::new(1).unwrap())); test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(Wrapper(&0u32))); @@ -99,6 +116,13 @@ fn main() { test_abi_compat(0u32, Result::<(), _>::Err(num::NonZeroU32::new(1).unwrap())); test_abi_compat(&0u32 as *const u32, Result::<(), _>::Err(Wrapper(&0u32))); test_abi_compat(0u32, Result::<(), _>::Err(Wrapper(num::NonZeroU32::new(1).unwrap()))); + // - Guaranteed null-pointer-optimizations for custom option-like types + test_abi_compat(&0u32 as *const u32, Either::<_, ()>::Left(&0u32)); + test_abi_compat(&0u32 as *const u32, Either::<_, !>::Left(&0u32)); + test_abi_compat(&0u32 as *const u32, Either::<(), _>::Right(&0u32)); + test_abi_compat(&0u32 as *const u32, Either::::Right(&0u32)); + test_abi_compat(&0u32 as *const u32, Either2::<_, ()>::Left(&0u32)); + test_abi_compat(&0u32 as *const u32, Either2::<_, [u8; 0]>::Left(&0u32)); // These must work for *any* type, since we guarantee that `repr(transparent)` is ABI-compatible // with the wrapped field.