RFR: 8293578: Duplicate ldc generated by javac

Jan Lahoda jlahoda at openjdk.org
Wed Sep 14 17:26:28 UTC 2022


Consider code like:

var testC = "incorrect".getClass();
var testV = testC.getConstructor(String.class)
                           .newInstance("correct");
System.err.println(testV);


javac has a concept of types with attached constants (to support proper behavior of constant expressions). Normally, these constants are cleared as specific places. But, in the above case, the type of `testC` is actually `Class<? extends String["incorrect"]>`, the constant then gets propagated, and eventually the type of `testV` is `String["incorrect"]`, and the code generator will then replace reads from `testV` with a ldc for the constant, so the code above prints "incorrect" instead of "correct".

The solution is to invoke `.baseType()` to remove the constant value when constructing the synthetic `.getClass()`'s type (so the type of `testC` is `Class<? extends String>`), and also a tweak to `checkLocalVarType`, as suggested in the bug.

-------------

Commit messages:
 - 8293578: Duplicate ldc generated by javac

Changes: https://git.openjdk.org/jdk/pull/10272/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=10272&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8293578
  Stats: 103 lines in 3 files changed: 99 ins; 0 del; 4 mod
  Patch: https://git.openjdk.org/jdk/pull/10272.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/10272/head:pull/10272

PR: https://git.openjdk.org/jdk/pull/10272


More information about the compiler-dev mailing list