Skip to content

Commit 74b967c

Browse files
committed
miri: update ABI compat checks to accept Option-like types
1 parent bca5fde commit 74b967c

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

compiler/rustc_const_eval/src/interpret/call.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::borrow::Cow;
55

66
use either::{Left, Right};
77
use rustc_middle::ty::layout::{FnAbiOf, IntegerExt, LayoutOf, TyAndLayout};
8-
use rustc_middle::ty::{self, AdtDef, Instance, Ty};
8+
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
99
use rustc_middle::{bug, mir, span_bug};
1010
use rustc_span::sym;
1111
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
12-
use rustc_target::abi::{self, FieldIdx, Integer};
12+
use rustc_target::abi::{self, FieldIdx, Integer, VariantIdx};
1313
use rustc_target::spec::abi::Abi;
1414
use tracing::{info, instrument, trace};
1515

@@ -93,29 +93,46 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
9393

9494
/// Unwrap types that are guaranteed a null-pointer-optimization
9595
fn unfold_npo(&self, layout: TyAndLayout<'tcx>) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
96-
// Check if this is `Option` wrapping some type or if this is `Result` wrapping a 1-ZST and
97-
// another type.
96+
// Check if this is an option-like type wrapping some type.
9897
let ty::Adt(def, args) = layout.ty.kind() else {
9998
// Not an ADT, so definitely no NPO.
10099
return interp_ok(layout);
101100
};
102-
let inner = if self.tcx.is_diagnostic_item(sym::Option, def.did()) {
103-
// The wrapped type is the only arg.
104-
self.layout_of(args[0].as_type().unwrap())?
105-
} else if self.tcx.is_diagnostic_item(sym::Result, def.did()) {
106-
// We want to extract which (if any) of the args is not a 1-ZST.
107-
let lhs = self.layout_of(args[0].as_type().unwrap())?;
108-
let rhs = self.layout_of(args[1].as_type().unwrap())?;
109-
if lhs.is_1zst() {
110-
rhs
111-
} else if rhs.is_1zst() {
112-
lhs
113-
} else {
114-
return interp_ok(layout); // no NPO
101+
if def.variants().len() != 2 {
102+
// Not a 2-variant enum, so no NPO.
103+
return interp_ok(layout);
104+
}
105+
assert!(def.is_enum());
106+
107+
let all_fields_1zst = |variant: &VariantDef| -> InterpResult<'tcx, _> {
108+
for field in &variant.fields {
109+
let ty = field.ty(*self.tcx, args);
110+
let layout = self.layout_of(ty)?;
111+
if !layout.is_1zst() {
112+
return interp_ok(false);
113+
}
115114
}
115+
interp_ok(true)
116+
};
117+
118+
// If one variant consists entirely of 1-ZST, then the other variant
119+
// is the only "relevant" one for this check.
120+
let var0 = VariantIdx::from_u32(0);
121+
let var1 = VariantIdx::from_u32(1);
122+
let relevant_variant = if all_fields_1zst(def.variant(var0))? {
123+
def.variant(var1)
124+
} else if all_fields_1zst(def.variant(var1))? {
125+
def.variant(var0)
116126
} else {
117-
return interp_ok(layout); // no NPO
127+
// No varant is all-1-ZST, so no NPO.
128+
return interp_ok(layout);
118129
};
130+
// The "relevant" variant must have exactly one field, and its type is the "inner" type.
131+
if relevant_variant.fields.len() != 1 {
132+
return interp_ok(layout);
133+
}
134+
let inner = relevant_variant.fields[FieldIdx::from_u32(0)].ty(*self.tcx, args);
135+
let inner = self.layout_of(inner)?;
119136

120137
// Check if the inner type is one of the NPO-guaranteed ones.
121138
// For that we first unpeel transparent *structs* (but not unions).

src/tools/miri/tests/pass/function_calls/abi_compat.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(never_type)]
2+
13
use std::rc::Rc;
24
use std::{mem, num, ptr};
35

@@ -12,6 +14,18 @@ fn id<T>(x: T) -> T {
1214
x
1315
}
1416

17+
#[derive(Copy, Clone)]
18+
enum Either<T, U> {
19+
Left(T),
20+
Right(U),
21+
}
22+
#[derive(Copy, Clone)]
23+
enum Either2<T, U> {
24+
Left(T),
25+
#[allow(unused)]
26+
Right(U, ()),
27+
}
28+
1529
fn test_abi_compat<T: Clone, U: Clone>(t: T, u: U) {
1630
fn id<T>(x: T) -> T {
1731
x
@@ -89,6 +103,7 @@ fn main() {
89103
test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1u32).unwrap())));
90104
// - Guaranteed Result<X, ZST1> does the same as Option<X> (RFC 3391)
91105
test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(&0u32));
106+
test_abi_compat(&0u32 as *const u32, Result::<_, !>::Ok(&0u32));
92107
test_abi_compat(main as fn(), Result::<_, ()>::Ok(main as fn()));
93108
test_abi_compat(0u32, Result::<_, ()>::Ok(num::NonZeroU32::new(1).unwrap()));
94109
test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(Wrapper(&0u32)));
@@ -99,6 +114,13 @@ fn main() {
99114
test_abi_compat(0u32, Result::<(), _>::Err(num::NonZeroU32::new(1).unwrap()));
100115
test_abi_compat(&0u32 as *const u32, Result::<(), _>::Err(Wrapper(&0u32)));
101116
test_abi_compat(0u32, Result::<(), _>::Err(Wrapper(num::NonZeroU32::new(1).unwrap())));
117+
// - Guaranteed null-pointers-opts for custom option-like types
118+
test_abi_compat(&0u32 as *const u32, Either::<_, ()>::Left(&0u32));
119+
test_abi_compat(&0u32 as *const u32, Either::<_, !>::Left(&0u32));
120+
test_abi_compat(&0u32 as *const u32, Either::<(), _>::Right(&0u32));
121+
test_abi_compat(&0u32 as *const u32, Either::<!, _>::Right(&0u32));
122+
test_abi_compat(&0u32 as *const u32, Either2::<_, ()>::Left(&0u32));
123+
test_abi_compat(&0u32 as *const u32, Either2::<_, [u8; 0]>::Left(&0u32));
102124

103125
// These must work for *any* type, since we guarantee that `repr(transparent)` is ABI-compatible
104126
// with the wrapped field.

0 commit comments

Comments
 (0)