Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Cerber-Ursi opened this issue Jun 27, 2022 · 6 comments · Fixed by #131991
Closed

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

Cerber-Ursi opened this issue Jun 27, 2022 · 6 comments · Fixed by #131991
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Cerber-Ursi
Copy link
Contributor

Cerber-Ursi commented Jun 27, 2022

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.

@Cerber-Ursi Cerber-Ursi added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 27, 2022
@lyming2007
Copy link

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

?

@Cerber-Ursi
Copy link
Contributor Author

Cerber-Ursi commented Jul 12, 2022

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

@estebank
Copy link
Contributor

estebank commented Oct 4, 2024

Current output:

error[E0404]: expected trait, found builtin type `str`
 --> src/lib.rs:6:18
  |
6 |         <Self as str>::trim(self);
  |                  ^^^ not a trait

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Oct 4, 2024
@jannden
Copy link
Contributor

jannden commented Oct 20, 2024

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

@rustbot claim

@jannden
Copy link
Contributor

jannden commented Oct 21, 2024

@rustbot review

@rustbot
Copy link
Collaborator

rustbot commented Oct 21, 2024

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.

jannden added a commit to jannden/rust that referenced this issue Oct 21, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 21, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#130350 (stabilize Strict Provenance and Exposed Provenance APIs)
 - rust-lang#131737 (linkchecker: add a reminder on broken links to add new/renamed pages to `SUMMARY.md` for mdBooks)
 - rust-lang#131991 (test: Add test for trait in FQS cast, issue rust-lang#98565)
 - rust-lang#131997 (Make `rustc_abi` compile on stable again)
 - rust-lang#131999 (Improve test coverage for `unit_bindings` lint)
 - rust-lang#132001 (fix coherence error for very large tuples™)
 - rust-lang#132003 (update ABI compatibility docs for new option-like rules)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in a9da2be Oct 21, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 21, 2024
Rollup merge of rust-lang#131991 - jannden:issue-98565-test, r=jieyouxu

test: Add test for trait in FQS cast, issue rust-lang#98565

Closes rust-lang#98565 by adding a test to check for diagnostics when the built-in type `str` is used in a cast where a trait is expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants