RFR: 8293578: Duplicate ldc generated by javac
Maurizio Cimadamore
mcimadamore at openjdk.org
Thu Sep 15 15:14:45 UTC 2022
On Wed, 14 Sep 2022 17:15:30 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> 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.
Looks good
test/langtools/tools/javac/lvti/ConstantTypes.java line 99:
> 97: }
> 98:
> 99: }
watch out for newline
-------------
Marked as reviewed by mcimadamore (Reviewer).
PR: https://git.openjdk.org/jdk/pull/10272
More information about the compiler-dev
mailing list