8366968: Exhaustive switch expression rejected by for not covering all possible values #27547
+67
−7
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.
Consider case like (from the bug):
This fails to compile:
Note there is no instance of
Special
that would not be an instance ofValue
as well. I.e. coveringValue
will catch all input.Also, note that if
case Value
is replaced withcase SpecialValue
, javac also compile the code:Which shows the problem: replacing a type with a super type should not cause the switch to stop to be exhaustive (i.e. the switch is exhaustive for
SpecialValue
, but replacing it with its super typeValue
, the switch is no longer exhaustive for javac).javac contains a piece of code that searches through subtypes to handle diamond class hierarchies like the one above. But, when it searches for subtypes, it does a search through permitted subtypes of the type. And since
Value
is not sealed, this search will not findSpecialValue
, and javac won't see the switch to be exhaustive.The proposal herein is to broaden the search, and consider all transitive permitted subtypes of the selector type, and filter subtypes of the current type from this set (if the current type is abstract, if it is not abstract, we can't do the subtype search at all). This should, I think, include all relevant subtypes.
Progress
Integration blocker
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27547/head:pull/27547
$ git checkout pull/27547
Update a local copy of the PR:
$ git checkout pull/27547
$ git pull https://git.openjdk.org/jdk.git pull/27547/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 27547
View PR using the GUI difftool:
$ git pr show -t 27547
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27547.diff
Using Webrev
Link to Webrev Comment