Skip to content

Commit bdc321b

Browse files
committed
Fix codegen of uninhabited PassMode::Indirect return types.
Add codegen test for uninhabited PassMode::Indirect return types.
1 parent 6e9eadb commit bdc321b

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use rustc_abi::{BackendRepr, ExternAbi, HasDataLayout, Reg, WrappingRange};
44
use rustc_ast as ast;
55
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
66
use rustc_hir::lang_items::LangItem;
7-
use rustc_middle::mir::{
8-
self, AssertKind, BasicBlock, InlineAsmMacro, SwitchTargets, UnwindTerminateReason,
9-
};
7+
use rustc_middle::mir::{self, AssertKind, InlineAsmMacro, SwitchTargets, UnwindTerminateReason};
108
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, ValidityRequirement};
119
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
1210
use rustc_middle::ty::{self, Instance, Ty};
@@ -919,7 +917,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
919917
&fn_abi.ret,
920918
&mut llargs,
921919
Some(intrinsic),
922-
target,
923920
);
924921
let dest = match ret_dest {
925922
_ if fn_abi.ret.is_indirect() => llargs[0],
@@ -975,19 +972,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
975972
};
976973

977974
let mut llargs = Vec::with_capacity(arg_count);
978-
let destination = target.as_ref().map(|&target| {
979-
(
980-
self.make_return_dest(
981-
bx,
982-
destination,
983-
&fn_abi.ret,
984-
&mut llargs,
985-
None,
986-
Some(target),
987-
),
988-
target,
989-
)
990-
});
975+
976+
// We still need to call `make_return_dest` even if there's no `target`, since
977+
// `fn_abi.ret` could be `PassMode::Indirect`, even if it is uninhabited,
978+
// and `make_return_dest` adds the return-place indirect pointer to `llargs`.
979+
let return_dest = self.make_return_dest(bx, destination, &fn_abi.ret, &mut llargs, None);
980+
let destination = target.map(|target| (return_dest, target));
991981

992982
// Split the rust-call tupled arguments off.
993983
let (first_args, untuple) = if abi == ExternAbi::RustCall && !args.is_empty() {
@@ -1790,11 +1780,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
17901780
fn_ret: &ArgAbi<'tcx, Ty<'tcx>>,
17911781
llargs: &mut Vec<Bx::Value>,
17921782
intrinsic: Option<ty::IntrinsicDef>,
1793-
target: Option<BasicBlock>,
17941783
) -> ReturnDest<'tcx, Bx::Value> {
1795-
if target.is_none() {
1796-
return ReturnDest::Nothing;
1797-
}
17981784
// If the return is ignored, we can just return a do-nothing `ReturnDest`.
17991785
if fn_ret.is_ignore() {
18001786
return ReturnDest::Nothing;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// See https://github.com/rust-lang/rust/issues/135802
2+
3+
#![crate_type = "lib"]
4+
5+
enum Void {}
6+
7+
// Should be ABI-compatible with T, but wasn't prior to the PR adding this test.
8+
#[repr(transparent)]
9+
struct NoReturn<T>(T, Void);
10+
11+
// Returned by invisible reference (in most ABIs)
12+
#[allow(dead_code)]
13+
struct Large(u64, u64, u64);
14+
15+
extern "Rust" {
16+
fn opaque() -> NoReturn<Large>;
17+
fn opaque_with_arg(rsi: u32) -> NoReturn<Large>;
18+
}
19+
20+
// CHECK-LABEL: @test_uninhabited_ret_by_ref
21+
#[no_mangle]
22+
pub fn test_uninhabited_ret_by_ref() {
23+
// CHECK: %_1 = alloca [24 x i8], align {{8|4}}
24+
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %_1)
25+
// CHECK-NEXT: call void @opaque(ptr noalias nocapture noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_1) #2
26+
// CHECK-NEXT: unreachable
27+
unsafe {
28+
opaque();
29+
}
30+
}
31+
32+
// CHECK-LABEL: @test_uninhabited_ret_by_ref_with_arg
33+
#[no_mangle]
34+
pub fn test_uninhabited_ret_by_ref_with_arg(rsi: u32) {
35+
// CHECK: %_2 = alloca [24 x i8], align {{8|4}}
36+
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %_2)
37+
// CHECK-NEXT: call void @opaque_with_arg(ptr noalias nocapture noundef nonnull sret([24 x i8]) align {{8|4}} dereferenceable(24) %_2, i32 noundef %rsi) #2
38+
// CHECK-NEXT: unreachable
39+
unsafe {
40+
opaque_with_arg(rsi);
41+
}
42+
}

0 commit comments

Comments
 (0)