Open WaffleLapkin opened 4 years ago
cc @rust-lang/wg-mir-opt perhaps?
Here it is on playground - you can press the ASM button etc.
I forget why the fact that these are actually unused, doesn't factor into whether this should compile or not, I wonder if this, once upon a time, fixed impl Trait
.
The problem with traits is it's almost impossible to figure out where they might be used from. You can write code that would have the effect of a downstream crate needing <() as NeverUsed>::never_used
, and the MIR for that isn't serialized, so it has to exist in the original crate.
What's weird is adding #[inline]
to the impl
method doesn't fix this, even though it should IMO.
Sadly I don't know these aspects of monomorphization, cc @michaelwoerister @Zoxc
Ah, for some reason both functions need #[inline]
.
@WaffleLapkin Out of curiosity, is that a suitable workaround? We should keep this issue open, probably, but a fix might be hard or effectively impossible, so maybe you could use the workaround.
@eddyb well, I was trying to copy std's approach with #[inline(never)]
panic-fns to reduce code bloat. But since it doesn't work well, I think I'll just move panic!()
to the trait's fn itself. That is not so critical to me.
@WaffleLapkin libstd
's approach assumes you will need to panic, it's only a good idea to do that in other situations if you're using LTO (if it even works to remove the panic).
There's even code like this where it switches between #[inline(never)]
and #[inline]
: https://github.com/rust-lang/rust/blob/55299b2ba99432d10f925cd28ff52fe397577371/src/libstd/panicking.rs#L362-L366
It might have to do with the reachability pass being rather conservative around methods in trait impls? https://github.com/rust-lang/rust/blob/1add455ec6f81045e7651c6225902823f5d4fbfa/src/librustc_mir/monomorphize/collector.rs#L1008
Probably because the logic to find unused trait functions are not in place. Also see https://github.com/rust-lang/rust/issues/50927.
rustc
seems to compile never used functions in traits: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
lib
andbin
crates:cargo new --bin neverbin && cargo new --lib neverlib
fill
./neverlib/src/lib.rs
with:cargo asm
for bin crate, you'll see something like that:never_used
andcall_panic
functions, all thepanic!
stuffMeta
rustc --version --verbose
: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.