From 559ba69c20d5e2f2b828032ee82a996f3a949eee Mon Sep 17 00:00:00 2001 From: Chris Povirk Date: Tue, 19 Jan 2021 14:10:14 -0500 Subject: [PATCH] Handling casting from a non-nullable wildcard capture to a type variable. --- .../jspecify/nullness/NullSpecVisitor.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/google/jspecify/nullness/NullSpecVisitor.java b/src/main/java/com/google/jspecify/nullness/NullSpecVisitor.java index ead54f9..56253d7 100644 --- a/src/main/java/com/google/jspecify/nullness/NullSpecVisitor.java +++ b/src/main/java/com/google/jspecify/nullness/NullSpecVisitor.java @@ -119,6 +119,22 @@ private void ensureNonNull(Tree tree, String messageKey) { } } + @Override + protected boolean isTypeCastSafe(AnnotatedTypeMirror castType, AnnotatedTypeMirror exprType) { + /* + * TODO(cpovirk): I probably need to completely revisit this method to understand what it does + * and what the Right Way is to reconcile it with our non-standard treatment of wildcards and + * type variables. + * + * For now, the supermethod reports errors for some problematic casts (like from Object to int) + * but also reports errors for some safe casts (like from ? to T when the wildcard's + * corresponding type parameter doesn't permit null values). As an expedient measure, we + * short-circuit the check in the second case. + */ + return atypeFactory.isNullExclusiveUnderEveryParameterization(exprType) + || super.isTypeCastSafe(castType, exprType); + } + @Override protected String extraArgForReturnTypeError(Tree tree) { /*