roc: Remove all uses of `panic!`, `unwrap()`, `expect(...)`, and `unreachable!()`
We want to use our new error macros instead of panic!, unwrap(), expect(...), and unreachable!().
The new error macros both live in roc_reporting and can be imported with use roc_reporting:{internal_error, user_error}.
Essentially, all cases were the panic!/unwrap()/expect(...) would be a sign of a compiler bug, we want to use internal_error! instead. user_error! should be used in cases where we eventually want to use roc_reporting to print a pretty error message. It is essentially documentation that eventually better errors should be return there. todo! should be used for cases were the feature is planned to be implemented but is not yet implemented. unimplemented! should be used if the feature is not implemented, and there is not a current plan to implement the feature.
Uses of unreachable!() should always be replaced with internal_error!. If possible, a message around why the statement is unreachable/what invariant was broken, should be added.
This issue will be fixed when we can enable clippy::panic, clippy::unwrap_used, clippy::expect_used, and clippy::unreachable on CI by default.
This command can be used to track how many use cases are left:
cargo clean && cargo clippy --no-deps -- -W clippy::unwrap_used -W clippy::expect_used -W clippy::panic -W clippy::unreachable 2> >(grep -e "warning: \`panic\`" -e "warning: used" -e "warning: usage of" | wc -l )
At the date of creating this issue, we have 1283 cases to go.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (11 by maintainers)
Commits related to this issue
- replace unreachable! with internal_error! in expr canonicalization See https://github.com/roc-lang/roc/issues/2046 — committed to mulias/roc by mulias 3 years ago
- replace unreachable! with internal_error! in expr canonicalization See https://github.com/roc-lang/roc/issues/2046 — committed to mulias/roc by mulias 3 years ago
- replace unreachable! with internal_error! in expr canonicalization See https://github.com/roc-lang/roc/issues/2046 — committed to mulias/roc by mulias 3 years ago
- replace unreachable! with internal_error! in expr canonicalization See https://github.com/roc-lang/roc/issues/2046 — committed to mulias/roc by mulias 3 years ago
Could this be simpler: we can execute the count command (
$ cargo clean && cargo clippy --no-deps -- -W clippy::unwrap_used ... )) in CI and error if it has increased.If we use
#[ignore(clippy::panic)]in the code, people may be tempted to just copy thatProgress report
Correct! Clippy was used in https://github.com/roc-lang/roc/pull/7221 😃
I like that idea, Anton!
We could also have a script which errors if the number decreases, so you can make sure to decrease the count.
So the error messages could be:
Also, it’s a bit unfortunately, but things like
unwrapare fine (and useful for conciseness) in tests - butgrepdoesn’t know about the distinction between test code and production code. We could make something fancier to detect that distinction in the future perhaps.As a quick note, this should definitely be the preferred solution (even though it takes longer) whenever possible! A lot of the worst user experiences in Roc right now are due to compiler panics (e.g. because they block later steps in the compiler, such as error reporting, from running as normal), so anywhere we can replace a panic with telling the user what went wrong and then continuing, that’s what we should do!
I think these would be the least likely to have conflicts right now:
…and these would be more likely to have conflicts:
I’d also avoid anything in the
vendor/directory since those are all forks of libraries that we tweaked for our own needs 😄As an extra notes, in many cases, it may also be fine to replace
panic!,unwrap(), andexpect(...)with a?or returningNone/Err(...). These are ways of propagating the error up to the calling function. This should be done whenever it is reasonable for the calling function to expect and handle an option or result. If the calling function couldn’t cleanly handle and resolve the issues, please stick to other solutions.