Skip to content

Commit

Permalink
Handle uninferred type parameter in array accesses (Fixes #3020) (#3107)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdietl committed Feb 27, 2020
1 parent c3d7f6c commit 24bcf17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions checker/tests/nullness/Issue3020.java
@@ -0,0 +1,17 @@
enum Issue3020 {
INSTANCE;

void retrieveConstant() {
Class<?> theClass = Issue3020.class;
// :: error: (accessing.nullable)
Object unused = passThrough(theClass.getEnumConstants())[0];
}

void nonNullArray(String[] p) {
Object unused = passThrough(p)[0];
}

<T> T passThrough(T t) {
return t;
}
}
Expand Up @@ -36,6 +36,7 @@
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType;
import org.checkerframework.framework.type.visitor.AnnotatedTypeMerger;
import org.checkerframework.framework.util.AnnotatedTypes;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.Pair;
import org.checkerframework.javacutil.TreeUtils;

Expand Down Expand Up @@ -224,8 +225,17 @@ public AnnotatedTypeMirror visitArrayAccess(ArrayAccessTree node, AnnotatedTypeF
f.visitorState.setAssignmentContext(null);

AnnotatedTypeMirror type = f.getAnnotatedType(node.getExpression());
assert type instanceof AnnotatedArrayType;
return ((AnnotatedArrayType) type).getComponentType();
if (type.getKind() == TypeKind.ARRAY) {
return ((AnnotatedArrayType) type).getComponentType();
} else if (type.getKind() == TypeKind.WILDCARD
&& ((AnnotatedWildcardType) type).isUninferredTypeArgument()) {
// Clean-up after Issue #979.
AnnotatedTypeMirror wcbound = ((AnnotatedWildcardType) type).getExtendsBound();
if (wcbound instanceof AnnotatedArrayType) {
return ((AnnotatedArrayType) wcbound).getComponentType();
}
}
throw new BugInCF("Unexpected type: " + type);
} finally {
f.visitorState.setAssignmentContext(preAssCtxt);
}
Expand Down

0 comments on commit 24bcf17

Please sign in to comment.