File tree 3 files changed +52
-3
lines changed
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns
3 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -4650,11 +4650,11 @@ void checkSwitchCaseStructure(List<JCCase> cases) {
4650
4650
if (previousCompletessNormally &&
4651
4651
c .stats .nonEmpty () &&
4652
4652
c .labels .head instanceof JCPatternCaseLabel patternLabel &&
4653
- hasBindings (patternLabel .pat )) {
4653
+ ( hasBindings (patternLabel .pat ) || hasBindings ( c . guard ) )) {
4654
4654
log .error (c .labels .head .pos (), Errors .FlowsThroughToPattern );
4655
4655
} else if (c .stats .isEmpty () &&
4656
4656
c .labels .head instanceof JCPatternCaseLabel patternLabel &&
4657
- hasBindings (patternLabel .pat ) &&
4657
+ ( hasBindings (patternLabel .pat ) || hasBindings ( c . guard ) ) &&
4658
4658
hasStatements (l .tail )) {
4659
4659
log .error (c .labels .head .pos (), Errors .FlowsThroughFromPattern );
4660
4660
}
@@ -4663,7 +4663,7 @@ void checkSwitchCaseStructure(List<JCCase> cases) {
4663
4663
}
4664
4664
}
4665
4665
4666
- boolean hasBindings (JCPattern p ) {
4666
+ boolean hasBindings (JCTree p ) {
4667
4667
boolean [] bindings = new boolean [1 ];
4668
4668
4669
4669
new TreeScanner () {
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @test /nodynamiccopyright/
3
+ * @bug 8314578
4
+ * @enablePreview
5
+ * @summary Parsing of erroneous patterns succeeds
6
+ * @compile/fail/ref=T8314578.out -XDrawDiagnostics T8314578.java
7
+ */
8
+ public class T8314578 {
9
+ record R1 () {}
10
+ record R2 () {}
11
+
12
+ static void test (Object o ) {
13
+ switch (o ) {
14
+ case R1 () when o instanceof String s :
15
+ case R2 () when o instanceof Integer i :
16
+ System .out .println ("hello: " + i );
17
+ break ;
18
+ default :
19
+ break ;
20
+ }
21
+ }
22
+
23
+ static void test2 (Object o ) {
24
+ switch (o ) {
25
+ case R1 () when o instanceof String s :
26
+ System .out .println ("hello: " + s );
27
+ case R2 () when o instanceof Integer i :
28
+ System .out .println ("hello: " + i );
29
+ break ;
30
+ default :
31
+ break ;
32
+ }
33
+ }
34
+
35
+ static int unnamedInGuardsOK (String s ) {
36
+ return switch (s ) {
37
+ case String _ when s instanceof String _ -> // should be OK
38
+ 1 ;
39
+ default ->
40
+ -1 ;
41
+ };
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ T8314578.java:14:18: compiler.err.flows.through.from.pattern
2
+ T8314578.java:15:18: compiler.err.flows.through.to.pattern
3
+ T8314578.java:27:18: compiler.err.flows.through.to.pattern
4
+ - compiler.note.preview.filename: T8314578.java, DEFAULT
5
+ - compiler.note.preview.recompile
6
+ 3 errors
You can’t perform that action at this time.
0 commit comments