Open
Description
rustc
seems to compile never used functions in traits:
// Note: both trait and fn are private and never used
trait NeverUsed {
fn never_used();
}
impl NeverUsed for () {
fn never_used() {
size_expectation_failed()
}
}
fn size_expectation_failed() -> ! {
panic!("size of `slice` must not be less than size of `Self` to ref cast to success")
}
Generates a lot of "panic" asm (godbolt).
I expected not to see unused functions in asm, but they are there.
This also happens when using lib with a similar (never used) trait.
Steps to reproduce
- create
lib
andbin
crates:
cargo new --bin neverbin && cargo new --lib neverlib
- fill
./neverlib/src/lib.rs
with:trait NeverUsed { fn never_used(); } impl NeverUsed for () { fn never_used() { call_panic() } } fn call_panic() { panic!(\"ooops\") }
- add dependency to bin crate:
# Cargo.toml [dependencies] neverlib = { path = "../neverlib" }
- install & run
cargo asm
for bin crate, you'll see something like that:$ cargo asm <() as neverlib::NeverUsed>::never_used <T as core::any::Any>::type_id <std::panicking::begin_panic::PanicPayload<A> as core::panic::BoxMeUp>::get <std::panicking::begin_panic::PanicPayload<A> as core::panic::BoxMeUp>::take_box core::ops::function::FnOnce::call_once{{vtable.shim}} core::ptr::drop_in_place neverbin::main neverlib::call_panic std::panicking::begin_panic std::rt::lang_start std::rt::lang_start::{{closure}}
- Note
never_used
andcall_panic
functions, all thepanic!
stuff - this behaviour also holds with lto
Meta
rustc --version --verbose
:
rustc 1.43.0-nightly (564758c4c 2020-03-08)
binary: rustc
commit-hash: 564758c4c329e89722454dd2fbb35f1ac0b8b47c
commit-date: 2020-03-08
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0
P.S.
I may misunderstand something, and maybe I've forgot to turn on some optimizations, so correct me if I'm wrong. But this behaviour seems strange.