Skip to content

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

Closed
@nrc

Description

@nrc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions