Skip to content

Spurious dead code warning when item is only used in impls #18290

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

Closed
arielb1 opened this issue Oct 24, 2014 · 5 comments · Fixed by #63317
Closed

Spurious dead code warning when item is only used in impls #18290

arielb1 opened this issue Oct 24, 2014 · 5 comments · Fixed by #63317
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Oct 24, 2014

It seems that if a const is only used in impls, but not in actual code, then rustc emits a "constant item is never used" warning, as in the following code:

const TLC: uint = 4;
trait Tr { fn doit(&self); }
impl Tr for [uint, ..TLC] {
    fn doit(&self) { println!("called 4"); }
}

fn main() {
    let s = [0,1,2,3u];
    s.doit(); // which .doit is called depends on architecture
}

Which prints (when compiled and then run):

<anon>:1:1: 1:21 warning: constant item is never used: `TLC`, #[warn(dead_code)] on by default
<anon>:1 const TLC: uint = 4;
         ^~~~~~~~~~~~~~~~~~~~
called 4
@arielb1 arielb1 changed the title Spurious "constant item is never used" when a constant is used only in impls Spurious dead code warning when a constant is used only in impls Oct 24, 2014
@sfackler sfackler added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Oct 24, 2014
@ghost ghost self-assigned this Dec 12, 2014
@steveklabnik
Copy link
Member

steveklabnik commented Oct 27, 2015

Updated code:

const TLC: usize = 4;
trait Tr { fn doit(&self); }
impl Tr for [usize; TLC] {
    fn doit(&self) { println!("called 4"); }
}

fn main() {
    let s = [0,1,2,3];
    s.doit(); // which .doit is called depends on architecture
}

Still gives the warning.

@wthrowe
Copy link
Contributor

wthrowe commented Oct 28, 2015

Not particularly related to constants: impls just aren't counted as uses for types they refer to. Y and Z are both marked dead here:

struct X;
struct Y;
struct Z;

trait Foo<T> {
    type Ty;
    fn foo() -> Self::Ty;
}

impl Foo<Y> for X {
    type Ty = Z;
    fn foo() -> Self::Ty { unimplemented!() }
}

fn main() {
    X::foo();
}

@binarycrusader
Copy link
Contributor

binarycrusader commented Jan 4, 2017

I hit another case with this just in the last few days using nightly via rustup; impls don't appear to be involved?

const ACT_STRINGS: &'static [&'static str] = &["set name=foo value=foo"];

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse() {
        for astr in ACT_STRINGS.iter() {
            println!("{}", astr);
        }
    }   
}
$ cargo --version
cargo 0.17.0-nightly (740f9c0 2016-12-29)
$ rustc --version
rustc 1.16.0-nightly (4ecc85beb 2016-12-28)
$ cargo build -v
   Compiling parser v0.1.0 (file:///builds/srwalker/rsdev/parser)
     Running `rustc --crate-name parser lib.rs --crate-type lib -g -C metadata=be6ddb31ceb06482 -C extra-filename=-be6ddb31ceb06482 --out-dir /builds/srwalker/rsdev/parser/target/debug/deps --emit=dep-info,link -L dependency=/builds/srwalker/rsdev/parser/target/debug/deps`
warning: constant item is never used: `ACT_STRINGS`, #[warn(dead_code)] on by default
 --> lib.rs:1:1
  |
1 | const ACT_STRINGS: &'static [&'static str] = &["set name=foo value=foo"];
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    Finished debug [unoptimized + debuginfo] target(s) in 0.6 secs

Changing the use super::*; into use super::ACT_STRINGS; makes no difference.

I suspect my case is issue #33166 ?

@Mark-Simulacrum
Copy link
Member

Triage:

  • Original error reproduces.
  • This example reproduces.
  • This example seems unrelated, more due to test code being #[cfg(test)] (so really unused).

@Mark-Simulacrum Mark-Simulacrum changed the title Spurious dead code warning when a constant is used only in impls Spurious dead code warning when item is only used in impls Jun 22, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@Mark-Simulacrum
Copy link
Member

Self needs to be examined as well: #30029.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants