OpenJDK 14.0.1 gives "the switch expression does not cover all possible input values"
Asked Answered
H

1

10

Using OpenJDK 14.0.1

public class Example {
    private String test(final ExampleEnum ee) {
        return switch (ee) {
            case Value -> null;
        };
    }
}
public enum ExampleEnum {

    Value;

    public enum InnerEnum {
    }

}

Compilation fails with "the switch expression does not cover all possible input values". If I remove InnerEnum from ExampleEnum the code compiles. Why does the presence of this inner enum cause the switch expression to fail? Is there a logical explanation or a compiler bug?

Hostile answered 2/5, 2020 at 18:56 Comment(2)
You might want to check bugs.openjdk.java.net/browse/JDK-8243548Horripilation
@Horripilation that appears to be the same and confirms that this is a compiler bug. Thanks!Hostile
R
6

You need add default case, like this:

public class Example {
private String test(final ExampleEnum ee) {
    return switch (ee) {
        case Value -> null;
        default -> throw new IllegalStateException("Unexpected value");
    };
}
Remediable answered 19/5, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.