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

poor error message with question mark applied to non-Result #32175

Closed
durka opened this issue Mar 10, 2016 · 13 comments
Closed

poor error message with question mark applied to non-Result #32175

durka opened this issue Mar 10, 2016 · 13 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@durka
Copy link
Contributor

durka commented Mar 10, 2016

This code

#![feature(question_mark)]

fn main() {
    42?;
}

prints the error

<anon>:4:5: 4:9 error: mismatched types:
 expected `_`,
    found `core::result::Result<_, _>`
(expected integral variable,
    found enum `core::result::Result`) [E0308]
<anon>:4     42?;
             ^~~~

which is confusing. The error is that I put a non-Result where a Result was expected, but the error says the opposite.

cc @japaric

@durka
Copy link
Contributor Author

durka commented Mar 10, 2016

This is because it expands to

fn main() {
    match 42 {
        Err(err) => return Err(From::from(err)),
        Ok(val) => val,
    };
}

so the error comes from the match arms. I know the expansion is done before typechecking, but a possible way to fix it is with type ascription. This expansion produces a better error:

#![feature(type_ascription)]

fn main() {
    match 42: Result<_,_> {
        Err(err) => return Err(From::from(err)),
        Ok(val) => val,
    };
}

@japaric
Copy link
Member

japaric commented Mar 10, 2016

I think we can swap the expected and found types for these errors when they originate from desugared syntax and seems like we do something like that for if let. I'll give it a try.

@durka
Copy link
Contributor Author

durka commented Mar 10, 2016

@japaric

Heh, but if you switch them what of this code?

#![feature(question_mark, type_ascription)]

fn main() {
    (42: Result<_,_>)?;
}

@japaric
Copy link
Member

japaric commented Mar 10, 2016

I think that error message would remain unchanged between there is no type mismatch between the "head" (42: Result<_, _>) and the arm patterns.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 11, 2016
@pnkfelix
Copy link
Member

pnkfelix commented Jun 3, 2016

cc me

@cbreeden
Copy link
Contributor

cbreeden commented Sep 9, 2016

I am now seeing a new error message. @durka, how do you feel about this?

error[E0277]: the trait bound `{integer}: std::ops::Carrier` is not satisfied
 --> src/main.rs:4:5
  |
4 |     42?;
  |     ^^^ trait `{integer}: std::ops::Carrier` not satisfied
  |
  = help: the following implementations were found:
  = help:   <std::ops::_DummyErrorType as std::ops::Carrier>
  = help:   <std::result::Result<U, V> as std::ops::Carrier>
  = note: required by `std::ops::Carrier::translate`

error[E0277]: the trait bound `(): std::ops::Carrier` is not satisfied
 --> src/main.rs:4:5
  |
4 |     42?;
  |     ^^^ trait `(): std::ops::Carrier` not satisfied
  |
  = note: required by `std::ops::Carrier::from_error`

@durka
Copy link
Contributor Author

durka commented Sep 9, 2016

That's a pretty terrible error message from a user perspective. cc @nrc (about Carrier)

@carols10cents
Copy link
Member

Just to clarify: the examples in this issue are also showing the error messages when you try to use ? in a function that does not return a Result, which is #35946/#36988, but this issue is for trying to call ? on a non-Result, correct? To distinguish this issue from the other issues, I think a better example would be:

fn something() -> Result<(), ()> {
    42?;
    Ok(())
}

which currently results in this single error:

error[E0277]: the trait bound `{integer}: std::ops::Carrier` is not satisfied
 --> src/main.rs:5:5
  |
5 |     42?;
  |     ^^^ trait `{integer}: std::ops::Carrier` not satisfied
  |
  = help: the following implementations were found:
  = help:   <std::ops::_DummyErrorType as std::ops::Carrier>
  = help:   <std::result::Result<U, V> as std::ops::Carrier>
  = note: required by `std::ops::Carrier::translate`

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@cengiz-io
Copy link
Contributor

Hello
Any updates? I've just bumped into this.

cc @steveklabnik

@steveklabnik
Copy link
Member

hey @cengizio 👋

Not yet, no. There's some linked issues/PRs in the comments above, but it has not yet been fixed.

@nodakai
Copy link
Contributor

nodakai commented Jul 16, 2017

If #37671 had to be closed as a duplicate of #35946, so does this

@nodakai nodakai marked this as a duplicate of #35946 Jul 16, 2017
@carols10cents
Copy link
Member

@nodakai no, this issue is for using the ? on something that doesn't support the question mark operator, and this can happen in a function that does have a return type that supports a question mark. Please see my example in this comment, this is a distinct issue from both #37671 and #35946.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@estebank
Copy link
Contributor

estebank commented Feb 5, 2018

Current output:

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
 --> src/main.rs:2:5
  |
2 |     42?;
  |     ^^^ the `?` operator cannot be applied to type `{integer}`
  |
  = help: the trait `std::ops::Try` is not implemented for `{integer}`
  = note: required by `std::ops::Try::into_result`

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-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

10 participants