RFR: 8372635: Lambdas do not copy over SYNTHETIC flag for local variables
Vicente Romero
vromero at openjdk.org
Tue Dec 9 20:05:07 UTC 2025
On Tue, 9 Dec 2025 15:04:42 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> Consider case like:
>
> Object o = ...;
>
> //synthetic variable for the instanceof's expression
> boolean b = o.toString() instanceof String str && str.isEmpty();
>
> Runnable r = () -> {
> //synthetic variable for the instanceof's expression
> boolean b = o.toString() instanceof String str && str.isEmpty();
> };
>
>
> There are synthetic variables generated for `o.toString()`, to ensure the method is invoked only once for each `instanceof`. These variables are marked as synthetic in `TransPatterns` (and then consequently omitted in `LocalVariableTable`), but when the flag is lost for the variable in the lambda.
>
> This PR proposes to keep the flag, as suggested in the bug.
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java line 1155:
> 1153: break;
> 1154: case LOCAL_VAR:
> 1155: ret = new VarSymbol(sym.flags() & (SYNTHETIC | FINAL), sym.name, sym.type, translatedSym);
this will effectively make synthetic all local variables, should we do this starting from source 27?, what about the parameter being generated below? should be it synthetic too?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/28724#discussion_r2604141496
More information about the compiler-dev
mailing list