-
Bug
-
Resolution: Unresolved
-
P3
-
None
-
23, 25
A DESCRIPTION OF THE PROBLEM :
When using "spotbugs" for static code analysis, it gives a warning for the code if compiled with JDK23, but not if compiled with JDK22:
M D DLS: Dead store to $L7 in DeadStoreBug.getDouble() At DeadStoreBug.java:[line 21]
Of course, this may be a bug in spotbugs, but looking at the byte code I don't think so.
In the byte code there is the instruction "dstore 7" that is not followed up by any load.
Byte code:
public java.util.OptionalDouble getDouble();
descriptor: ()Ljava/util/OptionalDouble;
flags: (0x0001) ACC_PUBLIC
Code:
stack=4, locals=9, args_size=1
0: aload_0
1: getfield #15 // Field value:LDeadStoreBug$MyInterface;
4: astore 4
6: aload 4
8: instanceof #48 // class DeadStoreBug$RecordWithDouble
11: ifeq 42
14: aload 4
16: checkcast #48 // class DeadStoreBug$RecordWithDouble
19: astore_1
20: aload_1
21: invokevirtual #50 // Method DeadStoreBug$RecordWithDouble.d:()D
24: dstore 5
26: dload 5
28: dstore 7
30: iconst_1
31: ifeq 42
34: dload 5
36: dstore_2
37: dload_2
38: invokestatic #54 // Method java/util/OptionalDouble.of:(D)Ljava/util/OptionalDouble;
41: areturn
42: invokestatic #59 // Method java/util/OptionalDouble.empty:()Ljava/util/OptionalDouble;
45: areturn
46: astore_1
47: new #39 // class java/lang/MatchException
50: dup
51: aload_1
52: invokevirtual #41 // Method java/lang/Throwable.toString:()Ljava/lang/String;
55: aload_1
56: invokespecial #45 // Method java/lang/MatchException."<init>":(Ljava/lang/String;Ljava/lang/Throwable;)V
59: athrow
REGRESSION : Last worked in version 22
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the code.
(And run through spotbugs).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiled code does no dead store.
(And spotbugs does not warn).
ACTUAL -
Compiled code does dead store.
(And spotbugs warns).
---------- BEGIN SOURCE ----------
import java.util.Objects;
import java.util.OptionalDouble;
import java.util.OptionalInt;
public class DeadStoreBug {
private final MyInterface value;
private DeadStoreBug(final MyInterface aValue) {
value = Objects.requireNonNull(aValue);
}
public OptionalInt getInt() {
if (value instanceof RecordWithInt(final int i)) {
return OptionalInt.of(i);
}
return OptionalInt.empty();
}
public OptionalDouble getDouble() {
if (value instanceof RecordWithDouble(final double d)) {
return OptionalDouble.of(d);
}
return OptionalDouble.empty();
}
public sealed interface MyInterface permits RecordWithInt, RecordWithDouble {
}
private record RecordWithInt(int i) implements MyInterface {
}
private record RecordWithDouble(double d) implements MyInterface {
}
}
---------- END SOURCE ----------
When using "spotbugs" for static code analysis, it gives a warning for the code if compiled with JDK23, but not if compiled with JDK22:
M D DLS: Dead store to $L7 in DeadStoreBug.getDouble() At DeadStoreBug.java:[line 21]
Of course, this may be a bug in spotbugs, but looking at the byte code I don't think so.
In the byte code there is the instruction "dstore 7" that is not followed up by any load.
Byte code:
public java.util.OptionalDouble getDouble();
descriptor: ()Ljava/util/OptionalDouble;
flags: (0x0001) ACC_PUBLIC
Code:
stack=4, locals=9, args_size=1
0: aload_0
1: getfield #15 // Field value:LDeadStoreBug$MyInterface;
4: astore 4
6: aload 4
8: instanceof #48 // class DeadStoreBug$RecordWithDouble
11: ifeq 42
14: aload 4
16: checkcast #48 // class DeadStoreBug$RecordWithDouble
19: astore_1
20: aload_1
21: invokevirtual #50 // Method DeadStoreBug$RecordWithDouble.d:()D
24: dstore 5
26: dload 5
28: dstore 7
30: iconst_1
31: ifeq 42
34: dload 5
36: dstore_2
37: dload_2
38: invokestatic #54 // Method java/util/OptionalDouble.of:(D)Ljava/util/OptionalDouble;
41: areturn
42: invokestatic #59 // Method java/util/OptionalDouble.empty:()Ljava/util/OptionalDouble;
45: areturn
46: astore_1
47: new #39 // class java/lang/MatchException
50: dup
51: aload_1
52: invokevirtual #41 // Method java/lang/Throwable.toString:()Ljava/lang/String;
55: aload_1
56: invokespecial #45 // Method java/lang/MatchException."<init>":(Ljava/lang/String;Ljava/lang/Throwable;)V
59: athrow
REGRESSION : Last worked in version 22
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the code.
(And run through spotbugs).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiled code does no dead store.
(And spotbugs does not warn).
ACTUAL -
Compiled code does dead store.
(And spotbugs warns).
---------- BEGIN SOURCE ----------
import java.util.Objects;
import java.util.OptionalDouble;
import java.util.OptionalInt;
public class DeadStoreBug {
private final MyInterface value;
private DeadStoreBug(final MyInterface aValue) {
value = Objects.requireNonNull(aValue);
}
public OptionalInt getInt() {
if (value instanceof RecordWithInt(final int i)) {
return OptionalInt.of(i);
}
return OptionalInt.empty();
}
public OptionalDouble getDouble() {
if (value instanceof RecordWithDouble(final double d)) {
return OptionalDouble.of(d);
}
return OptionalDouble.empty();
}
public sealed interface MyInterface permits RecordWithInt, RecordWithDouble {
}
private record RecordWithInt(int i) implements MyInterface {
}
private record RecordWithDouble(double d) implements MyInterface {
}
}
---------- END SOURCE ----------