RFR: 8214934: Wrong type annotation offset on casts on expressions [v2]

Vicente Romero vromero at openjdk.org
Fri Feb 27 20:45:58 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

Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:

  fixing imports

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/29961/files
  - new: https://git.openjdk.org/jdk/pull/29961/files/c24a9c09..b23743ac

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=29961&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=29961&range=00-01

  Stats: 5 lines in 1 file changed: 4 ins; 0 del; 1 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