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

Correct type argument inference for method references with var args. #6692

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions framework/tests/all-systems/Issue6664.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public abstract class Issue6664<A, B> {

public static <A, B> Collector<Issue6664<A, B>, ?, Issue6664<A, List<B>>> breakIt() {
return Collectors.reducing(
Eithers.right(new ArrayList<>()),
either -> either.map(Arrays::asList),
(either, eithers) -> either);
}

public <C> Issue6664<A, C> map(Function<B, C> function) {
throw new RuntimeException();
}

public static final class Eithers {
public static <A, B> Issue6664<A, B> left(A left) {
throw new RuntimeException();
}

public static <A, B> Issue6664<A, B> right(B right) {
throw new RuntimeException();
}
}
}
29 changes: 29 additions & 0 deletions framework/tests/all-systems/Issue6664B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public abstract class Issue6664B {

@SuppressWarnings("purity.methodref")
void method2() {
// Doesn't use variable arity for method applicability.
Function<Object[], List<Object>> f1 = Arrays::asList;

// Equivalent to Arrays.asList(obj).
Function<Object, List<Object>> f2 = Arrays::asList;
// Equivalent to Arrays.asList(o1,o2,o3).
Foo f3 = Arrays::asList;
// Equivalent to Arrays.asList().
Foo2 f4 = Arrays::asList;
}

interface Foo {

List<Object> apply(Object o1, Object o2, Object o3);
}

interface Foo2 {

List<Object> apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,17 @@ public static boolean isVariableTreeDeclaredUsingVar(VariableTree variableTree)
return type != null && type.pos == Position.NOPOS;
}

/**
* Returns true if the given method reference has a varargs formal parameter.
*
* @param methref a method reference
* @return if the given method reference has a varargs formal parameter
*/
public static boolean hasVarargsParameter(MemberReferenceTree methref) {
JCMemberReference jcMethoRef = (JCMemberReference) methref;
return jcMethoRef.varargsElement != null;
}

/**
* Returns true if the given method/constructor invocation is a varargs invocation.
*
Expand All @@ -2582,6 +2593,8 @@ public static boolean isVarargsCall(Tree tree) {
return isVarargsCall((MethodInvocationTree) tree);
case NEW_CLASS:
return isVarargsCall((NewClassTree) tree);
case MEMBER_REFERENCE:
return hasVarargsParameter((MemberReferenceTree) tree);
default:
return false;
}
Expand Down