rust-lang / rust-lang/rust-analyzer

Unknown symbol import assist, if it finds a renamed import, imports the original name instead

Open
#16,802 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: 0.3.1868-standalone (037924c4d 2024-03-03)

rustc version: rustc 1.76.0 (07dca489a 2024-02-04)

code snippet to reproduce:

use std::io::Result as IoResult;

mod foo {
    pub fn bar() -> IoResult<()> {
        Ok(())
    }
}

This code produces the error “cannot find type IoResult in this scope”. Put the cursor on the IoResult and try to fix it using rust-analyzer assists.

  • What should happen: add use crate::IoResult;, or qualify as crate::IoResult.
  • What actually happens: add use std::io::Result;, which doesn't fix the error, or qualify as std::io::Result, which compiles but isn't appropriate (in the actual case I met this problem, the name is cfged to be one of two possible types).

Also, rustc provides a correct suggestion here, but there's no obvious way to apply it.

Arguably, for this purpose I should be using a type alias instead of use (which RA handles correctly), but there is clearly a bug at least in the case of importing rather than qualifying.

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 issue with the Rust snippet and the unknown-symbol import assist, then trace how the assist resolves renamed imports. Done means the import or qualification preserves the visible alias and fixes the unresolved name, with a regression test covering this case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.