RFR: JDK-8214529: Exception while using Anonymous class in switch expression
Jan Lahoda
jan.lahoda at oracle.com
Mon Dec 3 12:37:38 UTC 2018
Hi,
In:
https://bugs.openjdk.java.net/browse/JDK-8214529
This is a report that anonymous class' bodies are lost if the anonymous
class is the break value. This was due to a mistake in desugaring, and
has been fixed alongside with JDK-8214031, which removes the desugaring.
But when writing a test for this case, it turned out that "return"
statements inside the anonymous class are reported as errors:
---
interface TestIntf {
default int fun() {
return 1;
}
}
public class SwitchTest {
public static void main(String[] args) {
int x = 10;
var y = switch(x) {
case 0 -> new TestIntf(){ public int fun() { return 0; }};
default -> new TestIntf(){};
};
System.out.println(y.fun());
}
}
---
---
$ javac --enable-preview --source 12 /tmp/SwitchTest.java
/tmp/SwitchTest.java:12: error: return outside of enclosing switch
expression
case 0 -> new TestIntf(){ public int fun() { return 0; }};
^
Note: /tmp/SwitchTest.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
1 error
---
This is not appropriate, to my knowledge. The proposed fix is to clear
the breakResult when creating methodEnv, as is already done for
lambdaEnv. The breakResult is (also) used to detect inappropriate
returns nested inside the switch expression, so clearing it eliminates
the error.
Webrev:
http://cr.openjdk.java.net/~jlahoda/8214529/webrev.00/
How does this look?
Thanks,
Jan
More information about the compiler-dev
mailing list