[JEP 325 Switch Expression] switch labeled block completion issue?

Manoj Palat manojnp.java at gmail.com
Wed Nov 28 04:19:16 UTC 2018


Hi Joe, all,
The following program compiles without errors.

package se2;

import java.io.IOException;
public class X {
public static int twice(int i) throws IOException {
int k = switch (i) {
case 1 -> {
System.out.println("hello");
// break 1;
  }
case 2 -> {
System.out.println("world");
// break 2;
  }
  default ->  10;
};
return k;
}
public static void main(String[] args) {
try {
System.out.println(X.twice(1));
} catch (IOException e) {
//do nothing;
}
}
}

$ javac12 --enable-preview -source 12 se2/X.java
Note: se2\X.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

$ java12 --enable-preview se2/X
hello
world
10

Other details:
$ uname -a
CYGWIN_NT-6.1 XXXXX 2.9.0(0.318/5/3) 2017-09-12 10:18 x86_64 Cygwin

The output of the program is given above – essentially a fall-through akin
to a switch statement’s expected behavior.
However, From JLS 12 15.29.1,* “Given a switch expression, if the switch
block consists of switch labeled rules, then it is a compile-time error if
any switch labeled block can complete normally. If, on the other hand, the
switch block consists of switch labeled statement groups, then it is a
compile-time error if either the last statement in the switch block can
complete normally, or the switch block includes one or more switch labels
at the end.” *

>From the above I expected a compilation error to be shown since the case 1
and case 2 arms can complete normally without a break expression.

Is this an error in Javac or is it a wrong interpretation of "complete
normally" in the spec on my part?
 (Once the break expressions are uncommented, the switch expression returns
the appropriate  values which is of course the correct behavior.) I am
using ea 20 for this.

Regards,
Manoj Palat
Eclipse Java Dev,
IBM.

PS: I had sent this mail yesterday from my IBM official id, but I don;t see
that mail in the mailing list yet - hence resending from personal id.

From: "Joseph D. Darcy" <joe.darcy at oracle.com>
To: jdk-dev <jdk-dev at openjdk.java.net>
Date: 11/27/2018 06:29 AM
Subject: Reminder: submit CSRs for JDK 12 with sufficient time ahead of
rampdown 1 milestone
Sent by: "jdk-dev" <jdk-dev-bounces at openjdk.java.net>



Hello,

As a reminder, per the JDK 12 schedule
(
https://urldefense.proofpoint.com/v2/url?u=http-3A__openjdk.java.net_projects_jdk_12_&d=DwICaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=NBCxwvEUF_6bxl1FYxFdnHSL7MKL5Ya8ynDOXXPMdM4&s=nQVLCHjGW7vanaEOQjwFI3s_yk30KByjLdaV09RMXlA&e=)
rampdown phase 1 of the
release is coming up in a few weeks on Dec. 13, 2018.

If you are planning to get a change into JDK 12 before the rampdown 1
milestone and the change requires a CSR, the CSR needs to be an the
Approved state before the change is pushed. The nominal review time of
the CSR is one week. Note this is the time to review, not necessarily
the time to approve. As with code reviews, the CSR process can provide
feedback which has to be acted upon before a request is approved.

Please plan accordingly; cheers,

-Joe
CSR Lead


More information about the jdk-dev mailing list