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

Providing a trait object as a type parameter to a function causes nonsense error #47990

Closed
sgrif opened this issue Feb 3, 2018 · 3 comments · Fixed by #68377
Closed

Providing a trait object as a type parameter to a function causes nonsense error #47990

sgrif opened this issue Feb 3, 2018 · 3 comments · Fixed by #68377
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. 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.

Comments

@sgrif
Copy link
Contributor

sgrif commented Feb 3, 2018

I had mistakenly written this code in crates.io: conn.transaction::<_, CargoError, _>(...) (note: CargoError is a trait). I should have written Box<CargoError>. That resulted in this compiler error, which makes absolutely no sense:

error: the `transaction` method cannot be invoked on a trait object
   --> src/user/mod.rs:546:10
    |
546 |     conn.transaction::<_, CargoError, _>(|| {
    |          ^^^^^^^^^^^
    |
    = note: another candidate was found in the following trait, perhaps add a `use` for it:
            candidate #1: `use diesel::Connection;`

I am not invoking the method on a trait object, nor would use have any effect as the Connection trait is already in scope. For reference, the signature of the method being called is:

fn transaction<T, E, F>(&self, f: F) -> Result<T, E>
where
    F: FnOnce() -> Result<T, E>,
    E: From<Error>,
@TimNN TimNN added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 6, 2018
@comex
Copy link
Contributor

comex commented Apr 1, 2018

I ran into this too. For the record, it only applies to method calls using dot syntax, not all function calls.

Reproducer: https://play.rust-lang.org/?gist=8460beff8fd0243103d587ebb15006dd&version=nightly

@dtolnay dtolnay added C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 6, 2019
@dtolnay
Copy link
Member

dtolnay commented May 6, 2019

We hit this in #rust today with @Yatekii. Retagging as a bug. What makes this confusing is that the following two situations show exactly the same error message even though the nature of the problem is different. The first situation is more common so people recognize the error message as pertaining to that, and don't think to look for the second situation in their code.

trait Trait {
    fn f<T>(&self) where Self: Sized {}
}

struct Param;

fn f(receiver: &dyn Trait) {
    receiver.f::<Param>();
}

fn main() {}
trait Trait {}

struct Receiver;
impl Receiver {
    fn f<T>(&self) {}
}

fn main() {
    let receiver = Receiver;
    type Param = dyn Trait;
    receiver.f::<Param>();
}
error: the `f` method cannot be invoked on a trait object
  --> src/main.rs:11:14
   |
11 |     receiver.f::<Param>();
   |              ^

@estebank estebank added 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 Jan 30, 2020
@estebank
Copy link
Contributor

Would the following output be enough to help in these cases?

error: the `bar` method cannot be invoked on a trait object
  --> file.rs:10:9
   |
5  |     fn bar<T>(&self) {}
   |            - this has a `Sized` requirement
...
10 |     foo.bar::<Trait>(&foo);
   |         ^^^
error: the `f` method cannot be invoked on a trait object
 --> file.rs:8:14
  |
2 |     fn f<T>(&self) where Self: Sized {}
  |                                ----- this has a `Sized` requirement
...
8 |     receiver.f::<Param>();
  |              ^

estebank added a commit to estebank/rust that referenced this issue Feb 2, 2020
@bors bors closed this as completed in 5b0caef Feb 4, 2020
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 C-bug Category: This is a bug. 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants