Skip to content

A single shorthand scheme for augmented types, for prose and other non-source-code contexts #129

Open

Description

@cpovirk

kevin1e100 and I touched on this a bit in #32, and I think it's come up elsewhere. It's not a question that it's JSpecify job to answer, but we should keep it in mind as we discuss naming of annotations -- and maybe features that could add more information to augmented types (like having both lower and upper bounds?). Plus, we have many tool authors following these issues, and the GitHub issue UI is nice (and permits cross-linking with our other discussions). So I'm creating an issue here about the question. If nothing else, the issue may help me resist the urge to talk about this further on other issues :)

One way for tools to print augmented types (in their error messages, in tooltips in IDEs, etc.) is to make them look much like the source -- @Nullable Foo, @NullnessUnspecified T, etc. [edit: This is complicated by the existence of both declaration and type-use annotations, either or both of which may be in use in a given file.]

Another way is to use special symbols, similar to Kotlin -- Foo?, T!, etc.

We already gave some thoughts about symbols (especially !) in the linked discussion on issue 32. (And I owe kevin1e100 a reply, which I'll put below.)

Another thing to think about is that Java already uses ? for wildcards. This isn't ideal, but it doesn't have to be a fatal flaw: I don't think there's true ambiguity. (I do seem to recall that, the Checker Framework sometimes says things like @Nullable ?. Under Kotlin rules, that would become ??, which is no good. Perhaps we'd say (?)? or something.... But I expect most tools to refer to captured type variables instead thereby sidestepping the problem (by producing T1?, etc.).)

We can also consider whether we'll someday have non-nullness-related kinds of type annotations. I don't know that we will, but you can at least imagine having a type-use @CompileTimeConstant, @FormatString, @TypeCompatibleWith, something related to immutability and thread-safety?? Anyway, my point is that we might end up ouputting types using special symbols for nullness but using annotations for the other cases. That's probably tolerable, too.

Activity

  1. cpovirk commented on Sep 28, 2020

    @cpovirk
    CollaboratorAuthor

    from #32:

    @NullnessUnspecified is so noisy when it is never the cause of the failure

    Thanks for observing this.

    To me, ! suggests "not null"

    That is in fact what it meant in Spec# I believe. I've at times used !?, FWIW, but am not claiming it's a great solution.

    It may not be popular, but besides String!? there does seem to be another reasonable solution: just String could be used to mean @NullnessUnspecified String / String!? in error messages as long as both other options, @NonNull String / String!! and @Nullable String / String?, are always made explicit. This would remove "unspecified" clutter as desired and is consistent with the observation that

    the token "@NullnessUnspecified" never appears in the source file I'm imagining.

    I understand that this would take us away from Kotlin's error messages and is incompatible with the desire to generally write String!! as String. Instead, this solution would leverage "unspecified" as the default-default (so should still be reasonably intuitive to Java users).

    Thanks for the reference to Spec#. I could probably go along with !?, among other options.

    As for making String itself stand for @NullnessUnspecified String: My main concern is type-variable usages. We need a way to distinguish "T in annotated code" from "T in unannotated code." Using a more Kotlin-style notation, they're T and T!?, respectively. If we instead use T for the unannotated code, then "T in annotated code" probably has to become T!. That may sound like "T but not null," when in fact T might still permit nulls.

    (Maybe this is solvable by replacing ! with a different symbol in that model. Avoiding ! may be nice, anyway, as you say, to avoid the Kotlin connection.)

    I also feel a little sad that annotating more code for nullness may make error messages slightly longer (by adding ! in places). But I'm not too concerned about a single character, even when multiplied by n types.

    And I do acknowledge the upside that using String for unspecified matches what users see today in Java -- and somewhat matches what they will continue to see in the various non-nullness-related error messages they get from javac.

  2. added
    nullness
    on Oct 2, 2020
  3. kevinb9n commented on Jun 17, 2021

    @kevinb9n
    Collaborator

    I'm definitely in favor of this project developing a body of recommendations for tool authors in addition to our specifications.

    And I think consensus guidelines about error/warning messages are a great fit for that.

    And I heartily approve of concise notations for this (String? for @Nullable String). The notation must be universal regardless of whether the offending code is under @NullMarked or not, [so String must mean what it always has] [edit: nah].

  4. added this to the Release-independent milestone on Oct 6, 2021
  5. kevinb9n commented on Apr 22, 2022

    @kevinb9n
    Collaborator

    I've realized that I want a good way to talk about nullness-augmented types in prose, and I think that need and this may be the same need.

    We often do this in prose by writing out source code, but it's unwieldy. Plus technically we aren't bound by this convention, especially since what source code shows isn't really types anyway; it only shows declarations of types and usages of types, never types "themselves".

    Optimistically, we can teach users that the source code @Nullable String represents a usage of a type that we write down in prose as String? and that we pronounce verbally as "string-or-null". That primes them to understand compiler messages once they start seeing them. IntelliJ probably already has an option to render @Nullable String that way in the editor as it is (?).

    So then what notation for the other two kinds? I would suggest:

    • For unspecified, String*, pronounced "string-maybe-or-null" or "string-unspecified" (or "string-oblivious" to be like C#)
    • For non-null, String!, pronounced "string" because after all we should be saying everything with enthusiasm!

    I think it at least makes sense to decorate all three kinds for these reasons

    • All three carry "specific knowledge", whereas an unannotated usage of a nullable-bounded type variable (UUONBTV?) has parametric nullness, which is like the lack of all specific knowledge, and that seems like the thing to show unadorned.
    • While it does seem like the fact that javac messages will show unadorned base types is an argument for letting unadorned mean unspecified, I'm not so sure. If nullness messages are always using one of String!/String*/String? then a message including just String will look more clearly like something that's just not trying to talk about nullness at all, which I think might be good.
  6. changed the title [-]Discussion for tool authors: how to print augmented types?[/-] [+]How shall we describe augmented types in prose, which tool owners may take after in their finding messages?[/+] on Apr 22, 2022
  7. cpovirk commented on Apr 22, 2022

    @cpovirk
    CollaboratorAuthor

    I think I'd be good with that in prose.

    In error messages, I'd feel a little sad adding even one character for the non-null case (i.e., saying String! instead of String). One character is not a big deal, but it adds up for complex types. That goes not just for printing parameterized types individually but also for printing whole signatures, which may contain a number of types.

    Also, the difference from Kotlin's meaning is unfortunate.

    Neither of those is a deal-breaker on its own. But put them together, and I think we'd be better served by String in error messages, especially when users are likely to be switching between Java and Kotlin error messages as they work.

    I think it would be OK for prose and error messages to differ in this respect: Prose needs to express "When you write Foo, you get Foo! or Foo*, depending on...." In contrast, error messages live in a world in which we've already converted the source code to types. It's true that users will also see compile errors that are unrelated to nullness, and there is definitely value in making those look distinct. But I think the greater danger is in making nullness error messages look scarier than they need to.

  8. kevinb9n commented on Jun 21, 2022

    @kevinb9n
    Collaborator

    I would very very much like us to be able to settle on ONE scheme for how to notate these types. So I think we have some tension over whether non-null should be explicitly marked with !.

    One complication is that we're supporting non-null projection of type variables (for the moment), meaning that T and T! have distinct meanings (unprojected T, and T projected to non-null).

  9. kevinb9n commented on Jun 22, 2022

    @kevinb9n
    Collaborator

    I do think this is important to nail down and rely on in communication/documentation/etc.

    The suffix that goes naturally with each of the 4 nullness descriptors in Chris's current spec are:

    • UNION_NULL "?"
    • MINUS_NULL "!"
    • NO_CHANGE ""
    • UNSPECIFIED "*"

    For type variables these are all distinct and all make sense. (Well, maybe the last doesn't, but let's move on)

    For non-variable reference types in null-marked code, "" and "!" are the same in effect. I propose we promote the explicit, redundant usage of the "!" suffix to provide needed clarity, in all contexts. This scheme will then work equally well for talking about null-unmarked code, where "no change" might have seemed to mean "unspecified".

    I think the only case I left uncovered is value types (i.e. the 8 primitives) and suffixes should just not apply in that case.

    So the proposal:

    • ? suffix always means UNION_NULL and is always used for that
    • ! suffix always means MINUS_NULL and is always used for that even when strictly unnecessary
    • * suffix always means UNSPECIFIED and is always used for that
    • No suffix always means NO_CHANGE and is only recommended for type variable usages and value types (for nonvariable reference types, use MINUS_NULL/! instead)

    Also, as I think maybe David was the first to suggest, I think Foo!<Bar?> is much nicer than Foo<Bar?>!.

    I think our reference checker should follow this scheme in its error messages. But if Tool X doesn't want to, okay. We could try out letting these actually show to our internal users and see how it goes.

  10. netdpb commented on Jun 22, 2022

    @netdpb
    Collaborator

    I like this proposal generally. I'm a little worried about leaving out the ! for value types. Right now the primitive types are well known and spelled differently by convention (lowercase initial letter), so it's easy to remember that int doesn't include null. But in a Valhalla world there will be value types that are not well known and are spelled with an initial uppercase letter just like reference types, and I think those should use !.

    What's wrong with always using int! for consistency? (Not disallowing int, but not preferring it either.)

  11. cpovirk commented on Jun 22, 2022

    @cpovirk
    CollaboratorAuthor

    In error messages in particular, do you think that the lack of ! would lead to confusion?

    For example, if a user saw this...

    my/project/File.java:79: error: invalid return type
        method return type: Foo
        expression type: Foo?
    

    ...I think that it would be pretty clear that the method was returning a nullable value when a non-nullable value was expected.

    Maybe there are cases in which ! would be more useful. But I think it's more likely to be a net negative, as laid out in #129 (comment). To expand slightly on that: It would be a nice carrot for error messages to become simpler as users specify the nullness of more types -- in particular when the alternative is for error messages to look more like Kotlin but with slightly different meaning!

  12. netdpb commented on Jun 22, 2022

    @netdpb
    Collaborator

    I think we should consider specifications separately from tool outputs.

    I think specifications, including these design discussions and conformance tests, should always prefer the most unambiguous terms, including int!.

    On the other hand, tool outputs such as error messages can be freer to prefer shorter forms when they expect users to take context into account. In any case, we don't intend to constrain tool output very harshly.

    I'd like not to care very much about tool outputs. I don't expect Kotlin to change how they refer to platform types (with !), for example, even though that directly conflicts with our notation here.

  13. kevinb9n commented on Jun 22, 2022

    @kevinb9n
    Collaborator

    But in a Valhalla world there will be value types that are not well known and are spelled with an initial uppercase letter just like reference types,

    News: I've been pushing hard that the value type always be spelled Foo.val (where Foo is the corresponding reference type), and at the moment I seem to be winning. But if it goes the other way, we'll have plenty of time to reconsider.

    What's wrong with always using int! for consistency? (Not disallowing int, but not preferring it either.)

    My feeling is that people would find it strange, distracting, and/or overzealous.

  14. 31 remaining items

  15. kevinb9n commented on Sep 22, 2022

    @kevinb9n
    Collaborator

    Wiki page for this notation exists now. Incomplete, but spells out the goals I think we're shooting for.

    Another way to serve that goal is to always output augmented types inside special markers, at least when using them in prose or other locations that we worry may be ambiguous. So we could write that "The type of List<@Nullable String> in null-unmarked code is 'List*<String?>', but the type in null-marked code is 'List<String?>'."

    Reminiscent of putting slashes around regular expressions when you mention them. (But, most people don't do that.)

    My guess is that people would bother to do that rarely enough that no one would understand what it means when they do anyway. It also feels a wee bit like an "extra" detail to have to teach people besides just the idea of a suffix.

    Issues

    Issue 1: non-null character

    IF a non-null character is needed, what should it be?

    • !
      • − means something else in kotlin
    • +
      • ~ regex precedent argues for no suffix, but + is at least close-ish to the right meaning

    Issue 2: parametric character

    IF a parametric character is needed, what should it be?

    • %
      • + starts with p; symbol could be seen as suggesting 'this or that'
    • ...?
      • (haven't heard any other suggestions)

    Issue 3: Scheme

    1. Always suffix
      • + Maximally clear; no base/augmented ambiguity
    2. Omit suffix for parametric only
      • + Parametric is probably the weirdest symbol
    3. May omit... (allow both 1 and 2)
    4. Omit suffix for non-null only, except we need it for a "real" non-null projection
      • + Similar to kotlin
    5. May omit... (allow both 1 and 4)
    6. Omit suffix for "parametric if a nullable-bounded type variable, non-null otherwise"
      • ~ Baasically "no change", but this is a nullness "state", not a nullness "operator" a la the spec
    7. May omit... (allow both 1 and 6)

    Comparison of options

    For the table below I'll just arbitrarily assume + and % from above.

    Example signature: class Foo<A extends @Nullable Object, B>

    Code Description 1 2 3 4 5 6 7
    A parametric A% A A, A% A% A% A A, A%
    @NonNull A "real" non-null projection A+ A+ A+ A+ A+ A+ A+
    B/@NonNull B non-null type var B+ B+ B+ B B, B+ B B, B+
    Xy/@NonNull Xy non-null non-t.v. Xy+ Xy+ Xy+ Xy Xy, Xy+ Xy Xy, Xy+
  16. modified the milestones: 1.0, 0.3 on Oct 14, 2022
  17. kevinb9n commented on Oct 14, 2022

    @kevinb9n
    Collaborator

    I still find myself really reluctant to give this up in the current docs, so I'm going to quasi-optimisticallly retarget for 0.3, knowing we could still bump it back out again.

  18. kevinb9n commented on Oct 26, 2022

    @kevinb9n
    Collaborator

    To get a "working decision" for now, I claim the simplest thing we can do is:

    • Always suffix
    • Just use ! for NN because that's what we've been doing

    I can see a likely enough change to + or to a different scheme before 1.0.

    For now we are marking issues closed when they have a working decision.

  19. added
    documentation
    on Nov 30, 2022
  20. kevinb9n commented on Mar 13, 2024

    @kevinb9n
    Collaborator

    There's a detailed reasoned argument against putting the nullness signifier before the type arguments. Enough work was put into it that I'm reopening this for now, but the doc I'm referring to should be published. @eamonnmcmanus

  21. eamonnmcmanus commented on Aug 2, 2024

    @eamonnmcmanus
    Collaborator

    Sorry, I wasn't able to share the original document publicly and I procrastinated about making a sharable copy. There is one now: Short-form nullness for generic types.

  22. kevinb9n commented on Aug 15, 2024

    @kevinb9n
    Collaborator

    Now that https://openjdk.org/jeps/8303099 exists, I think the nature of this debate changes. JSpecify should simply mirror what the language syntax is going to be -- to the extent we can know/predict it.

    Éamonn and I should probably repurpose this debate into a debate about that syntax, in that forum.

  23. cpovirk commented on Aug 16, 2024

    @cpovirk
    CollaboratorAuthor

    The JEP definitely changes how I feel about various notation questions (mainly because mirroring Java is much more important than mirroring Kotlin):

    • this post/mid discussion
    • the use of ! for non-nullable
    • what to do about * and % (and I still think one other that we'd need for a complete set), along with whether to use ! for non-type-variable types (e.g., String)

    Parts of that last one are still tricky, since we have less idea what the JDK will do for generics in particular. Even with that, it makes sense to be as much in contact with the language developers as possible.

  24. kevin1e100 commented on Aug 16, 2024

    @kevin1e100
    Collaborator
  25. kevinb9n commented on Aug 16, 2024

    @kevinb9n
    Collaborator

    Aside from !/!! that sounds like the current scheme, which is documented at https://github.com/jspecify/jspecify/wiki/notation

    This issue is open to consider changes to that scheme:

    • The placement relative to type argument list
    • We might have gotten the order of String?[]![]? wrong in that
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

    documentationnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions