Integrated: 8281100: Spurious "variable might not have been initialized" with sealed class switch

Jan Lahoda jlahoda at openjdk.java.net
Fri Feb 11 12:15:13 UTC 2022


On Wed, 9 Feb 2022 09:40:21 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider:
> 
> interface I permits A, B {}
> 
> String data;
> I i = ...;
> switch (i) {
>     case A a -> data = 0;
>     case B b -> data = 0;
> }
> System.err.println(data); //incorrect error here
> 
> 
> The specification says that `data` is definitely assigned as the `System.err.println`, as the switch covers `I`, but javac will report an error that the variable may not be assigned.
> 
> This patch fixes that - the only tricky part is that if there's an error in the switch (like when the switch is expected to be exhaustive, but is not), the current patch will try to suppress the follow-up "variable may not be assigned" errors. As an example:
> 
> interface I permits A, B {}
> 
> String data;
> I i = ...;
> switch (i) { //error here, not exhaustive
>     case A a -> data = 0;
> }
> System.err.println(data); //no error here, due to the preceding exhaustiveness error

This pull request has now been integrated.

Changeset: 4ff5824f
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.java.net/jdk/commit/4ff5824f5bc13826d2eae1c83094acfcccdb7b8f
Stats:     107 lines in 4 files changed: 90 ins; 3 del; 14 mod

8281100: Spurious "variable might not have been initialized" with sealed class switch

Reviewed-by: vromero

-------------

PR: https://git.openjdk.java.net/jdk/pull/7402


More information about the compiler-dev mailing list