Surprised that TYPE_USE annotations are accepted on `var` lambda parameters

Liam Miller-Cushon cushon at google.com
Wed Oct 19 19:52:53 UTC 2022


This feels like a bug to me too, I'd expect type annotations on var lambda
parameters to either be rejected, or accepted and included in the output,
but not accepted and then ignored.

I wonder if this could have been an oversight, and JLS 9.7.4 should be
updated to include lambda parameters that use var. Does anyone on the list
remember discussion about this type annotation edge case from JEP 323?

Looking at the handling of var locals in javac, I think the reason it
doesn't also reject var lambda parameters is that their type is inferred
earlier, so the check for `s.type.hasTag(NONE)` is false:
https://github.com/openjdk/jdk/blob/f872467d69a6d8442f8004609ce819641cab568b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java#L3425

On Wed, Oct 19, 2022 at 4:09 AM Chris Povirk <cpovirk at google.com> wrote:

> Since type-use annotations are rejected on var local variables, I would
>> have expected them to be rejected on var lambda parameters, too.
>>
>
> That position fits with the idea that, when we use var, we're expecting
> the full type to be inferred.
>
> In contrast, if it *were* possible to put annotations on var, there would
> be ambiguity about what the *absence* of an annotation means: Does it mean
> "Please infer whether an annotation should be present on the inferred
> type," or does it mean "Behave as if no annotation is present on the
> inferred type?"
>
> The conservative approach for developers would be to annotate *every* var
> declaration. But, in addition to being verbose, that approach is possible
> for our nullness work only if every type is either nullable or non-null—and
> that's not the case, since a type-variable usage T has a nullness that will
> depend on the type argument.
>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20221019/3e5ad586/attachment-0001.htm>


More information about the compiler-dev mailing list