close
Skip to content

Suggest importable type that differs slightly#156239

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

Suggest importable type that differs slightly#156239
GTimothy wants to merge 19 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 propose 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){}
   |

The work also checks for expected parameter counts to avoid suggesting incompatible types.

  • 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 count match before suggesting an enum
  • squash and split commits for a clean PR

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

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

@rust-log-analyzer

This comment has been minimized.

GTimothy added 16 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
use is_exact_match field to customize help message
remove a nonmacroattr condition
only suggest modules if the following segment matches too
cleanup
bless customized help message
undo blessing a bad suggestion
test stderr moved
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