Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8269455 | 18 | Robo Duke | P3 | Resolved | Fixed | b04 |
JDK-8270642 | 17.0.1 | Robo Duke | P3 | Resolved | Fixed | b03 |
I try the following code when solving the JDK-8267610.
```
class Main {
String test(Object o) {
if (o instanceof (CharSequence cs && cs instanceof String s)){
return s;
}
return null;
}
}
```
The javac compiles successfully and generates the following bytecode.
```
java.lang.String test(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/String;
flags: (0x0000)
Code:
stack=1, locals=4, args_size=2
0: aload_1
1: instanceof #7 // class java/lang/CharSequence
4: ifeq 26
7: aload_1
8: checkcast #7 // class java/lang/CharSequence
11: astore_2
12: aload_1 // <---------- should be `aload_2`
13: instanceof #9 // class java/lang/String
16: ifeq 26
19: aload_1 // <---------- should be `aload_2`
20: checkcast #9 // class java/lang/String
23: astore_3
24: aload_3
25: areturn
26: aconst_null
27: areturn
```
There are two wrong places where I mark "// <---------- should be `aload_2`".
After fixingJDK-8267610 locally, javac generates the following byte code:
```
java.lang.String test(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/String;
flags: (0x0000)
Code:
stack=1, locals=4, args_size=2
0: aload_1
1: instanceof #7 // class java/lang/CharSequence
4: ifeq 26
7: aload_1
8: checkcast #7 // class java/lang/CharSequence
11: astore_2
12: aload_2
13: instanceof #9 // class java/lang/String
16: ifeq 26
19: aload_1 // <---------- should be `aload_2`
20: checkcast #9 // class java/lang/String
23: astore_3
24: aload_3
25: areturn
26: aconst_null
27: areturn
```
One wrong byte code is also generated.
BothJDK-8267610 and this issue are caused by the BindingPattern and BindingSymbol.
During desugar, especially TransPatterns, we shouldn't make a tree which contains JCBindingPattern or BindingSymbol. So after desugar, the whole AST shouldn't contain JCBindingPattern or BindingSymbol anymore.
If not, the tree is wrong and the javac may crash or generate wrong bytecode unexpectedly.
```
class Main {
String test(Object o) {
if (o instanceof (CharSequence cs && cs instanceof String s)){
return s;
}
return null;
}
}
```
The javac compiles successfully and generates the following bytecode.
```
java.lang.String test(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/String;
flags: (0x0000)
Code:
stack=1, locals=4, args_size=2
0: aload_1
1: instanceof #7 // class java/lang/CharSequence
4: ifeq 26
7: aload_1
8: checkcast #7 // class java/lang/CharSequence
11: astore_2
12: aload_1 // <---------- should be `aload_2`
13: instanceof #9 // class java/lang/String
16: ifeq 26
19: aload_1 // <---------- should be `aload_2`
20: checkcast #9 // class java/lang/String
23: astore_3
24: aload_3
25: areturn
26: aconst_null
27: areturn
```
There are two wrong places where I mark "// <---------- should be `aload_2`".
After fixing
```
java.lang.String test(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/String;
flags: (0x0000)
Code:
stack=1, locals=4, args_size=2
0: aload_1
1: instanceof #7 // class java/lang/CharSequence
4: ifeq 26
7: aload_1
8: checkcast #7 // class java/lang/CharSequence
11: astore_2
12: aload_2
13: instanceof #9 // class java/lang/String
16: ifeq 26
19: aload_1 // <---------- should be `aload_2`
20: checkcast #9 // class java/lang/String
23: astore_3
24: aload_3
25: areturn
26: aconst_null
27: areturn
```
One wrong byte code is also generated.
Both
During desugar, especially TransPatterns, we shouldn't make a tree which contains JCBindingPattern or BindingSymbol. So after desugar, the whole AST shouldn't contain JCBindingPattern or BindingSymbol anymore.
If not, the tree is wrong and the javac may crash or generate wrong bytecode unexpectedly.
- backported by
-
JDK-8269455 Javac generates uncorrect bytecodes when using nested pattern variables
-
- Resolved
-
-
JDK-8270642 Javac generates uncorrect bytecodes when using nested pattern variables
-
- Resolved
-
- relates to
-
JDK-8267610 NPE at at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop
-
- Resolved
-