Skip to content

Suggest importable type that differs slightly#156239

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

Suggest importable type that differs slightly#156239
GTimothy wants to merge 9 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 3 times, most recently 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

@rust-bors

This comment has been minimized.

@GTimothy

GTimothy commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

upstream change #157991 contains commit cd2d10a and 4f6a600 from #157974 which do some weird file swapping instead of renamings that completely confuses and breaks my rebase attempts.
I may have some skill issue here, does anyone know how to fix this? Is there a proper git workflow to resolve this or do I need to copy my changes over to the swapped file and delete the changes in the original file by hand?

If this is not a skill issue on my part, maybe avoiding this kind of file swapping and splitting this in multiple rename commits would help git not getting confused ?

GTimothy and others added 9 commits June 24, 2026 15:35
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
When a pattern has `..` and a matching binding, suggest replacing `..`
with `binding, ..`
@GTimothy GTimothy force-pushed the issue-suggest-similar-imports branch from 1bcb37c to 981c290 Compare June 24, 2026 14:02
@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
To only update this specific test, also pass `--test-args libstd-case-typo/libstd.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/libstd-case-typo/libstd.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/libstd-case-typo/libstd" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0425]: cannot find type `LayOut` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/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;
   |

error[E0425]: cannot find type `system` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:5:20
   |
LL | fn test_system(_x: system){}
   |                    ^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::alloc::System;
   |

error[E0425]: cannot find type `Typeid` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:10:20
   |
LL | fn test_typeid(_x: Typeid){}
   |                    ^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::any::TypeId;
   |

error[E0425]: cannot find type `Escapedefault` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:15:27
   |
LL | fn test_escapedefault(_x: Escapedefault){}
   |                           ^^^^^^^^^^^^^ not found in this scope
   |
help: consider importing one of these similarly named structs
   |
LL + use std::ascii::EscapeDefault;
---

error[E0425]: cannot find type `cell` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:20:18
   |
LL | fn test_cell(_x: cell<()>){}
   |                  ^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::cell::Cell;
   |

error[E0425]: cannot find type `DecodeUTF16` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:25:25
   |
LL | fn test_decodeutf16(_x: DecodeUTF16<()>){}
   |                         ^^^^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::char::DecodeUtf16;
   |

error[E0425]: cannot find type `Escapeunicode` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:28:27
   |
LL | fn test_escapeunicode(_x: Escapeunicode){}
   |                           ^^^^^^^^^^^^^ not found in this scope
   |
help: consider importing one of these similarly named structs
   |
LL + use std::char::EscapeUnicode;
   |
LL + use std::str::EscapeUnicode;
   |

error[E0425]: cannot find type `Tolowercase` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:31:25
   |
LL | fn test_tolowercase(_x: Tolowercase){}
   |                         ^^^^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::char::ToLowercase;
   |

error[E0425]: cannot find type `Touppercase` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:34:25
   |
LL | fn test_touppercase(_x: Touppercase){}
   |                         ^^^^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::char::ToUppercase;
   |

error[E0425]: cannot find type `reverse` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:39:21
   |
LL | fn test_reverse(_x: reverse<()>){}
   |                     ^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::cmp::Reverse;
   |

error[E0425]: cannot find type `BtreeMap` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:44:22
   |
LL | fn test_btreemap(_x: BtreeMap<(), ()>){}
   |                      ^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::BTreeMap;
   |

error[E0425]: cannot find type `BtreeSet` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:46:22
   |
LL | fn test_btreeset(_x: BtreeSet<()>){}
   |                      ^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::BTreeSet;
   |

error[E0425]: cannot find type `Binaryheap` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:48:24
   |
LL | fn test_binaryheap(_x: Binaryheap<()>){}
   |                        ^^^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::BinaryHeap;
   |

error[E0425]: cannot find type `Hashmap` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:50:21
   |
LL | fn test_hashmap(_x: Hashmap<String, ()>){}
   |                     ^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::HashMap;
   |

error[E0425]: cannot find type `Hashset` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:52:21
   |
LL | fn test_hashset(_x: Hashset<()>){}
   |                     ^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::HashSet;
   |

error[E0425]: cannot find type `Linkedlist` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:54:24
   |
LL | fn test_linkedlist(_x: Linkedlist<()>){}
   |                        ^^^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::LinkedList;
   |

error[E0425]: cannot find type `Vecdeque` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:56:22
   |
LL | fn test_vecdeque(_x: Vecdeque<()>){}
   |                      ^^^^^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |
LL + use std::collections::VecDeque;
   |

error[E0425]: cannot find type `args` in this scope
##[error]  --> /checkout/tests/ui/libstd-case-typo/libstd.rs:61:18
   |
LL | fn test_args(_x: args){}
   |                  ^^^^ not found in this scope
   |
help: consider importing this similarly named struct
   |

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