RFR: 8336491: Unnecessary boxing conversions in void-returning lambdas [v2]

Liam Miller-Cushon cushon at openjdk.org
Thu Jul 18 17:30:31 UTC 2024


On Thu, 18 Jul 2024 09:34:48 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> So I suppose you concluded that it would have been safe to generalize what `boxIfNeeded` is doing by skipping `void`, which can only really occur when lowering a lambda expression body. Correct?

Yes, that's where I ended up. The initial approach was to handle this in `visitLambda` for expression lambdas, and I added an assertion to `boxIfNeeded` to verify that it wasn't called with `VOID`, but then the tests pointed out a case where I think an expression lambda is first desugared to a block lambdas to handle boxing `void` to `Void`.

Your alternative of setting `currentRestype` to `Type.noType` should handle that case as well, I'm happy to make that switch if you'd prefer it over handling `VOID` in `boxIfNeeded`.

---

Distilled from `test/langtools/tools/javac/patterns/MatchExceptionLambdaExpression.java`:


class MatchExceptionLambdaExpression {

  public static void main(String[] args) {
    doRunPrimitiveVoid(o -> checkVoidBox(o instanceof A(String s)));
  }

  static void doRunPrimitiveVoid(PrimitiveVoidFI toRun) {}

  static Void checkVoidBox(boolean a) {
    return null;
  }

  interface PrimitiveVoidFI {
    public void run(Object o);
  }

  record A(String s) {}
}
...
java.lang.AssertionError
	at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
	at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.boxIfNeeded(Lower.java:3333)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.translate(Lower.java:2150)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.visitReturn(Lower.java:3842)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1769)
	at jdk.compiler/com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.translate(Lower.java:2139)
	at jdk.compiler/com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:70)
	at jdk.compiler/com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:172)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:3812)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1133)
	at jdk.compiler/com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:58)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.translate(Lower.java:2139)
	at jdk.compiler/com.sun.tools.javac.comp.Lower.visitLambda(Lower.java:3854)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:2035)

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

PR Comment: https://git.openjdk.org/jdk/pull/20222#issuecomment-2237129242


More information about the compiler-dev mailing list