Skip to content

Commit

Permalink
8314423: Multiple patterns without unnamed variables
Browse files Browse the repository at this point in the history
8314216: Case enumConstant, pattern compilation fails

Reviewed-by: jlahoda
  • Loading branch information
biboudis authored and lahodaj committed Aug 17, 2023
1 parent 249dc37 commit 4331193
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -4609,6 +4609,19 @@ void checkSwitchCaseStructure(List<JCCase> cases) {
if (!allUnderscore) {
log.error(c.labels.tail.head.pos(), Errors.FlowsThroughFromPattern);
}

boolean allPatternCaseLabels = c.labels.stream().allMatch(p -> p instanceof JCPatternCaseLabel);

if (allPatternCaseLabels) {
preview.checkSourceLevel(c.labels.tail.head.pos(), Feature.UNNAMED_VARIABLES);
}

for (JCCaseLabel label : c.labels.tail) {
if (label instanceof JCConstantCaseLabel) {
log.error(label.pos(), Errors.InvalidCaseLabelCombination);
break;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,8 @@ PatternResult analyzePattern(int lookahead) {
} else {
pendingResult = PatternResult.PATTERN;
}
} else if (typeDepth == 0 && parenDepth == 0 && (peekToken(lookahead, tk -> tk == ARROW || tk == COMMA))) {
return PatternResult.EXPRESSION;
}
break;
case UNDERSCORE:
Expand Down
18 changes: 18 additions & 0 deletions test/langtools/tools/javac/T8314216.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @test /nodynamiccopyright/
* @bug 8314216
* @summary Multiple patterns without unnamed variables
* @compile/fail/ref=T8314216.out -XDrawDiagnostics --enable-preview --source ${jdk.version} T8314216.java
*/

public class T8314216 {
enum X {A, B}

void test(Object obj) {
switch (obj) {
case X.A, Integer _ -> System.out.println("A or Integer");
case String _, X.B -> System.out.println("B or String");
default -> System.out.println("other");
}
}
}
5 changes: 5 additions & 0 deletions test/langtools/tools/javac/T8314216.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T8314216.java:13:23: compiler.err.invalid.case.label.combination
T8314216.java:14:28: compiler.err.invalid.case.label.combination
- compiler.note.preview.filename: T8314216.java, DEFAULT
- compiler.note.preview.recompile
2 errors
23 changes: 23 additions & 0 deletions test/langtools/tools/javac/T8314423.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* @test /nodynamiccopyright/
* @bug 8314423
* @summary Multiple patterns without unnamed variables
* @compile/fail/ref=T8314423.out -XDrawDiagnostics T8314423.java
* @compile --enable-preview --source ${jdk.version} T8314423.java
*/

public class T8314423 {
record R1() {}
record R2() {}

static void test(Object obj) {
switch (obj) {
case R1(), R2() -> System.out.println("R1 or R2");
default -> System.out.println("other");
}
}

public static void main(String[] args) {
test(new R1());
}
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8314423.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
T8314423.java:15:24: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.unnamed.variables)
1 error

1 comment on commit 4331193

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.