NullAway icon indicating copy to clipboard operation
NullAway copied to clipboard

support models of generic types

Open msridhar opened this issue 8 years ago • 2 comments

As a baby step toward support of generics, we should support writing models of generic library types and checking uses of those types against the models. E.g., for a Pair<S, T> type, we should be able to write a model indicating the type is really Pair<@PolyNull S, @PolyNull T> and then allowing app code to write, e.g., Pair<@Nullable String, String>. We would not initially check that the implementation of the Pair class respects the model. Depending on implementation difficulty of figuring out how generic types are instantiated, we may require the models to be more or less explicit.

msridhar avatar Nov 07 '17 01:11 msridhar

Couple notes based on some initial tinkering:

  • We'll need to handle assignments, e.g., you can't assign a Pair<@Nullable String, String> to Pair<String, String> (with both @NonNull). Probably just want to support invariance.
  • Without inference, ergonomics will be a bit rough. E.g., if you write Pair<@Nullable String, String> p = new Pair<>(null, "hello"); I don't think javac's inference will tell us that the first type parameter for the new expression is @Nullable. Would be a bit unfortunate to have to write Pair<@Nullable String, String> p = new Pair<@Nullable String, String>(null, "hello"); but supporting inference could be a lot more work.

Overall I think adding basic support (even without inference) will be a moderate amount of work to get right.

msridhar avatar Nov 07 '17 18:11 msridhar

FWIW Error Prone has some support for generics and null checking now, implemented by @bennostein. See the inference code and this commit. Perhaps we could do something similar in NullAway? @lazaroclapp

msridhar avatar Aug 28 '18 18:08 msridhar