Skip to content

Visit const node in dead-code analysis #118713

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

Conversation

long-long-float
Copy link
Contributor

close #118424

I fixed to visit const node in dead-code analysis.

Before this PR, the body of const node is not visited in dead-code analysis.

type T = u32;
// std::mem::size_of::<T>() is not checked.
const _A: usize = std::mem::size_of::<T>();

fn main() {}

Therefore the type T is detected as a dead_code.

warning: type alias `T` is never used
 --> src/main.rs:1:6
  |
1 | type T = u32;
  |      ^
  |
  = note: `#[warn(dead_code)]` on by default

@rustbot
Copy link
Collaborator

rustbot commented Dec 7, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 7, 2023
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-16 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_f1c83a9b-0ed9-4c1c-b3b9-198653eeafe1
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=visit-const-node-in-dead-code-analysis
GITHUB_JOB=pr
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_f1c83a9b-0ed9-4c1c-b3b9-198653eeafe1
GITHUB_REF=refs/pull/118713/merge
GITHUB_REF_NAME=118713/merge
GITHUB_REF_PROTECTED=false
---
Built container sha256:9c3c93a371e5aed5c18185b24f130d95d5140dbd72a9b325e7b6b49e521a4faa
Looks like docker image is the same as before, not uploading
https://ci-caches.rust-lang.org/docker/7ebc15c01a233894034d277c8cce4e949f4e7791f66b4727c8fb6e058a0b8171d6152e1441d677cef0653843ceeee469c097b8699b2bb74249e674f6aa1a8813
sha256:9c3c93a371e5aed5c18185b24f130d95d5140dbd72a9b325e7b6b49e521a4faa
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-16]
##[group]Clock drift check
  local time: Thu Dec  7 15:50:47 UTC 2023
  network time: Thu, 07 Dec 2023 15:50:47 GMT
  network time: Thu, 07 Dec 2023 15:50:47 GMT
##[endgroup]
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-16', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-missing-tools', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-16/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: rust.codegen-units-std := 1
---
   Compiling linux-raw-sys v0.4.5
   Compiling bitflags v2.4.0
   Compiling fastrand v2.0.0
   Compiling smallvec v1.10.0
error: invalid `--check-cfg` argument: `values(freebsd10)` (expected `cfg(name, values("value1", "value2", ... "valueN"))`)
error: could not compile `libc` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...
Build completed unsuccessfully in 0:10:36
  local time: Thu Dec  7 16:01:57 UTC 2023

@compiler-errors
Copy link
Member

I think this is intentional. T is dead unless _A is used.

If anything, we need to check if the item name of the const starts with _ first, and only treat it as visited then.

@compiler-errors
Copy link
Member

Also, for the record, this also occurs in other items:

fn foo() {}
//~^ WARN function `foo` is never used

fn _a() {
    foo();
}

I believe this should be fixed more consistently, then we can T-lang nominate this for their feedback.

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 7, 2023
@long-long-float
Copy link
Contributor Author

In variables, there is no warning about x with or without _ of y.

fn main() {
    let x = 10;
    let _y = x;
}

// There are no warnings.

I may be mistaken, but I think the constants should be analyzed in the same way.

const X: i32 = 10;
const _Y: i32 = X;

fn main() {}

/*
warning: constant `X` is never used
  --> test.rs:20:7
   |
20 | const X: i32 = 10;
   |       ^
   |
   = note: `#[warn(dead_code)]` on by default
*/

@long-long-float
Copy link
Contributor Author

Furthremore, in the following code, the type alias T is used statically.

type T = u32;
const _A: () = assert!(std::mem::size_of::<T>() != std::mem::size_of::<T>());

fn main() {}

/*
warning: type alias `T` is never used
 --> test.rs:1:6
  |
1 | type T = u32;
  |      ^
  |
  = note: `#[warn(dead_code)]` on by default

error[E0080]: evaluation of constant value failed
 --> test.rs:2:16
  |
2 | const _A: () = assert!(std::mem::size_of::<T>() != std::mem::size_of::<T>());
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: std::mem::size_of::<T>() != std::mem::size_of::<T>()', test.rs:2:16
  |
  = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
*/

@JohnCSimon
Copy link
Member

@long-long-float
ping from triage - can you post your status on this PR? This PR has not received an update in a few months.

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

@long-long-float
Copy link
Contributor Author

@JohnCSimon
Thank you for reminder. I don't know what I should do next.
My opinion is that it would be more intuitive to match the behavior of the dead code analysis of variables and constants.

@JohnCSimon
Copy link
Member

@long-long-float

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Apr 14, 2024
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect dead_code lint with items only used in const.
5 participants