Skip to content

Commit 6b5a58c

Browse files
authored
Rollup merge of #132057 - RalfJung:miri-abi-compat, r=wesleywiser
miri: update ABI compat checks to accept Option-like types This implements the t-lang decision described [here](#130628 (comment)). Fixes rust-lang/miri#3983
2 parents 9200cbc + b2fe71a commit 6b5a58c

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

compiler/rustc_const_eval/src/interpret/call.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::assert_matches::assert_matches;
44
use std::borrow::Cow;
55

66
use either::{Left, Right};
7-
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer};
7+
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
88
use rustc_middle::ty::layout::{FnAbiOf, IntegerExt, LayoutOf, TyAndLayout};
9-
use rustc_middle::ty::{self, AdtDef, Instance, Ty};
9+
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
1010
use rustc_middle::{bug, mir, span_bug};
1111
use rustc_span::sym;
1212
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
@@ -92,29 +92,46 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
9292

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

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

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

+24
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
@@ -81,6 +95,8 @@ fn main() {
8195
test_abi_compat(main as fn(), id::<i32> as fn(i32) -> i32);
8296
// - 1-ZST
8397
test_abi_compat((), [0u8; 0]);
98+
99+
// Guaranteed null-pointer-layout optimizations:
84100
// - Guaranteed Option<X> null-pointer-optimizations (RFC 3391).
85101
test_abi_compat(&0u32 as *const u32, Some(&0u32));
86102
test_abi_compat(main as fn(), Some(main as fn()));
@@ -89,6 +105,7 @@ fn main() {
89105
test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1u32).unwrap())));
90106
// - Guaranteed Result<X, ZST1> does the same as Option<X> (RFC 3391)
91107
test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(&0u32));
108+
test_abi_compat(&0u32 as *const u32, Result::<_, !>::Ok(&0u32));
92109
test_abi_compat(main as fn(), Result::<_, ()>::Ok(main as fn()));
93110
test_abi_compat(0u32, Result::<_, ()>::Ok(num::NonZeroU32::new(1).unwrap()));
94111
test_abi_compat(&0u32 as *const u32, Result::<_, ()>::Ok(Wrapper(&0u32)));
@@ -99,6 +116,13 @@ fn main() {
99116
test_abi_compat(0u32, Result::<(), _>::Err(num::NonZeroU32::new(1).unwrap()));
100117
test_abi_compat(&0u32 as *const u32, Result::<(), _>::Err(Wrapper(&0u32)));
101118
test_abi_compat(0u32, Result::<(), _>::Err(Wrapper(num::NonZeroU32::new(1).unwrap())));
119+
// - Guaranteed null-pointer-optimizations for custom option-like types
120+
test_abi_compat(&0u32 as *const u32, Either::<_, ()>::Left(&0u32));
121+
test_abi_compat(&0u32 as *const u32, Either::<_, !>::Left(&0u32));
122+
test_abi_compat(&0u32 as *const u32, Either::<(), _>::Right(&0u32));
123+
test_abi_compat(&0u32 as *const u32, Either::<!, _>::Right(&0u32));
124+
test_abi_compat(&0u32 as *const u32, Either2::<_, ()>::Left(&0u32));
125+
test_abi_compat(&0u32 as *const u32, Either2::<_, [u8; 0]>::Left(&0u32));
102126

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

0 commit comments

Comments
 (0)