rust-lang / rust-lang/rust-analyzer

Rust analyzer is unable to resolve trait method invocations

Open
#11,206 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ty C-support
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command)

rust-analyzer 2022-01-03

rustc version: (eg. output of rustc -V)

rustc 1.57.0 (f1edd0429 2021-11-29)

Issue summary

Renaming a trait method breaks the semantics of the project. Find references finds function invocations that do not have the proper signature, and thus cannot be invocations of a given trait method.

Renaming is a common refactoring option that should under no circumstances change the behaviour of the program. It should properly resolve which symbols have which name, change only the names of the symbols which resolve to the same symbol and change them.

However, as I can demonstrate, due to incorrect method invocation resolution in rust_analyzer functions of a completely different kind get renamed and cause a compilation error.

MWE

Steps to reproduce

  • Clone hyperledger iroha 2: git clone git@github.com:hyperledger/iroha.git
  • Checkout the iroha 2 development branch git checkout iroha2-dev
  • Open the file iroha/core/test_network/src/lib.rs
  • Rename the trait method definition on line 125 of the file to test_genesis
Expected behaviour

Only the trait impl, and the instances of the trait invocation are renamed.

Actual behaviour

Every instance of a trait method invocation named test is renamed. Most obvious example is client/tests/integration_tests/asset_propagation.rs line 47. The function called test is an invocation of a similarly named trait method, but has different signature, two arguments, none of which are boolean.

Possible solution

Renaming and finding references (usage) seems to not do full resolution. As a result in a relatively large but simple project like Iroha, there seem to be many false positives, when either renaming or finding references. This makes the refactoring facilities of rust_analyzer almost completely useless (as a human intervention is needed anyway, and a primitive method call resolution is a few lines of regex).

I suspect that this might be related to the functions being in different workspaces, as all instances of incorrect highlight/rename are in crates outside of the test_network crate. However, I think that this is a common-enough use case to be worth fixing.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the rename from iroha/core/test_network/src/lib.rs at the trait method on line 125, using the iroha2-dev branch and the reported Rust Analyzer and rustc versions. Compare references with client/tests/integration_tests/asset_propagation.rs line 47, then trace rename and find-references resolution across the affected workspaces. Done means only the matching trait definition, impl, and invocations are renamed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.