Skip to content

Commit

Permalink
fix explicitly-typed lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed Oct 10, 2024
1 parent 9dcac6b commit 25ad62a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public static Stream<? extends AnnotationMirror> getTypeUseAnnotations(
Symbol typeAnnotationOwner;
switch (symbol.getKind()) {
case PARAMETER:
// use the symbol's owner for parameters, unless it's the parameter of a lambda
typeAnnotationOwner = symbol.owner;
break;
default:
Expand Down Expand Up @@ -344,8 +345,16 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi
case PARAMETER:
switch (position.type) {
case METHOD_FORMAL_PARAMETER:
return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym)
== position.parameter_index;
int parameterIndex = position.parameter_index;
if (position.onLambda != null) {
com.sun.tools.javac.util.List<JCTree.JCVariableDecl> lambdaParams =
position.onLambda.params;
return parameterIndex < lambdaParams.size()
&& lambdaParams.get(parameterIndex).sym.equals(sym);
} else {
return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym)
== parameterIndex;
}
default:
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/Java8Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@ public void methodReferenceOnNullableVariable() {
"}")
.doTest();
}

@Test
public void testNullableLambdaParamTypeUse() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" @FunctionalInterface",
" interface NullableParamFunctionTypeUse<T, U> {",
" U takeVal(@org.jspecify.annotations.Nullable T x);",
" }",
" static void testParamTypeUse() {",
" NullableParamFunctionTypeUse n3 = (@org.jspecify.annotations.Nullable Object x) -> (x == null) ? \"null\" : x.toString();",
" NullableParamFunctionTypeUse n4 = (x) -> (x == null) ? \"null\" : x.toString();",
" }",
"}")
.doTest();
}
}

0 comments on commit 25ad62a

Please sign in to comment.