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

dead_code false positive for const used in definition of tuple struct #56281

Closed
crumblingstatue opened this issue Nov 27, 2018 · 1 comment · Fixed by #56953
Closed

dead_code false positive for const used in definition of tuple struct #56281

crumblingstatue opened this issue Nov 27, 2018 · 1 comment · Fixed by #56953
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@crumblingstatue
Copy link
Contributor

const LEN: usize = 4;

#[derive(Debug)]
struct Wrapper([u8; LEN]);

fn main() {
    println!("{:?}", Wrapper([0, 1, 2, 3]));
}
warning: constant item is never used: `LEN`
 --> src/main.rs:1:1
  |
1 | const LEN: usize = 4;
  | ^^^^^^^^^^^^^^^^^^^^^

If the struct is a record struct with a named field, the false positive doesn't trigger.

@Centril Centril added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Nov 27, 2018
@oli-obk oli-obk self-assigned this Nov 29, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Dec 18, 2018

I have not been able to get anywhere with this issue.

I did some experiments in https://github.com/rust-lang/rust/blob/master/src/librustc/middle/dead.rs and I believe the issue is that using a tuple struct's constructor will only mark the constructor as used (which has a different DefId and NodeId than the tuple struct item). This guess has a further datapoint than my reading: Adding let x: Wrapper; will silence the dead code warning about LEN.

The reason that we are not getting a dead code warning about Wrapper is that checking the item for "having been used" will also accept just the constructor having been used:

let ctor_id = get_struct_ctor_id(item);
should_warn && !self.symbol_is_live(item.id, ctor_id)

I have not been able to figure out a way to go from a constructor back to its item, there only seems to be code for the other way around.

We might be able to hack it by getting the function signature of the item and checking the return type, but in order to do that, we'd need to know for sure that it's a tuple struct constructor.

Alternatively we can just build up a map from constructors to items by filling it in whenever we encounter a tuple struct item.

ok enough rubber-ducking. I'm gonna try the sentence above now.

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Dec 21, 2018
Mark tuple structs as live if their constructors are used

fixes rust-lang#56281
Centril added a commit to Centril/rust that referenced this issue Dec 22, 2018
Mark tuple structs as live if their constructors are used

fixes rust-lang#56281
Centril added a commit to Centril/rust that referenced this issue Dec 23, 2018
Mark tuple structs as live if their constructors are used

fixes rust-lang#56281
Centril added a commit to Centril/rust that referenced this issue Dec 23, 2018
Mark tuple structs as live if their constructors are used

fixes rust-lang#56281
Centril added a commit to Centril/rust that referenced this issue Dec 23, 2018
Mark tuple structs as live if their constructors are used

fixes rust-lang#56281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint 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.

3 participants