Skip to content

Add an optional string parameter to the unreachable!() macro to communicate which invariant was violated #18842

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
bstrie opened this issue Nov 10, 2014 · 1 comment · Fixed by #18867
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority

Comments

@bstrie
Copy link
Contributor

bstrie commented Nov 10, 2014

In #18287 we had the submitter change a panic!() to an unreachable!() as follows:

_ => panic!("Invalid SearchStack.")
_ => unreachable!()

We felt that unreachable!() better communicated the intent of "this code should never panic assuming that the implementation is correct" (as opposed to panic!(), which communicates "this code should never panic assuming that the user passes in the correct inputs" (cf. indexing, division) and is generally to be avoided in the stdlib).

However, I think it's a shame that by expressing the intent more clearly in the code we're simultaneously losing information regarding which invariant was violated in order to reach the unreachable code.

Note that this isn't really a huge deal, because we can just move the "Invalid SearchStack" bit over into a comment next to unreachable!(). It's also not really a huge deal for the theoretical bug reporter who's filing an issue upon hitting this code, since unreachable!() still includes a filename and line number in its output.

However, given the precedent of optional messages set by panic!(), I don't think it would be a stretch to imagine that unreachable!() could also have an optional message. Usage for the typical case would be the same:

unreachable!();  // task '<main>' panicked at 'internal error: entered unreachable code', unreachable.rs:2

...but could optionally look like this:

unreachable!("bamboozled the frobnob");  // task '<main>' panicked at 'internal error: entered unreachable code: bamboozled the frobnob', unreachable.rs:2
@bstrie bstrie changed the title Add an option string parameter to the unreachable!() macro to communicate which invariant was violated Add an optional string parameter to the unreachable!() macro to communicate which invariant was violated Nov 10, 2014
@bstrie bstrie added P-low Low priority E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Nov 10, 2014
@michaelsproul
Copy link
Contributor

I'll have a go at implementing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants