RFR: 8214934: Wrong type annotation offset on casts on expressions

Vicente Romero vromero at openjdk.org
Fri Feb 27 20:42:53 UTC 2026


This is an interesting issue. Let's start with the code below:

import java.lang.annotation.*;
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
@interface TypeUse {}
class Test {
    private static String checkcast(Object obj) {
        return (@TypeUse String)(obj);
    }
}

javac produces this code for method `checkcast`:

private static java.lang.String checkcast(java.lang.Object);
    descriptor: (Ljava/lang/Object;)Ljava/lang/String;
    flags: (0x000a) ACC_PRIVATE, ACC_STATIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: checkcast     #7                  // class java/lang/String
         4: areturn
      LineNumberTable:
        line 7: 0
      RuntimeVisibleTypeAnnotations:
        0: #16(): CAST, offset=4, type_index=0
          TypeUse

see that the offset for the type annotation is incorrect, it should point to the offset of the `checkcast` instruction which is `1` not `4` which is the beginning of the following instruction. The reason for the issue is that TransTypes can sometimes create redundant typecasts. Later on during code generation these redundant type casts can get generated instead of the one present in the user code and this can affect the offset calculation as it is exposed by this test case. The proposal here is to not generate redundant type casts in TransTypes.

TIA

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

Commit messages:
 - removing debug code
 - docs
 - documentation
 - 8214934: Wrong type annotation offset on casts on expressions

Changes: https://git.openjdk.org/jdk/pull/29961/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=29961&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8214934
  Stats: 193 lines in 2 files changed: 188 ins; 3 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/29961.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/29961/head:pull/29961

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


More information about the compiler-dev mailing list