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

give a specific error message for the wrong return type #28953

Closed
sourcefrog opened this issue Oct 10, 2015 · 5 comments
Closed

give a specific error message for the wrong return type #28953

sourcefrog opened this issue Oct 10, 2015 · 5 comments

Comments

@sourcefrog
Copy link
Contributor

This can be confusing for people new to rust and perhaps trying to copy some example code from https://doc.rust-lang.org/std/fs/struct.File.html:

use std::fs::File;

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

https://play.rust-lang.org/?gist=2201429be6f0833e2161&version=stable

At the moment this produces

<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
<std macros>:1:1: 6:48 note: in expansion of try!
<anon>:4:17: 4:46 note: expansion site
<std macros>:5:8: 6:42 help: see the detailed explanation for E0308
error: aborting due to previous error
playpen: application terminated with error code 101

https://doc.rust-lang.org/error-index.html#E0308 is OK but may be hard to see how it applies.

Perhaps the friendliest thing would be to suggest using unwrap() rather than try!, though perhaps that is too specialized.

I think there was another bug about main returning a Result?

@steveklabnik
Copy link
Member

We have decided, generally, to assume that when reading API docs, you already know Rust. If you click the link to show the example in the playpen, it will insert the stuff needed to make it compile.

That said, E0308 could use a specific call-out of this situation.

And yes, there's an RFC repo issue about main and Result.

@sourcefrog
Copy link
Contributor Author

Yes, I wasn't suggesting doing this in the File documentation, which would be unscalable.

E0308 says "This error occurs when the compiler was unable to infer the concrete type of a variable", but there's no variable involved here, at least at the user-visible level.

Could we have a more specific error code for 'returning X in a function declared to return Y'? Or, otherwise, mention in the documentation for E0308 that it covers that case too?

@steveklabnik
Copy link
Member

/cc #27112

@sourcefrog sourcefrog changed the title explain why try! can't be used from functions not returning an error, such as main give a specific error message for the wrong return type Nov 6, 2015
@sourcefrog
Copy link
Contributor Author

Given #27112 exists, I think the real heart remaining part of this is https://doc.rust-lang.org/error-index.html#E0308 says "wrong type for a variable" but it would be clearer with "wrong return type."

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 7, 2016
@sourcefrog
Copy link
Contributor Author

sourcefrog commented Jan 12, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants