Skip to content

Commit

Permalink
8267119: switch expressions lack support for deferred type-checking
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
lahodaj committed May 21, 2021
1 parent 4ba7613 commit ec8a809
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Expand Up @@ -926,6 +926,11 @@ public void visitConditional(JCTree.JCConditional tree) {
scan(tree.falsepart);
}

@Override
public void visitSwitchExpression(JCSwitchExpression tree) {
scan(tree.cases);
}

@Override
public void visitReference(JCMemberReference tree) {
Assert.checkNonNull(tree.getOverloadKind());
Expand Down Expand Up @@ -1151,7 +1156,7 @@ void skip(JCTree tree) {}
static class PolyScanner extends FilterScanner {

PolyScanner() {
super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE, SWITCH_EXPRESSION));
}
}

Expand Down Expand Up @@ -1287,6 +1292,24 @@ public void visitReturn(JCReturn tree) {
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);
}

}

/**
Expand Down
@@ -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
18 changes: 17 additions & 1 deletion test/langtools/tools/javac/switchexpr/ExpressionSwitch.java
@@ -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
Expand Down Expand Up @@ -34,6 +34,7 @@ private void run() {
assertEquals(convert2(""), -1);
localClass(T.A);
assertEquals(castSwitchExpressions(T.A), "A");
testTypeInference(true, 0);
}

private String print(T t) {
Expand Down Expand Up @@ -144,6 +145,17 @@ private String castSwitchExpressions(T t) {
};
}

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);
Expand All @@ -162,4 +174,8 @@ void t() {
Runnable r = () -> {};
r.run();
}

interface Consumer<Z> {
public void consume(Z z);
}
}

1 comment on commit ec8a809

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.