Skip to content

rustc compiles function that is never used #70333

Open
@WaffleLapkin

Description

@WaffleLapkin

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

  1. create lib and bin crates:
    cargo new --bin neverbin && cargo new --lib neverlib
  2. fill ./neverlib/src/lib.rs with:
    trait NeverUsed { 
        fn never_used(); 
    } 
    
    impl NeverUsed for () { 
        fn never_used() { 
            call_panic() 
        } 
    } 
    
    fn call_panic() { panic!(\"ooops\") }
  3. add dependency to bin crate:
     # Cargo.toml
    [dependencies]
    neverlib = { path = "../neverlib" }
  4. 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}}
  5. Note never_used and call_panic functions, all the panic! stuff
  6. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions