< prev index next >

test/langtools/tools/javac/ConditionalWithVoid.java

Print this page
rev 56806 : 8232684: Make switch expressions final
Reviewed-by: TBD
   1 /*
   2  * @test /nodynamiccopyright/
   3  * @bug 4974927 8064464
   4  * @summary The compiler was allowing void types in its parsing of conditional expressions.
   5  * @author tball
   6  *
   7  * @compile/fail/ref=ConditionalWithVoid.out --enable-preview -source ${jdk.version} -XDrawDiagnostics ConditionalWithVoid.java
   8  */
   9 public class ConditionalWithVoid {
  10     public void test(Object o, String s) {
  11         // Should fail to compile since Object.wait() has a void return type. Poly case.
  12         System.out.println(o instanceof String ? o.hashCode() : o.wait());
  13         // Should fail to compile since Object.wait() has a void return type. Standalone case.
  14         (o instanceof String ? o.hashCode() : o.wait()).toString();
  15         // Should fail to compile since Object.wait() has a void return type. Poly case.
  16         System.out.println(switch (s) {case "" -> o.hashCode(); default -> o.wait();});
  17         // Should fail to compile since Object.wait() has a void return type. Standalone case.
  18         (switch (s) {case "" -> o.hashCode(); default -> o.wait();}).toString();
  19     }
  20 }
   1 /*
   2  * @test /nodynamiccopyright/
   3  * @bug 4974927 8064464
   4  * @summary The compiler was allowing void types in its parsing of conditional expressions.
   5  * @author tball
   6  *
   7  * @compile/fail/ref=ConditionalWithVoid.out -XDrawDiagnostics ConditionalWithVoid.java
   8  */
   9 public class ConditionalWithVoid {
  10     public void test(Object o, String s) {
  11         // Should fail to compile since Object.wait() has a void return type. Poly case.
  12         System.out.println(o instanceof String ? o.hashCode() : o.wait());
  13         // Should fail to compile since Object.wait() has a void return type. Standalone case.
  14         (o instanceof String ? o.hashCode() : o.wait()).toString();
  15         // Should fail to compile since Object.wait() has a void return type. Poly case.
  16         System.out.println(switch (s) {case "" -> o.hashCode(); default -> o.wait();});
  17         // Should fail to compile since Object.wait() has a void return type. Standalone case.
  18         (switch (s) {case "" -> o.hashCode(); default -> o.wait();}).toString();
  19     }
  20 }
< prev index next >