Skip to content

Allow @Null*arked to target a field #310

Closed

Description

@cpovirk

So far: no (#50).

#50 also said "no" to method parameters and local variables. I don't want to encourage us to revisit those parts.

I do want to revisit fields, though not urgently.

We've compiled at least 3 reasons against @NullMarked/@NullUnmarked on fields:

(1) We didn't use to even support METHOD and CONSTRUCTOR, and clearly the case for FIELD was worse than for those.

But now we've reversed that and allowed METHOD and CONSTRUCTOR. So we can consider FIELD, too, while still recognizing that its case is weaker than those.

(2) From bytecode, we can't tell what was done inside a field initializer.

But that's OK if we consider @NullMarked on a field to affect only the type of that field, not anything done in the initializer (e.g., inside an anonymous class).

This does mean that tools that read source code will have to be careful not to over-apply the effects of @NullMarked on a field.

Beyond that, annotating a field is just weird:

  • It's weird because fields don't count as "enclosing elements" the way that @NullMarked's other targets do.
  • It's weird that the annotation means something there but not what users might naively expect. (Hopefully most users just don't care about this at all, since they won't annotate fields and/or won't annotate fields with initializers and/or won't think or care about how the annotation affects those initializers.)

(3) We didn't have particularly compelling use cases.

I wouldn't call what I'm about to say "compelling," but I can give a less uncompelling variant of my earlier argument.

One of the things we've been doing is annotating classes in the JDK. That includes putting @NullMarked on JDK classes. But what happens when we provide annotations for the Java 11 version of a class and someone tries to use them with Java 17? Our annotations might say that JDK class Foo is @NullMarked, but they won't have any information about a field Foo.addedInJava17 (#94).

The conservative thing for tools to do is to treat Foo.addedInJava17 as @NullUnmarked. And if the tool reads the JDK annotations directly, it can do that.

However, we've been talking about providing a tool to rewrite JDK jars to actually include the annotations. That means that Foo would end up with an actual @NullMarked annotation in its class file. And then we need a way for the tool to tell nullness checkers "but addedInJava17 is @NullUnmarked"... like, say, by automatically putting a @NullUnmarked annotation on it :)

Really, I might discourage users from writing these annotations on a field themselves. But having the ability to insert them (mainly @NullUnmarked) with a tool might be valuable.

Or this might be too niche to care about: Our tool could already label methods as @NullUnmarked. How often would there be an API that is (a) newer than our JDK annotations that is (b) a public field that (c) needs @Nullable or another annotation? It's probably rare. Hopefully we'll actually try this and get some real data someday.

Activity

  1. added this to the 1.0 milestone on Oct 28, 2022
  2. kevinb9n commented on Nov 2, 2022

    @kevinb9n
    Collaborator

    It's weird because fields don't count as "enclosing elements" the way that @NullMarked's other targets do.

    Fun times:

    @NullMarked
    private String foo = switch("You've") {
        default -> {
            class GottaBe { 
                ...
            }
            yield "KiddinMe";
        }
    };

    That is NOT a compelling use case for the feature. :-)

    I do think in most ways allowing it on FIELD should be harmless.

    But I do have one subtle concern. Right now the @Null*arked and the @Nullable/@NN annotations don't tread on each other's turf much at all... with the exception that they could mix it up in and around method modifiers (google-java-format, for one, fixes that).

    Their domains being neatly partitioned might be helpful to novice users keeping the "big and clear" difference between them straight. I think if they see @Null*arked on a field or just that it can go there it might muddy the waters a bit. A field, unlike a method, really feels like the kind of "single unit" that would only have a type-use annotation (even though we know it has type components, and the code above notwithstanding).

    Again, subtle.

    And then we need a way for the tool to tell nullness checkers "but addedInJava17 is @NullUnmarked"... like, say, by automatically putting a @NullUnmarked annotation on it :)

    Sufficiently edgecasey and generally user-invisible that perhaps a tool-specific @ImpliesNullUnmarked would serve?

    EDIT: that doesn't spare JSpecify from having to specify semantics for that case, but maybe that would just be hidden in the spec where no sane person will find it. :-)

  3. cpovirk commented on Nov 8, 2022

    @cpovirk
    CollaboratorAuthor

    If it turns out that we care about this for types in the JDK or other types that we want to annotate, then we may want it to work across tools. But now that you mention it, maybe we'd only really need it to work Kotlin, since that's the main tool that inserts runtime checks? If so, we may be able to satisfy it with a @TypeQualifierNickname approach. (In doing so, we'd be defining and using a type-use @UnspecifiedNullness annotation :\ But we can hold our noses, since, again, it would be a tool-specific annotation, one that we could avoid exposing to end users. It wouldn't be the first time :))

    As for the concern: I'm not sure how much differently users will see fields and methods. But I definitely grant that, between the two of them, the field looks more like a "single unit." And methods will always be the more compelling use case.

    Primarily, I'm holding out hope that my proposed problem scenario is rare enough that we'll find it easiest to just manually annotate any public fields that need @Nullable.

  4. added
    nullness
    on Nov 29, 2022
  5. cpovirk commented on Nov 30, 2022

    @cpovirk
    CollaboratorAuthor

    I also see a slight connection to another idea that I think has come up, the idea of annotating only the user-visible parts of a class. For that purpose, it could be nice to be able to annotate a class (or enclosing package/module) as @NullMarked and then put @NullUnmarked on all its private and package-private members—including fields. (This reminds me of Spring's @NonNullApi/@NonNullFields distinction.)

    This just came up because AutoValue annotates its user-facing APIs but not its fields. That said, we're likely going to just get AutoValue to address that :) Plus, for obvious reasons, most tools don't care about the private members of a class in the first place, so incorrect annotations there usually don't matter (though they do in the unusual case we got a report about).

    I still don't see this as an important reason to have FIELD as a target. If anything, it is a slight motivation for opening an actual issue about "only the user-visible API is @NullMarked," except that that feels pretty iffy to me, too. If anyone really wants to do that, it's mostly possible to put @NullUnmarked on the class (if necessary to override an enclosing @NullMarked annotation) and then put @NullMarked on each user-visible method. The complicated case would be a generic class, but even that is still workable as long as each type-parameter bound can be declared as extends @Nullable Something or extends @NonNull Something (as opposed to <T ..., U extends T>).

  6. added
    design
    on Nov 30, 2022
  7. kevinb9n commented on Dec 1, 2022

    @kevinb9n
    Collaborator

    To be clear about my little gotcha

    @NullMarked
    private String foo = switch("You've") {
        default -> {
            class GottaBe { 
                ...
            }
            yield "KiddinMe";
        }
    };

    I am saying that I think if we allowed it, it has to make the GottaBe class null-marked as well.

  8. changed the title [-]Can `@Null*arked` target a field (affecting only that field, not anything in its initializer)?[/-] [+]Allow `@Null*arked` to target a field[/+] on Dec 1, 2022
  9. changed the title [-]Allow `@Null*arked` to target a field[/-] [+]Allow `@Null*arked` to target a field [working decision: no][/+] on Dec 1, 2022
  10. cpovirk commented on Dec 2, 2022

    @cpovirk
    CollaboratorAuthor

    Yeah. While I am happy to have spent November with this bouncing around in my brain, I think it's clear that some new information would have to surface for us to consider it worthwhile.

    I agree that the gotcha is bad (though hopefully very unlikely to come up). I might be willing to accept it if we saw an actual, significant need for this (like in my JDK example above), but that need itself seems very unlikely to come up.

    As a further point against this, I'd note that I wrote my comment about annotating user-visible APIs as if @NullMarked would affect only API signatures. But in fact we've said that it affects implementation code, too (though doing anything with annotations in implementation code in bytecode is unlikely to go well, anyway). So anyone who does what I said in that comment would likely already be "lying" a bit, and it's hard to get excited about adding a footgun feature to help them lie slightly less in one case.

  11. modified the milestones: 1.0, 0.3 on Dec 2, 2022
  12. cpovirk commented on Aug 16, 2023

    @cpovirk
    CollaboratorAuthor

    I am still happy with the answer of "no" here. But I'll note for the record that I just ran into a bug that was manifesting only when a particular field was recognized as non-nullable, and it would have been convenient to be able to use @NullUnmarked on that field.

    That said, we have the workaround of applying @NullMarked to all other members of the class instead. And also, the bug is in a tool that doesn't support @NullUnmarked yet, anyway... :)

  13. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    Current decision: @NullMarked and @NullUnmarked cannot target fields because they do not enclose other nonlocal declarations. No compelling use case has emerged that requires such annotation targets. In any case, we could always add such target validity in the future, compatibly, if such a case is described.

    Argument for changing: Fields do kind of enclose their initializers, and so @NullMarked and @NullUnmarked could be used to mark local classes or type arguments within them. However, that is an infrequent case, and could be considered implementation-only and outside the required scope of JSpecify.

    Timing: This must be decided before version 1.0 of the jar.

    Proposal for 1.0: Finalize the current decision. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.

    Results: This proposal received seven 👍s and no other votes. It is finalized. I'll edit the title to reflect that.

  14. netdpb commented on May 17, 2024

    @netdpb
    Collaborator

    Decision: @NullMarked and @NullUnmarked do not target fields.

  15. changed the title [-]Allow `@Null*arked` to target a field [working decision: no][/-] [+]Allow `@Null*arked` to target a field[/+] on May 17, 2024
  16. added
    discussion
    on May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    designdiscussionnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions