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

False positive dead_code for const used as const generic argument during impl marker trait #128617

Open
datdenkikniet opened this issue Aug 3, 2024 · 5 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@datdenkikniet
Copy link

datdenkikniet commented Aug 3, 2024

In the following snippet, CONST gets a dead_code warning. However, CONST is most definitely used and the trait is also pub, so this warning seems incorrect.

Removing CONST causes the crate to no longer compile, and removing the impl would be a breaking change.

Adding any function (i.e. fn method();) to the trait and implementing it removes the warning as well. However, for marker traits this is not an option. It also clearly demonstrates that CONST is considered to be used despite being unaffected by fn method().

Rust version: rustc 1.80.0 (0514789 2024-07-21)

This code compiles without warnings on 1.79.0 (rustc 1.79.0 (129f3b9 2024-06-10))

pub struct Value<const NUMBER: u8> {}

pub trait Trait {}

const CONST: u8 = 11;

impl Trait for Value<CONST> {}

Warning:

warning: constant `CONST` is never used
 --> lib.rs:5:7
  |
5 | const CONST: u8 = 11;
  |       ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Adding method() removes the warning:

pub struct Value<const NUMBER: u8> {}

pub trait Trait {
    fn method();
}

const CONST: u8 = 11;

impl Trait for Value<CONST> {
    fn method() {}
}
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 3, 2024
@datdenkikniet datdenkikniet changed the title Invalid detection of dead_code for const used as const generic argument during trait impl (regressed in 1.80.0) Invalid detection of dead_code for const used as const generic argument during impl of empty trait (regressed in 1.80.0) Aug 3, 2024
@datdenkikniet datdenkikniet changed the title Invalid detection of dead_code for const used as const generic argument during impl of empty trait (regressed in 1.80.0) Invalid detection of dead_code for const used as const generic argument during impl of empty trait Aug 3, 2024
@datdenkikniet datdenkikniet changed the title Invalid detection of dead_code for const used as const generic argument during impl of empty trait False positive dead_code for const used as const generic argument during impl of empty trait Aug 3, 2024
@datdenkikniet datdenkikniet changed the title False positive dead_code for const used as const generic argument during impl of empty trait False positive dead_code for const used as const generic argument during impl marker trait Aug 3, 2024
datdenkikniet added a commit to stm32-rs/stm32-eth that referenced this issue Aug 3, 2024
@datdenkikniet
Copy link
Author

datdenkikniet commented Aug 3, 2024

Potentially fixed by #128404. Have no opportunity to test RN, but saw this come by and it seemed potentially relevant.

@compiler-errors
Copy link
Member

@datdenkikniet: This is indeed fixed by that PR.

@compiler-errors
Copy link
Member

cc @mu001999: This is another regression that should be fixed first if the dead code changes are re-landed.

datdenkikniet added a commit to stm32-rs/stm32-eth that referenced this issue Aug 3, 2024
@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. L-dead_code Lint: dead_code labels Aug 3, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 3, 2024
@datdenkikniet
Copy link
Author

This is fixed in the new point release, 1.80.1. Should we close this issue, or keep it open for tracking?

@jieyouxu
Copy link
Member

jieyouxu commented Sep 5, 2024

This is probably worth keeping open for tracking purposes, and probably should be closed by the re-land PR(s) that add this case as a regression(?) test.

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. L-dead_code Lint: 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