8267119: switch expressions lack support for deferred type-checking

Reviewed-by: mcimadamore
This commit is contained in:
Jan Lahoda 2021-05-21 14:09:42 +00:00
parent 4ba761381c
commit ec8a8097c3
3 changed files with 44 additions and 5 deletions

View file

@ -926,6 +926,11 @@ public class DeferredAttr extends JCTree.Visitor {
scan(tree.falsepart);
}
@Override
public void visitSwitchExpression(JCSwitchExpression tree) {
scan(tree.cases);
}
@Override
public void visitReference(JCMemberReference tree) {
Assert.checkNonNull(tree.getOverloadKind());
@ -1151,7 +1156,7 @@ public class DeferredAttr extends JCTree.Visitor {
static class PolyScanner extends FilterScanner {
PolyScanner() {
super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE, SWITCH_EXPRESSION));
}
}
@ -1287,6 +1292,24 @@ public class DeferredAttr extends JCTree.Visitor {
lambdaScanner.scan(lambda.body);
}
}
@Override
public void visitSwitchExpression(JCSwitchExpression expr) {
SwitchExpressionScanner switchScanner = new SwitchExpressionScanner() {
@Override
public void visitYield(JCYield tree) {
Type prevPt = CheckStuckPolicy.this.pt;
try {
CheckStuckPolicy.this.pt = pt;
CheckStuckPolicy.this.scan(tree.value);
} finally {
CheckStuckPolicy.this.pt = prevPt;
}
}
};
switchScanner.scan(expr.cases);
}
}
/**

View file

@ -1,4 +1,4 @@
ExpressionSwitch.java:40:16: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.switch.expressions), 9, 14
ExpressionSwitch.java:41:20: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.switch.rules), 9, 14
ExpressionSwitch.java:93:31: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.multiple.case.labels), 9, 14
ExpressionSwitch.java:41:16: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.switch.expressions), 9, 14
ExpressionSwitch.java:42:20: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.switch.rules), 9, 14
ExpressionSwitch.java:94:31: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.multiple.case.labels), 9, 14
3 errors

View file

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8206986 8222169 8224031 8240964
* @bug 8206986 8222169 8224031 8240964 8267119
* @summary Check expression switch works.
* @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
* @compile ExpressionSwitch.java
@ -34,6 +34,7 @@ public class ExpressionSwitch {
assertEquals(convert2(""), -1);
localClass(T.A);
assertEquals(castSwitchExpressions(T.A), "A");
testTypeInference(true, 0);
}
private String print(T t) {
@ -144,6 +145,17 @@ public class ExpressionSwitch {
};
}
private void testTypeInference(boolean b, int i) {
m(s -> s.length(), String.class);
m(b ? s -> s.length() : s -> s.length(), String.class);
m(switch (i) {
case 0 -> s -> s.length();
default -> s -> s.length();
}, String.class);
}
<Z> void m(Consumer<Z> c, Class<Z> cl) {}
private void check(T t, String expected) {
String result = print(t);
assertEquals(result, expected);
@ -162,4 +174,8 @@ public class ExpressionSwitch {
Runnable r = () -> {};
r.run();
}
interface Consumer<Z> {
public void consume(Z z);
}
}