-
Bug
-
Resolution: Fixed
-
P3
-
21, 22
-
b27
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8323205 | 21.0.3-oracle | Angelos Bimpoudis | P3 | Resolved | Fixed | b02 |
JDK-8323176 | 21.0.3 | Aleksey Shipilev | P3 | Resolved | Fixed | b01 |
ADDITIONAL SYSTEM INFORMATION :
javac 22-ea, javac 21.0.1
A DESCRIPTION OF THE PROBLEM :
This does not compile:
{code:java}
public class WithFinal {
record ARecord(String aComponent) {}
public String match(Object o) {
return switch(o) {
case ARecord(final String s) -> s;
default -> "No match";
};
}
}
{code}
javac outputs the following errors:
{noformat}
./WithFinal.java:6: error: illegal start of expression
case ARecord(final String s) -> s;
^
./WithFinal.java:6: error: ';' expected
case ARecord(final String s) -> s;
^
./WithFinal.java:6: error: not a statement
case ARecord(final String s) -> s;
^
3 errors
{noformat}
According to chapter 14.30.1. of the JLS, {{final}} is allowed as a _VariableModifier_. Without {{final}}, mutating the pattern variable is valid:
{code:java}
public class NotEffectivelyFinal {
record ARecord(String aComponent) {}
public String match(Object o) {
return switch(o) {
case ARecord(String s) -> {
s = "";
yield s;
}
default -> "No match";
};
}
}
{code}
javac 22-ea, javac 21.0.1
A DESCRIPTION OF THE PROBLEM :
This does not compile:
{code:java}
public class WithFinal {
record ARecord(String aComponent) {}
public String match(Object o) {
return switch(o) {
case ARecord(final String s) -> s;
default -> "No match";
};
}
}
{code}
javac outputs the following errors:
{noformat}
./WithFinal.java:6: error: illegal start of expression
case ARecord(final String s) -> s;
^
./WithFinal.java:6: error: ';' expected
case ARecord(final String s) -> s;
^
./WithFinal.java:6: error: not a statement
case ARecord(final String s) -> s;
^
3 errors
{noformat}
According to chapter 14.30.1. of the JLS, {{final}} is allowed as a _VariableModifier_. Without {{final}}, mutating the pattern variable is valid:
{code:java}
public class NotEffectivelyFinal {
record ARecord(String aComponent) {}
public String match(Object o) {
return switch(o) {
case ARecord(String s) -> {
s = "";
yield s;
}
default -> "No match";
};
}
}
{code}
- backported by
-
JDK-8323176 Compiler should accept final variable in Record Pattern
- Resolved
-
JDK-8323205 Compiler should accept final variable in Record Pattern
- Resolved
- links to
-
Commit openjdk/jdk21u-dev/a85f3f83
-
Commit openjdk/jdk/4ba94ef6
-
Review openjdk/jdk21u-dev/125
-
Review openjdk/jdk/16899
(1 links to)