Skip to content

Commit

Permalink
getContainingMethod returns null for lambdas; fixes issue #3850
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored Nov 5, 2020
1 parent 1791802 commit 2aebf2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -202,17 +203,18 @@ protected TransferResult<NullnessValue, NullnessStore> strengthenAnnotationOfEqu
if (nullnessTypeFactory.containsSameByClass(secondAnnos, PolyNull.class)) {
thenStore = thenStore == null ? res.getThenStore() : thenStore;
elseStore = elseStore == null ? res.getElseStore() : elseStore;
// TODO: methodTree is null for lambdas. Handle that case. See Issue3850.java.
MethodTree methodTree = analysis.getContainingMethod(secondNode.getTree());
ExecutableElement methodElem =
TreeUtils.elementFromDeclaration(
analysis.getContainingMethod(secondNode.getTree()));
methodTree == null ? null : TreeUtils.elementFromDeclaration(methodTree);
if (notEqualTo) {
elseStore.setPolyNullNull(true);
if (polyNullIsNonNull(methodElem, thenStore)) {
if (methodElem != null && polyNullIsNonNull(methodElem, thenStore)) {
thenStore.setPolyNullNonNull(true);
}
} else {
thenStore.setPolyNullNull(true);
if (polyNullIsNonNull(methodElem, elseStore)) {
if (methodElem != null && polyNullIsNonNull(methodElem, elseStore)) {
elseStore.setPolyNullNonNull(true);
}
}
Expand Down
15 changes: 15 additions & 0 deletions checker/tests/nullness/Issue3850.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.checkerframework.checker.nullness.qual.PolyNull;

class Issue3850 {

private static Iterable<@PolyNull String> toPos(Iterable<? extends @PolyNull Object> nodes) {
// :: error: (return.type.incompatible)
return transform(nodes, node -> node == null ? null : node.toString());
}

public static <F, T> Iterable<T> transform(
Iterable<? extends F> iterable,
java.util.function.Function<? super F, ? extends T> function) {
throw new Error("implementation is irrelevant");
}
}

0 comments on commit 2aebf2e

Please sign in to comment.