rust icon indicating copy to clipboard operation
rust copied to clipboard

Improve dead code analysis for structs and traits defined locally

Open mu001999 opened this issue 1 year ago • 15 comments

This PR does some refactor and improvement on the dead code analysis, and doesn't lint pub structs.

  1. refactors the two-phase check for impls and impl items of trait
    1. checks them all later because we must use the trait/trait item and the adt defined locally firstly
    2. makes the logic cleaner and doesn't require special checks about whether it's public or not
  2. mark the adt live if it appears in pattern, like generic argument, it also implies the use of the adt
    1. based on 1 and 2, we can detect unused private adts impl Default, without adding special logics for Default
    2. so that we can remove rustc_trivial_field_reads on Default, and the logic in should_ignore_item
  3. extends rules to impls for types which refer to adts, like &Foo/[Foo] things
  4. lints unused assoc consts and assoc tys, like assoc fns

Fixes #120770 Fixes #126729 Fixes #127911 Fixes #128839

cc @pnkfelix r? @compiler-errors

mu001999 avatar Aug 04 '24 11:08 mu001999

Some changes occurred in tests/ui/sanitizer

cc @rust-lang/project-exploit-mitigations, @rcvalle

rustbot avatar Aug 04 '24 11:08 rustbot

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

Click to see the possible cause of the failure (guessed by this bot)

rust-log-analyzer avatar Aug 04 '24 11:08 rust-log-analyzer

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

Click to see the possible cause of the failure (guessed by this bot)
   Compiling rustc_borrowck v0.0.0 (/checkout/compiler/rustc_borrowck)
   Compiling rustc_codegen_llvm v0.0.0 (/checkout/compiler/rustc_codegen_llvm)
   Compiling rustc_privacy v0.0.0 (/checkout/compiler/rustc_privacy)
   Compiling rustc_passes v0.0.0 (/checkout/compiler/rustc_passes)
error: struct `MustUseAsync` is never constructed
    |
376 | pub struct MustUseAsync {
    |            ^^^^^^^^^^^^
    |

rust-log-analyzer avatar Aug 04 '24 13:08 rust-log-analyzer

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

Click to see the possible cause of the failure (guessed by this bot)
------
 > importing cache manifest from ghcr.io/rust-lang/rust-ci-cache:3aacb9c90579defe09351ac5e8ee504359f8054da6326ff19038f1b7c90e3cb2aafe33685c6d9b76ee8d2ccbd187ca80c46ab5380485abdd8c0ce7d69cd8d8fd:
------
##[endgroup]
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-17]
---
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-17', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--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', '--set', 'rust.lld=false', '--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-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-17/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---
  Downloaded boml v0.3.1
   Compiling boml v0.3.1
   Compiling y v0.1.0 (/checkout/compiler/rustc_codegen_gcc/build_system)
    Finished `release` profile [optimized] target(s) in 3.74s
     Running `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-codegen/x86_64-unknown-linux-gnu/release/y test --use-system-gcc --use-backend gcc --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/cg_gcc --release --mini-tests --std-tests`
Using system GCC
[BUILD] example
[AOT] mini_core_hello_world
/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/cg_gcc/mini_core_hello_world
abc

rust-log-analyzer avatar Aug 04 '24 15:08 rust-log-analyzer

Please separate this into separate commits each implementing an individual tweak to the dead code analysis, with the tests adjusted at each commit so I can see the fallout from each change specifically. It's very difficult to map the changes to the UI tests to each code change without that. --- I want to think very hard about each of these changes to determine if there are any false positives that are caused by each change, and that is harder to do with a single commit.

Also, if you want, please open a separate PR that removes the dead code from the compiler/standard library that is now detected after these changes. That can be landed separately.

compiler-errors avatar Aug 04 '24 16:08 compiler-errors

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 2.845 Building wheels for collected packages: reuse
#16 2.847   Building wheel for reuse (pyproject.toml): started
#16 3.089   Building wheel for reuse (pyproject.toml): finished with status 'done'
#16 3.091   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132715 sha256=dfa09868353292d98f811d3efdb0d54d07389e808efc71d68e3b93c514bf8bec
#16 3.091   Stored in directory: /tmp/pip-ephem-wheel-cache-_t8d3ibb/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#16 3.094 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#16 3.496 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#16 3.497 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#16 DONE 3.6s

