Skip to content

Targeted suggestions for specific "no method" errors on standard library types #59450

Closed

Description

@dtolnay

Rustc currently shows a perfectly reasonable error message when calling a nonexistent method:

use std::collections::BTreeSet;

fn main() {
    let mut set = BTreeSet::new();
    set.push("");
}
error[E0599]: no method named `push` found for type `std::collections::BTreeSet<_>` in the current scope
 --> src/main.rs:5:9
  |
5 |     set.push("");
  |         ^^^^

But for specific frequently encountered cases, it would be great to show a more targeted error that gives the name of the method you almost definitely meant to call, despite not being similar enough to trigger the "did you mean" suggestion.

Candidates would be cases that arise often from refactoring (this applies to BTreeSet::push, which I see every now and then after replacing a Vec with a BTreeSet) or cases of a Rust API being named differently from an equivalent C++ API.

  • BTreeSet::push ⟶ suggest insert
  • Vec::push_back ⟶ suggest push
  • VecDeque::push ⟶ suggest push_back
  • Vec::length, Vec::size ⟶ suggest len

Activity

  1. estebank commented on Mar 27, 2019

    @estebank
    Contributor

    Do you think this would be a common enough pattern that it'd be worth creating something like rustc_on_unimplemented but for confusables?

    Something like

    trait BTreeSet {
        #[rustc_confusables("insert")]
        fn push() {}
    }
    
    error[E0599]: no method named `push` found for type `std::collections::BTreeSet<_>` in the current scope
     --> src/main.rs:5:9
      |
    5 |     set.push("");
      |         ^^^^ help: you might have meant to use `insert`
    
  2. dtolnay commented on Mar 27, 2019

    @dtolnay
    MemberAuthor

    Something similar to rustc_on_unimplemented sounds like it would work! I don't know enough about the diagnostics implementation to know what the other options are.

    Bonus points if it is straightforward for a new rustc contributor to add a confusable and corresponding test case.

  3. added
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 8, 2020
  4. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Jun 11, 2020
  5. fmease commented on Oct 19, 2022

    @fmease
    Member

    We could also reuse (abuse?) #[doc(alias = "…")] for this. I think I've seen this mentioned a few times.

  6. kadiwa4 commented on Jan 23, 2023

    @kadiwa4
    Contributor

    Maybe relevant: #107108 adds doc(alias) support to that error

  7. jieyouxu commented on Feb 1, 2024

    @jieyouxu
    Member

    Now that PR #112239 is merged, it should be easy to add #[rustc_confusables(...)] to some of these methods along with some tests.

  8. estebank commented on Feb 14, 2024

    @estebank
    Contributor

    I'll have #120730 close this ticket. We can open new ones for specific methods that require more aliases.

  9. added a commit that references this issue on Feb 15, 2024
    c461e3b
  10. added a commit that references this issue on Feb 17, 2024
    f2d94c9
  11. added a commit that references this issue on Feb 23, 2024
    a28d221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions