Skip to content

8347291: Exhaustive switch over a generic sealed abstract class #23286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4238,7 +4238,7 @@ public void visitRecordPattern(JCRecordPattern tree) {
Type type = attribType(tree.deconstructor, env);
if (type.isRaw() && type.tsym.getTypeParameters().nonEmpty()) {
Type inferred = infer.instantiatePatternType(resultInfo.pt, type.tsym);
if (inferred == null) {
if (inferred == null || inferred.isErroneous()) {
log.error(tree.pos(), Errors.PatternTypeCannotInfer);
} else {
type = inferred;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ private Set<PatternDescription> reduceBindingPatterns(Type selectorType, Set<Pat
} else {
instantiated = infer.instantiatePatternType(selectorType, csym);
}

return instantiated != null && types.isCastable(selectorType, instantiated);
return instantiated != null &&
(instantiated.isErroneous() || types.isCastable(selectorType, instantiated));
});

for (PatternDescription pdOther : patterns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ public Type instantiatePatternType(Type expressionType, TypeSymbol patternTypeSy
//step 4:
return types.upward(substituted, freshVars);
} catch (Infer.InferenceException ex) {
return null;
return types.createErrorType(expressionType);
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/langtools/tools/javac/patterns/Exhaustiveness.java
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,36 @@ static <T1 extends A, T2 extends Abs & B> int r(R<T1, T2> r) {
""");
}

@Test
public void testNonExhaustiveCapture(Path base) throws Exception {
doTest(base,
new String[]{"""
package lib;
public sealed interface S<T extends S<T>> permits A, B {}
""",
"""
package lib;
public final class A implements S<A> {}
""",
"""
package lib;
public final class B<T extends B<T>> implements S<T> {}
"""},
"""
package test;
import lib.*;
public class Test {
public static void test(S<?> sealed) {
switch (sealed) {
case A one -> {}
}
}
}
""",
"Test.java:5:5: compiler.err.not.exhaustive.statement",
"1 error");
}

private void doTest(Path base, String[] libraryCode, String testCode, String... expectedErrors) throws IOException {
doTest(base, libraryCode, testCode, false, expectedErrors);
}
Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/patterns/InferenceUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void checkInferedType(String base, String test, String expected) {
Type baseType = parseType(base);
TypeSymbol testType = parseType(test).tsym;
Type actualType = infer.instantiatePatternType(baseType, testType);
String actualTypeString = actualType != null ? actualType.toString() : null;
String actualTypeString = actualType != null && !actualType.isErroneous() ? actualType.toString() : null;
if (!Objects.equals(expected, actualTypeString)) {
error("Unexpected type, expected: " + expected + ", got: " + actualTypeString);
}
Expand Down