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 from try! or ? and type-mismatch #36988

Closed
nrc opened this issue Oct 5, 2016 · 10 comments
Closed

Poor error message from try! or ? and type-mismatch #36988

nrc opened this issue Oct 5, 2016 · 10 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@nrc
Copy link
Member

nrc commented Oct 5, 2016

For try! we used to give a type mismatch message when the type of the value did not match the function type (see example below). We now give a message about the Carrier trait, which the user should not know about, e.g., error[E0277]: the trait bound(): std::ops::Carrieris not satisfied for the below example.

I removed the following from doc/error-index.md

Another situation in which this occurs is when you attempt to use the `try!`
macro inside a function that does not return a `Result<T, E>`:

```compile_fail,E0308
use std::fs::File;

fn main() {
    let mut f = try!(File::create("foo.txt"));
}

This code gives an error like this:

<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
     found `core::result::Result<_, _>`
 (expected (),
     found enum `core::result::Result`) [E0308]

try! returns a Result<T, E>, and so the function must. But main() has
() as its return type, hence the error.

@durka
Copy link
Contributor

durka commented Oct 6, 2016

Yeah, the error about Carrier is even worse than before (which even then was not enough to get users all the way to "I need to change this function to return a Result"). Should we make Carrier a lang item so it can be special-cased here?

@nikomatsakis
Copy link
Contributor

This feels like a serious problem. This is an area where user confusion is common and it's a particular stumbling block for new users (the old message itself was a problem). I agree with @durka that it is probably worth special-casing here!

cc @wycats @jonathandturner

@sophiajt
Copy link
Contributor

Is the special case here that it expects () as the output but sees a Result?

@durka
Copy link
Contributor

durka commented Oct 11, 2016

@jonathandturner we should be able to go further than that, since the message says "in this expansion of try" (at least it used to -- in nightly this info has been lost in favor of "macro outside of the current crate"), e.g. it knows about the macro. In the case of ? the compiler obviously knows about that too.

@sophiajt
Copy link
Contributor

@durka - sounds good

The macro backtrace should still be there for the try! case, so you could detect the type mismatch and before reporting it check the backtrace to see if we're calling through a try!

@durka
Copy link
Contributor

durka commented Oct 11, 2016

If someone can give me a pointer on how to get the macro backtrace during error reporting, I can work on this.

@sophiajt
Copy link
Contributor

@durka - you can check macro_backtrace in src/libsyntax/codemap.rs. Looks like it starts at about line 806.

Looks like E0308 is coming from report_and_explain_type_error in src/librustc/infer/error_reporting.rs. I think you should be able to dig through self.tcx.gcx.sess to get the codemap for the backtrace.

@carols10cents
Copy link
Member

Is this the same as #35946?

@durka
Copy link
Contributor

durka commented Oct 11, 2016

@carols10cents yes.

@sophiajt
Copy link
Contributor

Close as a dupe of #35946

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
Projects
None yet
Development

No branches or pull requests

5 participants