Bug 576649 - Null analysis inconsistent between method reference and lambda
Summary: Null analysis inconsistent between method reference and lambda
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.21   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2021-10-15 09:54 EDT by Lukas Eder CLA
Modified: 2023-10-06 05:01 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Eder CLA 2021-10-15 09:54:25 EDT
I've declared these three annotations (but any other probably does the trick):

// -------------------------------------
package p;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
public @interface NotNull {}
// -------------------------------------
package p;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
public @interface NotNullByDefault {}
// -------------------------------------
package p;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
public @interface Nullable {}
// -------------------------------------

Now, when I enable annotation based null analysis and violation of null specification, then the following doesn't compile:

// -------------------------------------
package p;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class Test {
    <T> void m1(Function<? super Test, ? extends T> f) {
    }

    void m2() {
        m1(Test::m3);
        m1(t -> t.m3());
    }

    @NotNull List<?> m3() {
        return new ArrayList<>();
    }
}
// -------------------------------------

There's an error on the method reference that doesn't appear on the lambda:

> Null type mismatch at method return type: Method descriptor Function<Test,List<capture#1-of ?>>.apply(Test) promises '@NotNull List<capture#of ?>' but referenced method provides '@NotNull List<?>'

The error disappears when I'm not using wildcards. Both of these work:

// -------------------------------------
package p;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class Test<X> {
    <T> void m1(Function<? super Test<?>, ? extends T> f) {
    }

    void m2() {
        m1(Test::m3);
        m1(t -> t.m3());
    }

    @NotNull List<X> m3() {
        return new ArrayList<>();
    }
}
// -------------------------------------
package p;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class Test {
    <T> void m1(Function<? super Test, ? extends T> f) {
    }

    void m2() {
        m1(Test::m3);
        m1(t -> t.m3());
    }

    <X> @NotNull List<X> m3() {
        return new ArrayList<>();
    }
}
// -------------------------------------
Comment 1 Eclipse Genie CLA 2023-10-06 05:01:19 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.