Skip to content

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

Closed
@Cerber-Ursi

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.

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