rust-log-analyzer avatar Aug 04 '24 16:08 rust-log-analyzer

@compiler-errors I have separated this to separate commits.

mu001999 avatar Aug 06 '24 00:08 mu001999

:umbrella: The latest upstream changes (presumably #128761) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Aug 07 '24 04:08 bors

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#15 2.709 Building wheels for collected packages: reuse
#15 2.710   Building wheel for reuse (pyproject.toml): started
#15 2.959   Building wheel for reuse (pyproject.toml): finished with status 'done'
#15 2.960   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132715 sha256=dfa09868353292d98f811d3efdb0d54d07389e808efc71d68e3b93c514bf8bec
#15 2.960   Stored in directory: /tmp/pip-ephem-wheel-cache-nthjnaws/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#15 2.963 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#15 3.362 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#15 3.363 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 DONE 3.5s

rust-log-analyzer avatar Aug 07 '24 15:08 rust-log-analyzer

:umbrella: The latest upstream changes (presumably #129046) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Aug 13 '24 14:08 bors

Could you submit 1317d543306ec010d149e374f52c5504a1450ab6 as a separate PR?

cjgillot avatar Aug 24 '24 15:08 cjgillot

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

Click to see the possible cause of the failure (guessed by this bot)

rust-log-analyzer avatar Aug 25 '24 04:08 rust-log-analyzer

It is probably necessary to make the lint handle associated types, I'd like an explanation why it is necessary

Just detecting more dead codes? I think removing more unused items is helpful. And we have detected unused inherit assoc types, although the feature is unstable for now.

and why we can't just rely on HIR and typeck-output resolutions.

We can only get Res::Err when meeting a assoc type in hir. typeck_results.qpath_res works but it only work for QPaths in an Expr or Pat node. And we may have cases like:

trait TwoWayStrategy {
    type Output;
    fn use_early_reject() -> bool;
    fn rejecting(a: usize, b: usize) -> Self::Output;
    fn matching(a: usize, b: usize) -> Self::Output;
}

I asked such things before in zulip

mu001999 avatar Aug 25 '24 05:08 mu001999

@rustbot ready

mu001999 avatar Aug 25 '24 07:08 mu001999

:umbrella: The latest upstream changes (presumably #129595) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Aug 26 '24 08:08 bors

:umbrella: The latest upstream changes (presumably #129941) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Sep 07 '24 23:09 bors

@cjgillot friendly ping

mu001999 avatar Oct 06 '24 08:10 mu001999

It is probably necessary to make the lint handle associated types, I'd like an explanation why it is necessary

@cjgillot

Updated: using simpler rules for assoc tys instead of reasoning on types, i.e., only marking assoc tys live only if the corresponding trait is live, which can help to lint more unused traits (is same to #126618 reverted in #128404).

Removes old logic for unused assoc tys. I think the it needs further consideration and could later be made as a separate PR.

mu001999 avatar Nov 03 '24 17:11 mu001999

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

Click to see the possible cause of the failure (guessed by this bot)

rust-log-analyzer avatar Nov 03 '24 17:11 rust-log-analyzer

Hi @cjgillot, it looks like this PR is ready for review again when you have time. Thanks 🙂

wesleywiser avatar Nov 14 '24 15:11 wesleywiser

seems cjgillot hasn't had much time recently, so let me roll the dice

r? compiler

mu001999 avatar Jan 06 '25 08:01 mu001999

:umbrella: The latest upstream changes (presumably #135272) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jan 24 '25 14:01 bors

I feel like I'm missing a lot of context as to the recent work on dead code lints and the description of the PR doesn't really explain any of it :thinking:

BoxyUwU avatar Jan 28 '25 18:01 BoxyUwU

@pnkfelix would you like to review this PR?

r? @pnkfelix

mu001999 avatar Jan 29 '25 02:01 mu001999

Failed to set assignee to pnkfelix: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

rustbot avatar Jan 29 '25 02:01 rustbot

r? @cjgillot

mu001999 avatar Jan 29 '25 02:01 mu001999

:umbrella: The latest upstream changes (presumably #134248) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jan 29 '25 22:01 bors

:umbrella: The latest upstream changes (presumably #137163) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Feb 17 '25 11:02 bors

:umbrella: The latest upstream changes (presumably #137162) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Feb 18 '25 08:02 bors

r? rust-lang/compiler

wesleywiser avatar Feb 27 '25 15:02 wesleywiser