Skip to content

Suggest importable type that differs slightly#156239

Draft
GTimothy wants to merge 16 commits into
rust-lang:mainfrom
GTimothy:issue-suggest-similar-imports
Draft

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

Conversation

@GTimothy
Copy link
Copy Markdown
Contributor

@GTimothy GTimothy commented May 6, 2026

This PR proposes to add types that differ slightly to suggested imports.
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 followed the suggestions in #72988.
In this initial change, the list is simply expanded to types with one char edit distance.
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, but for now it is ignored.

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 struct
   |
LL + use std::alloc::Layout;
   |  

for a more complex case:

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){}
   |
help: consider importing this struct
   |
LL + use std::time::SystemTime;
   |

SystemTame's edit-distance is 2, so not suggested.

GTimothy and others added 3 commits May 6, 2026 14:02
Co-authored-by: Chris Simpkins <git.simpkins@gmail.com>
adds a is_exact_match field to ImportSuggestion that is unused for now.
suggestions now include 1-edit variations as suggestions
@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
Copy link
Copy Markdown
Contributor Author

GTimothy commented May 6, 2026

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'...

@rust-log-analyzer

This comment has been minimized.

GTimothy added 13 commits May 8, 2026 13:11
move and update case insensitive import suggestion to the end of
try_lookup_name_relaxed
filter out nonmacroattr
filter out import suggestions that are in prelude
other updates
-move case insensitive import suggestion to its own function
-call it at after all the other suggestion-adding functions
-track whether any of them actually added a suggestion by making them
return bools
-only call it if no other was made previously
-fix: actually call it after all other suggestion adding functions
-only suggest modules when next_segment is Some
-fix return value when case insensitive suggestions are made
-add missing booleans to err_code_special_case and
suggest_ident_hidden_by_hygiene
fix trait_and_bounds return values by separating into two booleans
add missing boolean outputs to detect_missing_bindings
other small changes
some code cleanup
-in typo import suggestion, filter suggestions with the wrong number of
parameters
-add a missing true case in suggest_typo
-code cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants