Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upQuick draft "Result::expect" rfc #1119
Conversation
thepowersgang
referenced this pull request
May 13, 2015
Merged
libcore/Result - Add an `expect` method that prints a message and the `Err` value #25359
nagisa
reviewed
May 13, 2015
|
|
||
| # Drawbacks | ||
|
|
||
| - It involves adding a new method to a core rust type. |
This comment has been minimized.
This comment has been minimized.
gkoz
reviewed
May 13, 2015
|
|
||
| # Summary | ||
|
|
||
| Add an `expect` method to the Result type, bounded to `E: Debug` |
This comment has been minimized.
This comment has been minimized.
gkoz
May 13, 2015
The bound should probably be Display.
The output of unwrap and Debug isn't meant to reach the user, in that context adding a function that shows a message seems pointless. If it's Display, this would remove the need to do panic!("error: {}", e); (example).
This comment has been minimized.
This comment has been minimized.
thepowersgang
May 13, 2015
Author
Contributor
unwrap and expect are very similar methods. expect is intended to also to not be for user consumption (it panics after all).
The intent is to provide a version of unwrap that provides context on the error.
This comment has been minimized.
This comment has been minimized.
gkoz
May 13, 2015
it panics after all
Yeah, command line utilities (like rustc) have the luxury of using panics for error handling. It would be disappointing to have expect that can't be used for that.
This comment has been minimized.
This comment has been minimized.
nagisa
May 13, 2015
Contributor
In rustc an occurence of panic is ICE which should be fixed, so that’s a bad example.
This comment has been minimized.
This comment has been minimized.
gkoz
May 13, 2015
A panic caused by an ICE gets caught and then the code goes on to panic. Are you suggesting the latter should be fixed?
Both of you seem to be implying that a panic is not a valid way for a utility to die on a fatal error. Any citation for that?
This comment has been minimized.
This comment has been minimized.
quantheory
May 13, 2015
Contributor
According to RFC 236, which is focused on the standard library but is intended to be a guideline for Rust libraries in general, panics should only be used for bugs (not user facing) or when there is nothing else that you can meaningfully do (e.g. out of memory). This is why unwrap uses a Debug bound currently, AFAIK. Using unwrap or expect is basically an assertion that you will never get an Err value (unless there is a bug in your program, of course), and thus that a user will never see this error.
It is absolutely the case that whenever rustc panics, it is due to a bug. The code you linked should execute rarely or never in a release, but it exists in order to assist the user in filing a bug report when there is an error. There's no expectation that the user will be able to understand anything about the output, other than that they should send it to the Rust developers in a bug report.
My understanding is that, if you want a utility to be able to die in "normal" circumstances (i.e. not a bug in the program itself), it's preferable not to use an uncaught panic, but to print a message yourself and then call std::process::exit. We don't have any hard documentation that I know of about this, but I think that's in part because std::process::exit was only stabilized recently (a month or so ago, during the beta/release rush?).
If you want destructors to run, you do have to return up to main (or whatever level has the destructors you need) before exiting, which is an inconvenience in the current design. I think #1100 is intended to address this in part by allowing panics to be done more "quietly" and then caught at a higher level (but it is still very coarse).
This comment has been minimized.
This comment has been minimized.
bluss
May 13, 2015
panics should only be used for bugs (not user facing) or when there is nothing else that you can meaningfully do
Isn't this view too narrow for a general programming language? That kind of rule works for a mature application, but does it work on the way there? Or for tests, examples, wild experiments? I think we need to think of that kind of code too, and allow some easy escapes like .unwrap() or .expect().
This comment has been minimized.
This comment has been minimized.
quantheory
May 13, 2015
Contributor
To be clear:
a) I'm just giving my interpretation of the vague current conventions, not something that I have 110% support for.
b) The idea is not that you should avoid unwrap/expect, it's that they act as assertions that should only fail during testing/development, when a developer is the one encountering the error rather than a user. Therefore it's fine to use Debug instead of Display. So for "tests, examples, wild experiments," and new projects, having a lot of panics is not a problem, because in theory the people encountering these errors are either developers or relatively advanced users testing an experimental/beta version. (If Debug output is impossible even for a developer to understand, that's a whole separate problem.)
(This discussion reminds me of a rant I read about enabling the assert macro in release builds of C applications a while back. Though the situation is not exactly analogous, since in this case the assertion is still "there" in the release code, we just don't expect it to ever trigger, regardless of how bad the inputs to the program are.)
This comment has been minimized.
This comment has been minimized.
gkoz
May 13, 2015
@quantheory thanks for the elaboration.
If Debug output is impossible even for a developer to understand, that's a whole separate problem.
It's more that the Debug output is sometimes less helpful in the case of unwrapping Results:
- If this is a
Result<_, String>we get a string with newlines rendered as\ninstead of just text. - If this is an
io::Result, we getOs(some_number)instead of the actual error message.
So if expect is to provide some convenience over unwrap, a middle ground between just spitting out a line number of the failed assert and using proper error handling, utilizing Display feels like better balance to me.
This comment has been minimized.
This comment has been minimized.
bluss
commented
May 13, 2015
|
Seems logical to me. After you get to know .expect() on Option, you'll expect to find the method on Result too. |
alexcrichton
added
the
T-libs
label
May 14, 2015
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nielsle
commented
May 17, 2015
|
This would be great for quick and dirty scripts. It would also make one of the first examples in the rust book a little easier to read:
|
cmr
reviewed
May 22, 2015
|
|
||
| # Detailed design | ||
|
|
||
| Add a new method to the same `impl` block as `Result::unwrap` that takes a `&str` message and |
This comment has been minimized.
This comment has been minimized.
cmr
May 22, 2015
Member
I dislike this signature, and much prefer:
impl<T, U: Debug, V: Display> Foo<T, V> for Result<T, U> {
fn expect(self, msg: V) -> T {
match self {
Ok(v) => v,
Err(e) => panic!("Error: {} ({:?})", msg, e)
}
}
}
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
thepowersgang
May 22, 2015
Author
Contributor
Ooh, interesting idea. This would allow passing format_args! for formatted messages
This comment has been minimized.
This comment has been minimized.
grissiom
commented
May 22, 2015
|
I think |
This comment has been minimized.
This comment has been minimized.
|
@grissiom Possibly, this RFC just brings the avaliable methods into line with Option's methods. |
This comment has been minimized.
This comment has been minimized.
gsingh93
commented
May 22, 2015
|
@grissiom, I completely agree. I think this should at least be discussed before accepting this RFC so we'd only have to deprecate one method, not two, if this were accepted. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
gsingh93
commented
May 24, 2015
|
Yes, but it can be deprecated. |
This comment has been minimized.
This comment has been minimized.
grissiom
commented
May 25, 2015
|
@gsingh93 @thepowersgang OK, when I have time, I will open an other RFC to discuss |
alexcrichton
added
the
final-comment-period
label
Jun 2, 2015
This comment has been minimized.
This comment has been minimized.
|
This RFC is now entering the week-long final comment period. |
This comment has been minimized.
This comment has been minimized.
|
I still REALLY think that this should take |
This comment has been minimized.
This comment has been minimized.
gkoz
commented
Jun 3, 2015
|
I don't think the point about Which of these are more helpful? Maybe this issue should be raised elsewhere though because it's not a problem with |
This comment has been minimized.
This comment has been minimized.
I suspect this is the right way to think about it. For better or worse, |
This comment has been minimized.
This comment has been minimized.
|
The text as written now is a bit unclear. I'd really like it to explicitly provide the signature and implementation (since it should be trivial to provide, and is the entire point of the RFC). |
This comment has been minimized.
This comment has been minimized.
|
@gkoz That should be raised somewhere, probably on the offending error type, making |
This comment has been minimized.
This comment has been minimized.
Agreed. We could change the conventions for error types to have more informative |
This comment has been minimized.
This comment has been minimized.
bluss
commented
Jun 4, 2015
|
Edit: see below. @BurntSushi: I think the cases are different. The Also, we can demand a rarer trait like |
This comment has been minimized.
This comment has been minimized.
gkoz
commented
Jun 4, 2015
|
@bluss but that part of discussion was about the value inside the |
This comment has been minimized.
This comment has been minimized.
bluss
commented
Jun 4, 2015
|
@gkoz Oops! I'm sorry for the noise. |
sfackler
referenced this pull request
Jun 9, 2015
Closed
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os(1455) }' #25638
This comment has been minimized.
This comment has been minimized.
|
The consensus among the libs team is to accept this RFC now. The matter of generalizing the argument of Thanks for the RFC @thepowersgang! |
thepowersgang commentedMay 13, 2015
See also rust-lang/rust#25359 for implementation
Rendered