Description
The following simple program fails to compile with Rust because the Drop
trait introduces an incompatibility that causes a linker error. If you comment out the impl
block, then it will work just fine. Additionally, if you compile with optimizations turned on, it will work just fine. But if you compile the following without optimizations, it will fail.
t.rs:
#![feature(lang_items, start)]
#![no_std]
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
struct Foo {}
impl core::ops::Drop for Foo {
fn drop(&mut self) {
}
}
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let _foo = Foo {};
return 0;
}
The error: (part of the paths have been redacted; <RUSTUP>
is the installation directory for rustup
)
$ rustc t.rs -l c
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-m64" "-L" "<RUSTUP>/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "t.t0.rcgu.o" "t.t1.rcgu.o" "-o" "t" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "<RUSTUP>/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-l" "c" "<RUSTUP>/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-6ee4b7a50e610a70.rlib"
= note: Undefined symbols for architecture x86_64:
"_rust_eh_personality", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in t.t0.rcgu.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: aborting due to previous error
Rust version:
$ rustc --version
rustc 1.25.0-nightly (3f92e8d89 2018-01-14)
Note: Building with optimizations on works, presumably because the optimizer is able to optimize away the call to core::ptr::drop_in_place
(which is tagged with .cfi_personality 155, _rust_eh_personality
in the generated debug assembly, which I presume is the source of the problem):
$ rustc t.rs -O -l c
$ ./t
My system: macOS 10.13.2. rustc
is installed from rustup
.