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

error: [methodref.return] Incompatible return type #6071

Closed
rionda opened this issue Jul 1, 2023 · 1 comment
Closed

error: [methodref.return] Incompatible return type #6071

rionda opened this issue Jul 1, 2023 · 1 comment
Assignees

Comments

@rionda
Copy link

rionda commented Jul 1, 2023

Consider the following classes:

MapEntryGetValueWorks:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class MapEntryGetWorks {
    public static void main(String[] args) {
        ConcurrentMap<Integer, List<List<Integer>>> m =
                IntStream.range(0, 100)
                        .mapToObj(i -> List.of(i, i  % 2))
                        .collect(Collectors.groupingByConcurrent(l -> ((List<Integer>) l).get(1)));

        ArrayList<Integer> list = m.entrySet()
                        .stream()
                        .sorted(Map.Entry.comparingByKey())
                        .mapToInt(Map.Entry::getKey)
                        .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    }
}

and MapEntryGetFails

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class MapEntryGetFails {

    public static void main(String[] args) {
        ArrayList<Integer> list = IntStream.range(0, 100)
                .mapToObj(i -> List.of(i, i  % 2))
                .collect(Collectors.groupingByConcurrent(l -> ((List<Integer>) l).get(1)))
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByKey())
                .mapToInt(Map.Entry::getKey)
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    }
}

They do the same thing, but the Works one split the code in two statements, which is not really needed.

When running ~/checker-framework-3.35.0/checker/bin/javac -processor Nullness src/main/java/MapEntryGetWorks.java, I get no errors.

When running ~/checker-framework-3.35.0/checker/bin/javac -processor Nullness src/main/java/MapEntryGetFails.java, I get

src/main/java/MapEntryGetFails.java:16: error: [methodref.return] Incompatible return type
                .mapToInt(Map.Entry::getKey)
                          ^
  found   : /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object
  required: @Initialized @NonNull int
  Consequence: method in @Initialized @NonNull Entry</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object>
    /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object getKey(@Initialized @NonNull Entry</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object> this)
  is not a valid method reference for method in @Initialized @NonNull ToIntFunction<@Initialized @NonNull Entry</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object>>
    @Initialized @NonNull int applyAsInt(@Initialized @NonNull ToIntFunction<@Initialized @NonNull Entry</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object>> this, @Initialized @NonNull Entry</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object> p0)
1 error

One can build similar error cases using Map.Entry::getValue instead of getKey.

I'm not sure if it is a bug.

@smillst
Copy link
Member

smillst commented Jan 12, 2024

This test case now passes in master.

@smillst smillst closed this as completed Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants