Skip to content

When a stub file covers only part of an API, can APIs affected without being declared? #94

Open

Description

@cpovirk

(I don't think we've settled on a format for stubs / external annotations, so I'll just follow the Checker Framework's lead and write Java sources -- even though it's possible that the choice of format will have some effect on the questions I'm going to discuss.)

I'm not sure if my question in the issue title makes sense on its own, but my vote is "no" :)

In more detail:

Suppose someone is writing code that uses java.util.Map but doesn't have a stub file for it. (Realistically, we seem likely to provide a stub. But I'm not going to say anything unique to Map. I pick it only because it's well known and it should ultimately be annotated to use both V and @Nullable V.)

The user gets annoyed that writing Map<String, @Nullable Object> produces a warning (as it might) and decides to create an initial stub file:

interface Map<K extends @Nullable Object, V extends @Nullable Object> {}

That eliminates the warning, but what happens to the user's other usages of the Map API?

This example is about stub files, but I've designed it to be otherwise similar to the one in #89 (comment). As in that thread, I think that the rest of the Map API should be unaffected. For example, Map<String, String>.get(...) should still return @NullnessUnspecified V.

In the other thread, @kevin1e100 advocated for different behavior. Namely: A user who annotates the type-parameter bound should also annotate all usages. So, once the type-parameter bound is annotated, we're justified in concluding that the user has consciously declined to annotate get. Thus, we can assume that the return type of get is V (as opposed to @NullnessUnspecified V or @Nullable V).

But we could see this case as different than the case in that thread: There, the bounds were annotated in a full source file. Here, they are annotated only in a stub, which does not even contain declarations for methods like get. So we could argue that K and V should be considered to have been annotated only when we're looking at methods that are actually defined in the stub file. In this case, there are no methods in the stub file, so we'd continue to treat get as returning @NullnessUnspecified V.

Now let's tweak the example to cause problem for my model, too:

@DefaultNonNull
interface Map<K extends @Nullable Object, V extends @Nullable Object> {}

Map is now annotated with @DefaultNonNull. It would be sort of nice for this to make methods like keySet return a @NonNull Set<K>, but as above, it would be bad for it to make get return a K instead of @NullnessUnspecified K.

I think we'd want to apply the same rule: If a method isn't present in the stub file, then we analyze it as if the stub file doesn't exist.

This could extend even beyond methods and other members. The the following stub file:

@DefaultNonNull
class Equivalence<T> {}

The above stub file omits the implements clause from the class declaration. A properly annotated implements clause for the class would look like this:

class Equivalence<T> implements BiPredicate<@Nullable T, @Nullable T> { ... }

Thus, I would say that we have to assume that Equivalence<T> extends BiPredicate<@NullnessUnspecified T, @NullnessUnspecified T>, rather than incorrectly assuming BiPredicate<T, T>. (That incorrect assumption would at least turn out to be a conservative one in this case, but that isn't guaranteed in general.)

Final note: One notable case in which we may see incomplete stubs is when a user writes stubs for a whole class but then the class is later expanded to add more methods.

Activity

  1. kevin1e100 commented on Jan 25, 2020

    @kevin1e100
    Collaborator

    for more fun, what if the underlying class is annotated? What if methods not listed in the stub are annotated? May be the topic of another issue.

  2. kevinb9n commented on Jun 25, 2021

    @kevinb9n
    Collaborator

    Supposing the stub had been complete, but then Map itself added a new method (like it does from time to time). What do we want to happen then?

    If JSpecify adoption reaches steady-state for a library, then it's this use case that will dominate thereafter, right?

  3. added
    stub-files
    on Oct 13, 2021
  4. added this to the Release-independent milestone on Oct 13, 2021
  5. cpovirk commented on Oct 28, 2022

    @cpovirk
    CollaboratorAuthor

    (#310 makes a case for treating such APIs as @NullUnmarked.)

  6. added
    design
    on Mar 13, 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

    designstub-files

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions