Skip to content

Commit 5921685

Browse files
committed
Auto merge of rust-lang#86950 - tmiasko:personality, r=nagisa
Use existing declaration of rust_eh_personality If crate declares `rust_eh_personality`, re-use existing declaration as otherwise attempts to set function attributes that follow the declaration will fail (unless it happens to have exactly the same type signature as the one predefined in the compiler). Fixes rust-lang#70117. Fixes rust-lang#81469 (comment); probably.
2 parents 331da58 + f612ba1 commit 5921685

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,12 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
928928
pers_fn: &'ll Value,
929929
num_clauses: usize,
930930
) -> &'ll Value {
931+
// Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
932+
// LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
933+
// personality lives on the parent function anyway.
934+
self.set_personality_fn(pers_fn);
931935
unsafe {
932-
llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn, num_clauses as c_uint, UNNAMED)
936+
llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
933937
}
934938
}
935939

compiler/rustc_codegen_llvm/src/context.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,16 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
386386
} else {
387387
"rust_eh_personality"
388388
};
389-
let fty = self.type_variadic_func(&[], self.type_i32());
390-
self.declare_cfn(name, llvm::UnnamedAddr::Global, fty)
389+
if let Some(llfn) = self.get_declared_value(name) {
390+
llfn
391+
} else {
392+
let fty = self.type_variadic_func(&[], self.type_i32());
393+
let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
394+
attributes::apply_target_cpu_attr(self, llfn);
395+
llfn
396+
}
391397
}
392398
};
393-
attributes::apply_target_cpu_attr(self, llfn);
394399
self.eh_personality.set(Some(llfn));
395400
llfn
396401
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ extern "C" {
11651165
pub fn LLVMBuildLandingPad(
11661166
B: &Builder<'a>,
11671167
Ty: &'a Type,
1168-
PersFn: &'a Value,
1168+
PersFn: Option<&'a Value>,
11691169
NumClauses: c_uint,
11701170
Name: *const c_char,
11711171
) -> &'a Value;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Check that rust_eh_personality can have a different type signature than the
2+
// one hardcoded in the compiler. Regression test for #70117. Used to fail with:
3+
//
4+
// Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
5+
//
6+
// build-pass
7+
// compile-flags: --crate-type=lib -Ccodegen-units=1
8+
#![no_std]
9+
#![panic_runtime]
10+
#![feature(panic_runtime)]
11+
#![feature(rustc_attrs)]
12+
13+
pub struct DropMe;
14+
15+
impl Drop for DropMe {
16+
fn drop(&mut self) {}
17+
}
18+
19+
pub fn test(_: DropMe) {
20+
unreachable!();
21+
}
22+
23+
#[rustc_std_internal_symbol]
24+
pub unsafe extern "C" fn rust_eh_personality() {}

0 commit comments

Comments
 (0)