Skip to content

Panicking when an Option is unwrapped should give a useful error message #30429

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

Closed
jimmycuadra opened this issue Dec 17, 2015 · 6 comments
Closed
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jimmycuadra
Copy link
Contributor

Given the Rust program:

fn main() {
    let option: Option<bool> = None;
    option.unwrap();
}

Running the program outputs:

thread '<main>' panicked at 'called `Option::unwrap()` on a `None` value', ../src/libcore/option.rs:365

The same program using expect instead of unwrap outputs:

thread '<main>' panicked at 'oops', ../src/libcore/option.rs:333

In a program of non-trivial size, figuring out which call to unwrap or expect generated the panic is harder than it needs to be, since the line shown is where the panic actually happens inside libcore. For improved ergonomics, the file and line reported should be from the user's source code where the unwrap or expect was called.

@frewsxcv
Copy link
Member

How should the compiler know which stack frame to display in the error message when a panic happens?

@sfackler
Copy link
Member

I am also unsure through what mechanism this would be implemented. Even stack crawling's going to fail without debug info and give bad results if functions have been inlined.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 17, 2015

you can create a macro that reports the proper error file and line: http://is.gd/qhd6dt

this can probably be generalized by creating an Unwrappable trait and implementing it for Option and Result

@durka
Copy link
Contributor

durka commented Dec 17, 2015

In case you don't know, setting RUST_BACKTRACE=1 in the environment will print a full stack trace on panics. Otherwise, yeah, this is why expect is preferred to unwrap.

@jimmycuadra
Copy link
Contributor Author

I know about RUST_BACKTRACE. My concern here is about the ergonomics of dealing with errors for Rust developers—especially newcomers. Having to change unwraps to expects (which are often in upstream code and not modified as trivially) or to figure out the right arcane incantation of cargo or rustc to get output that is useful enough to proceed seems like it could be improved.

Similar/related issues:

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 24, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 15, 2017

This is going to need an RFC. Fortunately there already is one in rust-lang/rfcs#2091, and it seems to have passed FCP! I don't think we need this issue anymore, we can follow up there.

@dtolnay dtolnay closed this as completed Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants