mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-18 01:54:47 +02:00
8267119: switch expressions lack support for deferred type-checking
Reviewed-by: mcimadamore
This commit is contained in:
parent
4ba761381c
commit
ec8a8097c3
3 changed files with 44 additions and 5 deletions
|
@ -926,6 +926,11 @@ public class DeferredAttr extends JCTree.Visitor {
|
||||||
scan(tree.falsepart);
|
scan(tree.falsepart);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitSwitchExpression(JCSwitchExpression tree) {
|
||||||
|
scan(tree.cases);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitReference(JCMemberReference tree) {
|
public void visitReference(JCMemberReference tree) {
|
||||||
Assert.checkNonNull(tree.getOverloadKind());
|
Assert.checkNonNull(tree.getOverloadKind());
|
||||||
|
@ -1151,7 +1156,7 @@ public class DeferredAttr extends JCTree.Visitor {
|
||||||
static class PolyScanner extends FilterScanner {
|
static class PolyScanner extends FilterScanner {
|
||||||
|
|
||||||
PolyScanner() {
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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: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:42: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:94:31: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.multiple.case.labels), 9, 14
|
||||||
3 errors
|
3 errors
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* @test /nodynamiccopyright/
|
* @test /nodynamiccopyright/
|
||||||
* @bug 8206986 8222169 8224031 8240964
|
* @bug 8206986 8222169 8224031 8240964 8267119
|
||||||
* @summary Check expression switch works.
|
* @summary Check expression switch works.
|
||||||
* @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
|
* @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
|
||||||
* @compile ExpressionSwitch.java
|
* @compile ExpressionSwitch.java
|
||||||
|
@ -34,6 +34,7 @@ public class ExpressionSwitch {
|
||||||
assertEquals(convert2(""), -1);
|
assertEquals(convert2(""), -1);
|
||||||
localClass(T.A);
|
localClass(T.A);
|
||||||
assertEquals(castSwitchExpressions(T.A), "A");
|
assertEquals(castSwitchExpressions(T.A), "A");
|
||||||
|
testTypeInference(true, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String print(T t) {
|
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) {
|
private void check(T t, String expected) {
|
||||||
String result = print(t);
|
String result = print(t);
|
||||||
assertEquals(result, expected);
|
assertEquals(result, expected);
|
||||||
|
@ -162,4 +174,8 @@ public class ExpressionSwitch {
|
||||||
Runnable r = () -> {};
|
Runnable r = () -> {};
|
||||||
r.run();
|
r.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Consumer<Z> {
|
||||||
|
public void consume(Z z);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue