Allow Assertions.UNREACHABLE(...) in expressions#1998
Open
liblit wants to merge 1 commit into
Open
Conversation
Previously, the various overloads of `Assertions.UNREACHABLE(...)` returned `void`. Therefore, calls to these methods could be used as statements but not expressions. That restriction led to many code fragments like the following: ```java Assertions.UNREACHABLE(); return null; ``` An unfortunate side effect of that style was to make many methods appear to return nullable values even though the `return null` statement could never be reached. Now the `Asserions.UNREACHABLE(...)` methods statically claim to return a value of any arbitrary type. In reality, they will never return at all; each such method always throws an exception. However, by _pretending_ to return an arbitrarily typed value, we can now replace the above code pattern with: ```java return Assertions.UNREACHABLE(); ``` This change should help us recognize that some methods can never return `null`, even though they contain unreachable code. This change was inspired by several standard Kotlin features. The `TODO()`, `TODO(String)`, and `error(Any)` functions all return type `Nothing`, which is a subtype of every other type. Kotlin's `throw ...` construct is also an expression (not a statement) with static type `Nothing`. Also add JetBrains `@Contract` annotations to `Asserions.UNREACHABLE(...)` methods declaring that they always "fail" (i.e., throw exceptions) regardless of the arguments passed to them. Some static analysis tools may benefit from this information.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #1998 +/- ##
============================================
+ Coverage 50.51% 50.69% +0.17%
- Complexity 12695 12697 +2
============================================
Files 1368 1368
Lines 83799 83506 -293
Branches 14419 14419
============================================
- Hits 42333 42331 -2
+ Misses 36772 36478 -294
- Partials 4694 4697 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, the various overloads of
Assertions.UNREACHABLE(...)returnedvoid. Therefore, calls to these methods could be used as statements but not expressions. That restriction led to many code fragments like the following:An unfortunate side effect of that style was to make many methods appear to return nullable values even though the
return nullstatement could never be reached.Now the
Asserions.UNREACHABLE(...)methods statically claim to return a value of any arbitrary type. In reality, they will never return at all; each such method always throws an exception. However, by pretending to return an arbitrarily typed value, we can now replace the above code pattern with:This change should help us recognize that some methods can never return
null, even though they contain unreachable code.This change was inspired by several standard Kotlin features. The
TODO(),TODO(String), anderror(Any)functions all return typeNothing, which is a subtype of every other type. Kotlin'sthrow ...construct is also an expression (not a statement) with static typeNothing.Also add JetBrains
@Contractannotations toAsserions.UNREACHABLE(...)methods declaring that they always "fail" (i.e., throw exceptions) regardless of the arguments passed to them. Some static analysis tools may benefit from this information.