Skip to content

Suggest importable type that differs slightly#156239

Open
GTimothy wants to merge 5 commits into
rust-lang:mainfrom
GTimothy:issue-suggest-similar-imports
Open

Suggest importable type that differs slightly#156239
GTimothy wants to merge 5 commits into
rust-lang:mainfrom
GTimothy:issue-suggest-similar-imports

Conversation

@GTimothy

@GTimothy GTimothy commented May 6, 2026

Copy link
Copy Markdown
Contributor

This PR proposes case insensitive import suggestions.
Previous discussion of this topic here: #72641 and in this PR: #72988
If that is still of interest, a discussion may be needed to limit or expand the included list.
I initially followed the suggestions in #72988.

The tests are based on the tests by @chrissimpkins in #72988.

I added a is_exact_match field to ImportSuggestion to eventually modify the suggestion text accordingly.

Only when no other suggestion is made, do I check for case insensitive import suggestion.

fn test_layout(_x: LayOut){}
//~^ ERROR: cannot find type `LayOut` in this scope
error[E0425]: cannot find type `LayOut` in this scope
  --> $DIR/libstd.rs:3:20
   |
LL | fn test_layout(_x: LayOut){}
   |                    ^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::alloc::Layout;
   |

for a more complex case:
SystemTime exists in the stdlib, but I only suggest case insensitive import when nothing else is suggested

fn test_systemtime(_x: Systemtime){}
//~^ ERROR: cannot find type `Systemtime` in this scope

struct SystemTome{}
mod st{
    struct SystemTame{}
}
error[E0425]: cannot find type `Systemtime` in this scope
  --> $DIR/libstd.rs:428:24
   |
LL | fn test_systemtime(_x: Systemtime){}
   |                        ^^^^^^^^^^
...
LL | struct SystemTome{}
   | ----------------- similarly named struct `SystemTome` defined here
   |
help: a struct with a similar name exists
   |
LL - fn test_systemtime(_x: Systemtime){}
LL + fn test_systemtime(_x: SystemTome){}
   |

During this PR, I ended up adding two other things which could arguably be two separate PRs:

  • filtering out typos suggestions that do not have parameters when the typo has some
    motivation: It suggested Clone (no parameter) for the std cloned<(),()> test when I expected Cloned<(),()> to be suggested.

  • when suggesting that a missing binding is available in a pattern but not used because behind a .., suggest a MaybeIncorrect fix.
    motivation: this way, I can avoid looking for imports if there is a suggestion.

  • custom import message for case insensitive suggestion.

  • only suggest case insensitive when no other suggestion is made

  • check for enum variant match before suggesting an enum

  • check for enum variant parameter requirement before suggesting an enum

  • check with reviewers where to place the test, and whether to rename them

  • squash and split commits for a clean PR

@rustbot rustbot added 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. labels May 6, 2026
@GTimothy GTimothy force-pushed the issue-suggest-similar-imports branch from 36cee45 to a23f444 Compare May 6, 2026 12:06
@GTimothy

GTimothy commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

I see from the 247 failed tests that 1-edit difference is too big for small names. 'net' should probably NOT be suggested to replace 'new'...

Edit: i have now switched to case insensitive match only.

@rust-log-analyzer

This comment has been minimized.

@GTimothy GTimothy force-pushed the issue-suggest-similar-imports branch from 7719f50 to 158417a Compare May 31, 2026 17:11
GTimothy and others added 2 commits May 31, 2026 22:25
Co-authored-by: Chris Simpkins <git.simpkins@gmail.com>
adds a is_exact_match field to ImportSuggestion
use is_exact_match field to customize help message
suggest imports with different casing
-only suggest modules if the following segment matches too
@GTimothy GTimothy force-pushed the issue-suggest-similar-imports branch from 158417a to fdc8fcc Compare May 31, 2026 20:44
@GTimothy GTimothy force-pushed the issue-suggest-similar-imports branch from fdc8fcc to 2893b67 Compare June 1, 2026 20:18
@GTimothy GTimothy marked this pull request as ready for review June 2, 2026 06:56
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 2, 2026
@rustbot

rustbot commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants