Skip to content

Distinguish categories of unknownness #62

Closed

Description

@stephan-herrmann

I propose we use the word "unknown" with greatest care and use more specific terms wherever possible.

  1. Any concrete type without a nullness annotation (explicit or from default) is "unspecified", or has "unspecified nullness".
  2. A type parameter with no nullness specification for its bounds is "unconstrained", or has "unconstrained nullness". The same for wildcards.
  3. A local variable that will hold null if one branch in the code is taken, and nonnull on others, also has unknown nullness, but here the reason is closely connected to local flow analysis.

While (3) is mostly irrelevant for our task (but relevant for warnings issued by checkers), I believe the difference between (1) and (2) is key for getting a convincing story for type parameters / type variables / wildcards.

Avoiding (1) is necessary for soundiness, because practically checkers cannot check against unspecified signatures. Unspecified closely relates to "unchecked".

Avoiding (2) is not necessary at all, if instantiation of such generic types still ensures that all type arguments have specified nullness.

As a consequence from this distinction, Eclipse has coined the term of "pessimistic checking" for an unconstrained type variable.

Activity

  1. cpovirk commented on Aug 12, 2019

    @cpovirk
    Collaborator
    1. A type parameter with no nullness specification for its bounds is "unconstrained", or has "unconstrained nullness". The same for wildcards.
      ...
      Avoiding (2) is not necessary [for soundness], if instantiation of such generic types still ensures that all type arguments have specified nullness.

    Duplicating myself from #45, I'm seeing this differently (or just misunderstanding you):

    Suppose that the T in Optional<T> has "unconstrained nullness" (presumably because we don't have a stub defined for it, even though in practice we would). Then suppose that a user tries to create an Optional<@Nullable String> with Optional.of(null). The user is specifying a nullness value for the type argument, but the call is still unsound.

    The "pessimistic checking" you describe sounds to me like it has the same goal as the spec here has. The spec does it by:

    • treating unannotated type parameters as having an @UnknownNullness bound
    • applying the unknown nullness to all usage sites (unless explicitly annotated), regardless of the instantiated type

    The spec would (as I understand it) produce warnings in both the example cases you give. Unless that's wishful thinking on my part?

  2. stephan-herrmann commented on Aug 12, 2019

    @stephan-herrmann
    Author

    The "pessimistic checking" you describe

    This term refers to checking bodies of a generic class. So if requireNonNull() were annotated as requireNonNull(@NonNull T obj) then pessimistic analysis would report that this constructor:

    private Optional(T value) {
        this.value = Objects.requireNonNull(value);
    }
    

    is bogus because it passes a value of type T (which could be a nullable type) where requireNonNull requires a nonnull value. So to fix this class we'd need to add a bound to the type parameter, such as to constrain T to only nonnull types. After that fix, the instantiation Optional<@Nullable String> will be detected as bogus. Voila.

    Issues involving an unconstrained type parameter are a different kind of issue than any "null-unchecked" conversion. The latter only happens when "legacy" code is involved, whereas an unconstrained type parameter is perfectly OK also in 100% annotated code. I hold that ability to separately configure severity for both kinds of warnings is important.

  3. cpovirk commented on Aug 12, 2019

    @cpovirk
    Collaborator

    Thanks. Is the ability to separately configure severity the main enhancement desired? Are there other enhancements that you can tell I've missed?

    Configurable severity reminds me of #55, which I know you've seen.

    I suspect that "pessimistic checking," which sounds like "parametric nullability" under Discussion: Expression Types, will have a distinct warning type from the legacy/unspecified case. I base that on discussions like #57 (comment) and #33 (comment), which differentiate between "strict" and "lenient" modes based on whether "unchecked" conversions (those involving legacy/unspecified warnings) are errors.

    If we're lucky, the needed distinctions might already exist. But again, I may be missing other parts to this.

  4. kevinb9n commented on Aug 14, 2019

    @kevinb9n
    Collaborator

    I think I'm in 100% agreement with Stephan on the concepts, and we'll just have to pull our terminology together sometime soon.

    The third kind of nullness, which (non-primitive, etc.) legacy type usages have by default, is probably best described as "unspecified nullness". (This is the case we started trying to consistently call "unknown nullness" a month or two ago, but I'm more than open to a shift.)

    On the other hand, for a bounded type variable:

    1. If the upper bound is @NotNull (whether explicitly or because we decided defaults apply here -- that part debated elsewhere), we simply say the unannotated type variable usages are also definitively @NotNull, because why not.

    2. If the upper bound is @nullable (same parenthetical as above), we have been saying that unannotated type variable usages have "parametric nullness", though "unconstrained nullness" sounds like a good fit for this too. And this is definitely different from unspecified nullness (even the JVMLS slides make a quick mention of this); I think trying to assign something nullable to it, or assign it to something non-nullable, is more a of "hard no" than just an "unchecked-ish warning".

    3. If the upper bound is of unspecified nullness (perhaps explicitly, but usually because no default exists), I think we need to flesh out this story better. The conceptualization in How to conceptualize subtyping for legacy-nullness #33 (notnull <: unspec <: nullable) would imply that it can be parameterized with either unspec or notnull but not with nullable, but I haven't yet figured out whether that asymmetry is sensible or a weird accident. There should be an issue somewhere that's about that, but I'm not spotting it easily atm. [cpovirk edits: Some of this is under discussion in what happens to type variables when @NullnessUnknown (and @NotNull) type parameters are instantiated? #45.]

  5. added
    nullness
    on Oct 25, 2019
  6. kevinb9n commented on Jun 19, 2021

    @kevinb9n
    Collaborator

    I think we were all in sync on the conceptual model put forth here, and basically still are.

    Just in terms of actual word choices -- I might kinda like "unconstrained" over "parametric" because it more clearly works well for both type variables and wildcards. This may be a term we just need for conversational purposes, though. Officially the stance could be that these (like intersection types) don't have any describable nullness of their own.

  7. kevinb9n commented on Jun 19, 2021

    @kevinb9n
    Collaborator

    (I just couldn't quite rationalize keeping this whole issue open, over the unconstrained-vs.-parametric word choice alone. But you can still comment while it's closed if you want to share any opinion.,

  8. added
    design
    on Mar 13, 2024
  9. added this to the 0.3 milestone on Mar 27, 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

    designnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions