[code-reflection] Integrated: Disallow `null` return expressions in quoted lambdas

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Jul 8 16:42:51 UTC 2025


On Tue, 8 Jul 2025 12:07:13 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> Lambda expressions that are turned into a `Quoted` instance don't have an associated functional interface. Instead, they are type checked bottom-up (the parameter types in such lambdas must be explicit), and the return type is inferred from the various return expressions in the lambda body.
> 
> The aim of this patch is to establish a similar set of rules between quoted lambda return expressions and expressions that are allowed on the RHS of a `var`. More specifically, the RHS of a `var` is restricted as follows:
> 
> 1. can't be `null`
> 2. can't be a "naked" array initializer, e.g. `{ 1, 2, 3 }`
> 3. can't be a poly expression (a lambda, or a method reference)
> 
> Now, (2) and (3) are already taken care of. (2) is not allowed in return context, and (3) is already rejected, given the body of the quoted lambda is attributed w/o a target type.
> 
> So this PR adds a check for (1), to reject `null` return expressions.
> 
> There are some other quality-of-life changes to make sure the compiler doesn't generate spurious diagnostics in case of errors.
> 
> I've beefed up the test a bit. First, I've added tests to make sure that invalid return expressions in quoted lambdas are correctly rejected, regardless of whether the quoted lambda appears in assignment vs. method vs. generic method context.
> 
> Secondly, I've added an IR test to make sure that the inferred return type of a quoted lambda is correctly upward-projected, to get rid of any non-denotable type that might arise in the inference process.

This pull request has now been integrated.

Changeset: 547623b9
Author:    Maurizio Cimadamore <mcimadamore at openjdk.org>
URL:       https://git.openjdk.org/babylon/commit/547623b9d698187a12124bb4cb2cdf6e5ab9ac87
Stats:     198 lines in 11 files changed: 179 ins; 9 del; 10 mod

Disallow `null` return expressions in quoted lambdas

Reviewed-by: psandoz

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

PR: https://git.openjdk.org/babylon/pull/489


More information about the babylon-dev mailing list