rust-lang / rust-lang/rust-analyzer

`rust-analyzer` confusion when importing traits with functions with same name

Open
#12,119 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

S-unactionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 24cf95762 2022-04-11 stable

rustc version: 1.58.1

rust-analyzer confuses the function being used when you import traits with functions with the same name, in particular when these functions receive a different number of parameters each. In execution time, the correct function will be used, but the analyzer assumes there is an error with the number of parameters expected/found.

In particular, I came across this issue when calling the function rand() from ark-poly::polynomial::UVPolynomial (uses 2 parameters) and rand() from ark-ff::UniformRand (uses 1 parameter). Some small example of what is going on:

use ark_ec::AffineCurve;
use ark_ff::UniformRand;
use ark_poly::polynomial::UVPolynomial;

type ScalarField<G> = <G as AffineCurve>::ScalarField;

pub fn main<G: AffineCurve>() {
    let rng = &mut rand::rngs::OsRng;
    let example = ScalarField::<G>::rand(rng);
}

Here, the rust-analyzer complains in the last line with expected 2 arguments, found 1rust-analyzer[mismatched-arg-count](https://rust-analyzer.github.io/manual.html#mismatched-arg-count), as it expects the 2 arguments from UVPolynomial. At the same time, the IDE shows that the use ark_poly::polynomial::UVPolynomial; line is not being used. When executing, the rand that gets to be used is the one in use ark_ff::UniformRand;.

One way to "fix" this error is to cast that last line as let example = <ScalarField<G> as UniformRand>::rand(rng);, but it looks unnecessary as well as verbose given that in execution time this clarification is not needed.

Any ideas why is this happening?

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

Start by reproducing the diagnostic with the Rust example in the issue, using the two imported traits and their same-named rand functions. Trace rust-analyzer's trait method resolution and argument-count diagnostic; done means the one-argument call resolves to UniformRand::rand without falsely reporting a mismatch or marking UVPolynomial as unused.

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.