RFR: JDK-8223305: Compiler support for Switch Expressions

Florian Weimer fweimer at redhat.com
Fri May 31 12:26:35 UTC 2019


* Jan Lahoda:

> Hi,
>
> I'd like to ask for a review of changes to update javac to follow the
> current spec for switch expressions, in particular the break -> yield
> change:
> http://cr.openjdk.java.net/~gbierman/jep354-jls-20190524.html

I think the purpose of these rules is to avoid certain forms of
non-local control flow (and avoid specifying whether the loop control
expression(s) are part of the loop or not):

| It is a compile-time error if the break target of a break statement
| contains any method, constructor, initializer, lambda expression, or
| switch expression that encloses the break statement.

| It is a compile-time error if the continue target contains any method,
| constructor, initializer, lambda expression, or switch expression that
| contains the continue statement.

| It is a compile-time error if the return target contains any
| initializer, or switch expression that encloses the return statement.

Are these rules really technically correct?  Maybe it's more obvious to
define the target in such a way that it must be located within the same
method, constructor, or initializer.  I think then it's possible to
require that the break/continue/return target must not contain any
expression that also contains the break/continue/return statement.  (And
also define that do/for/switch/while statements contain both the body
and the expression(s).)

For a specification point of view, I think it might be more
straightforward to specify statement expressions first and then use them
to define switch expressions.

Thanks,
Florian


More information about the compiler-dev mailing list