Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link-dead-code limitations (bad for code coverage) #39293

Open
malbarbo opened this issue Jan 25, 2017 · 4 comments
Open

link-dead-code limitations (bad for code coverage) #39293

malbarbo opened this issue Jan 25, 2017 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. link-dead-code Linkage: using -Clink-dead-code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@malbarbo
Copy link
Contributor

Consider a cargo project (dead) with this src/lib.rs file:

pub fn fun0() {}

pub fn fun1<T>() {}

pub trait Trait {
    fn fun2(&self);
    fn fun3(&self) {}
}

impl Trait for () {
    fn fun2(&self) {}
}

Running

cargo clean && RUSTFLAGS='-C link-dead-code' cargo test && cargo sym -C target/debug/dead* | grep dead::

outputs

0000000000015e60 dead::fun0::he72896c4e971b738
0000000000015e70 <() as dead::Trait>::fun2::h21fa8b5ef4270ce1
0000000000015e90 dead::__test::main::h824aaf01f68ecd0a

We can observe that that only fun0 and <() as Trait>::fun2 is included (also checked with nm and running kcov). Not including fun1 and Trait::fun2 makes code coverage inflates (using kcov).

I can see that fun1 and Trait::fun2 is not included because they need to be monomorphised. One partial solution would be to monomorphise the default methods implementations for every type that implements the trait, but I think this could blow the binary size and compilation time.

Would it be possible to add this symbols without monomorphisation? Or maybe creating a monomorphised version that just panics? (Could all generic paramaters be set to !?)

@clarfonthey
Copy link
Contributor

One improvement for this that I can see would be monomorphising all of the methods and trait impls for a type when it's instantiated, rather than just the necessary methods. This would solve most of these problems, I think.

@JelteF
Copy link
Contributor

JelteF commented Apr 23, 2017

@clarcharr I think that would be a great intermediary solution, because it would catch all the cases I currently have. Even though only doing this for methods and trait impls of instansiated types would not fix this fully, because generic functions and generic types that are never instansiated would still have this issue (but I think those are quite rare).

@malbarbo I'm not sure about binary size, but because this would normally only be done for test builds I don't think it should be an issue for a first version. I have one possible optimization to avoid excessive blowing up of the binary though. There could be kept track of which methods are not monomorphised at all and only monomorphise them for one of the types that they could be monomorphised for.

@Mark-Simulacrum Mark-Simulacrum added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 22, 2017
@Mark-Simulacrum
Copy link
Member

Looks like the problem here is (in general) that we don't use the -Clink-dead-code anywhere except for the direct arguments to the linker. We should probably also utilize it in the collection for trans so that we generate the LLVM IR for dead code. One potential problem is that, IIRC, MIR has a dead code elimination pass which would also need to read this, and might cause problems later down the line.

OTOH, dead code doesn't really need to be tested -- so perhaps this isn't really a problem.

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 26, 2017
@malbarbo
Copy link
Contributor Author

OTOH, dead code doesn't really need to be tested -- so perhaps this isn't really a problem.

You maybe be writing a library and forget to test a generic function. It is dead code in the library but a client may use the (untested) function.

ramosbugs added a commit to ramosbugs/openidconnect-rs that referenced this issue Aug 15, 2018
Generic functions without monomorphization don't end up in the generated
binary, so they aren't included in the coverage metrics. See
rust-lang/rust#39293.
@workingjubilee workingjubilee added the link-dead-code Linkage: using -Clink-dead-code label Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. link-dead-code Linkage: using -Clink-dead-code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants