Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle uninferred type parameter in array accesses (Fixes #3020) #3107

Merged
merged 3 commits into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 instanceof AnnotatedArrayType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this is TypeKinds instead of instanceof?

return ((AnnotatedArrayType) type).getComponentType();
} else if (type instanceof AnnotatedWildcardType) {
// Clean-up after Issue #979.
AnnotatedWildcardType wc = (AnnotatedWildcardType) type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect this to be an uninferred type argument? If so should we check that it is?

AnnotatedTypeMirror wcbound = wc.getExtendsBound();
if (wcbound instanceof AnnotatedArrayType) {
return ((AnnotatedArrayType) wcbound).getComponentType();
}
}
throw new BugInCF("Unexpected type: " + type);
} finally {
f.visitorState.setAssignmentContext(preAssCtxt);
}
Expand Down