Skip to content

Misleading error when using fully qualified trait syntax without trait #98565

Closed

Description

@Cerber-Ursi

Given the following code:

trait StringExt {
    fn ext(&self);
}
impl StringExt for String {
    fn ext(&self) {
        <Self as str>::trim(self);
        // str::trim(self); - this works
    }
}

Playground

The current output is:

error[E0576]: cannot find method or associated constant `trim` in `str`
 --> src/lib.rs:6:24
  |
6 |         <Self as str>::trim(self);
  |                        ^^^^ not found in `str`

For more information about this error, try `rustc --explain E0576`.

However, str is not a trait, therefore the fully-qualified trait syntax should be simply invalid in this case - therefore, the error would better say the real problem ("str is not a trait"), probably with the help message suggesting the commented-out line.

Initially found in this SO question.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 27, 2022
  2. lyming2007 commented on Jul 12, 2022

    @lyming2007

    Did you mean the ideal output should like:

    error[E0576]: cannot find method or associated constant `trim` in `str`
     --> src/lib.rs:6:24
      |
    6 |         <Self as str>::trim(self);
      |                        ^^^^ `str` is not a trait
    
    For more information about this error, try `rustc --explain E0576`.
    

    ?

  3. Cerber-Ursi commented on Jul 12, 2022

    @Cerber-Ursi
    ContributorAuthor

    Not sure, to be honest. This might even better not be E0576, since the problem is not that "something wasn't found for the type" - it's "there's no type to search in".

  4. estebank commented on Oct 4, 2024

    @estebank
    Contributor

    Current output:

    error[E0404]: expected trait, found builtin type `str`
     --> src/lib.rs:6:18
      |
    6 |         <Self as str>::trim(self);
      |                  ^^^ not a trait
    
  5. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Oct 4, 2024
  6. jannden commented on Oct 20, 2024

    @jannden
    Contributor

    I would like to give it a try and add the test for this.

    @rustbot claim

  7. jannden commented on Oct 21, 2024

    @jannden
    Contributor

    @rustbot review

  8. rustbot commented on Oct 21, 2024

    @rustbot
    Collaborator

    Error: The "Ready" shortcut only works on pull requests.

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

  9. added a commit that references this issue on Oct 21, 2024
  10. added a commit that references this issue on Oct 21, 2024
  11. added a commit that references this issue on Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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