Repository navigation
Description
An explicitly typed lambda can declare a non-null parameter where the functional interface method, declared @NullUnmarked, has an explicit @Nullable parameter, and NullAway reports nothing. An anonymous class, a method reference, and an implicitly typed lambda implementing the same method are all reported.
Version: master at 5ba2b54 (0.14.3-SNAPSHOT), JDK 21.0.9. Not tested on the 0.14.2 release.
Options:
-XepOpt:NullAway:AnnotatedPackages=com.uber
-XepOpt:NullAway:JSpecifyMode=true
-XDaddTypeAnnotationsToSymbol=true
The result is the same with -XepOpt:NullAway:OnlyNullMarked=true in place of AnnotatedPackages, and with -XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true in place of JSpecifyMode=true.
Reproducer (run as a CompilationTestHelper test extending NullAwayTestsBase):
package com.uber;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
interface Fi {
@NullUnmarked
void apply(@Nullable Object o);
}
static void take(Object o) {}
void test() {
Fi anon = new Fi() {
@Override
public void apply(Object o) {}
};
Fi ref = Test::take;
Fi implicit = o -> o.toString();
Fi explicit = (Object o) -> o.toString();
}
}Actual: three warnings, none on line 19 (Fi explicit = (Object o) -> o.toString();):
/Test.java:15: warning: [NullAway] parameter o is @NonNull, but parameter in superclass method com.uber.Test.Fi.apply(java.lang.@org.jspecify.annotations.Nullable Object) is @Nullable
/Test.java:17: warning: [NullAway] parameter o of referenced method is @NonNull, but parameter in functional interface method com.uber.Test.Fi.apply(@Nullable Object) is @Nullable
/Test.java:18: warning: [NullAway] dereferenced expression 'o' is @Nullable
Expected: a fourth warning on line 19, parameter o is @NonNull, but parameter in functional interface method com.uber.Test.Fi.apply(@Nullable Object) is @Nullable, the message NullAway already gives for the same lambda when apply is not @NullUnmarked. JSpecify mode always acknowledges restrictive annotations in unmarked code (ErrorProneCLIFlagsConfig.acknowledgeRestrictiveAnnotations()), and the other three implementations on lines 15, 17, and 18 follow that.
Scope:
- A named class
class Impl implements Fiis reported like the anonymous class. - Nested nullness diverges the same way. With
@NullUnmarked void apply(@Nullable Object @NonNull... inputs),@NullUnmarked void apply(@Nullable Object[] inputs), or@NullUnmarked void apply(List<@Nullable Object> l), an anonymous class whose parameter isObject[]orList<Object>getsParameter has type Object [], but overridden method has parameter type @Nullable Object [], which has mismatched type parameter nullability(or theListequivalent), and the explicitly typed lambda gets nothing. The existing testJSpecifyVarargsTests#restrictiveAnnotationsVarargsLambdaUsesArrayNullnessasserts no warning for the varargs lambda. - For nested nullness, explicitly typed lambdas are not checked even when the interface method is
@NullMarked; open PR Check nested nullness for functional interface implementations #1739 adds that check insidecheckParamOverriding, which the@NullUnmarkedcase never reaches.
Suspected location (a guess; the report stands without it): NullAway.matchLambdaExpression returns NO_MATCH when codeAnnotationInfo.isSymbolUnannotated(funcInterfaceMethod, ...) holds, before calling checkParamOverriding. That skips the onOverrideMethodInvocationParametersNullability handler call through which RestrictiveAnnotationHandler marks the @Nullable parameter for the anonymous class and the method reference.
Found by rewriting a lambda from NullAway's own test suite into an equivalent anonymous class.
Activity
Check explicitly typed lambda parameters against unmarked interface m…
Check nested nullness of explicitly typed lambda parameters (#2015)
Run crash and metamorphic oracles over NullAway's test sources on master