Skip to content

Error when passing a trait object as a generic type doesn't indicate what is the trait object #61525

New issue

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

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

Already on GitHub? # to your account

Closed
shepmaster opened this issue Jun 4, 2019 · 3 comments · Fixed by #99146
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@shepmaster
Copy link
Member

pub trait Example {
    fn query<Q>(self, q: Q);
}

impl Example for i32 {
    fn query<Q>(self, _: Q) {
        unimplemented!()
    }
}

mod nested {
    use super::Example;
    fn example() {
        1.query::<dyn ToString>("")
    }
}
error: the `query` method cannot be invoked on a trait object
  --> src/lib.rs:14:11
   |
14 |         1.query::<dyn ToString>("")
   |           ^^^^^
   |
   = note: another candidate was found in the following trait, perhaps add a `use` for it:
           `use crate::Example;`

Issues with this diagnostic:

  1. The query method is not being invoked on a trait object; it's invoked on the concrete value 1.
  2. The note suggests importing the same trait that is already being reported about.
@shepmaster shepmaster added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 4, 2019
@shepmaster
Copy link
Member Author

/cc @estebank

#57744 also looks relevant based on the title, but appears to have a different enough error message.

@estebank estebank added A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 5, 2019
@estebank
Copy link
Contributor

estebank commented Feb 1, 2020

Is the following output from #68377 enough?

error: the `query` method cannot be invoked on a trait object
  --> file.rs:14:11
   |
2  |     fn query<Q>(self, q: Q);
   |              - this has a `Sized` requirement
...
14 |         1.query::<dyn ToString>("")
   |           ^^^^^
   |
   = note: another candidate was found in the following trait, perhaps add a `use` for it:
           `use Example;`

@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Feb 2, 2020
@shepmaster
Copy link
Member Author

I don't feel like it's at the usual amazing quality 😿. My points still appear to exist:

  1. The query method is not being invoked on a trait object; it's invoked on the concrete value 1.
  2. The note suggests importing the same trait that is already being reported about.

In some magical world, I might hope for something like this (and I'm very flexible in that):

error: the `query` method cannot be invoked on a trait object
  --> src/lib.rs:14:11
   |
14 |         1.query::<dyn ToString>("")
   |           ^^^^^
   = note: the method receiver is a trait object because of the generic type parameter
   |
14 |         1.query::<dyn ToString>("")
   |                   ^^^^^^^^^^^^
   = note: the trait `crate::Example` defines this method but it's not available because
   |
2  |     fn query<Q>(self, q: Q);
   |              - this has a `Sized` requirement

I tried to address my points via the notes.

bors added a commit that referenced this issue Feb 4, 2020
Tweak obligation error output

- Point at arguments or output when fn obligations come from them, or ident when they don't
- Point at `Sized` bound (fix #47990)
- When object unsafe trait uses itself in associated item suggest using `Self` (fix #66424, fix #33375, partially address #38376, cc #61525)
-  Point at reason in object unsafe trait with `Self` in supertraits or `where`-clause (cc #40533, cc #68377)
- On implicit type parameter `Sized` obligations, suggest `?Sized` (fix #57744, fix #46683)
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@compiler-errors compiler-errors self-assigned this Jul 11, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 11, 2022
Do not error during method probe on `Sized` predicates for types that aren't the method receiver

Fixes rust-lang#61525

This is safe even though we're skipping an error because we end up confirming the method, which means we're still checking the `Sized` predicate in the end. It just means that we don't emit an erroneous message as below:

```
error: the `query` method cannot be invoked on a trait object
  --> src/lib.rs:14:11
   |
14 |         1.query::<dyn ToString>("")
   |           ^^^^^
   |
   = note: another candidate was found in the following trait, perhaps add a `use` for it:
           `use crate::Example;`
```

Also fixes erroneously suggesting the same trait over again, as seen in the `issue-35976.rs` UI test.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 11, 2022
Do not error during method probe on `Sized` predicates for types that aren't the method receiver

Fixes rust-lang#61525

This is safe even though we're skipping an error because we end up confirming the method, which means we're still checking the `Sized` predicate in the end. It just means that we don't emit an erroneous message as below:

```
error: the `query` method cannot be invoked on a trait object
  --> src/lib.rs:14:11
   |
14 |         1.query::<dyn ToString>("")
   |           ^^^^^
   |
   = note: another candidate was found in the following trait, perhaps add a `use` for it:
           `use crate::Example;`
```

Also fixes erroneously suggesting the same trait over again, as seen in the `issue-35976.rs` UI test.
@bors bors closed this as completed in 5e223dc Jul 12, 2022
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants