Skip to content

Commit

Permalink
[fixes projectlombok#3225] Handle ambiguous extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed Mar 1, 2024
1 parent 8bea251 commit 091c9a3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2023 The Project Lombok Authors.
* Copyright (C) 2012-2024 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -321,9 +321,11 @@ public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend meth
MethodBinding fixedBinding = scope.getMethod(extensionMethod.declaringClass, methodCall.selector, argumentTypes.toArray(new TypeBinding[0]), methodCall);
if (fixedBinding instanceof ProblemMethodBinding) {
methodCall.arguments = originalArgs;
if (fixedBinding.declaringClass != null) {
PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope);
// Sometimes the declaring class is null, in that case we have to create a new ProblemMethodBinding using the extension method's declaring class
if (fixedBinding.declaringClass == null) {
fixedBinding = new ProblemMethodBinding(fixedBinding.selector, fixedBinding.parameters, extensionMethod.declaringClass, fixedBinding.problemId());
}
PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope);
} else {
// If the extension method uses varargs, the last fixed binding parameter is an array but
// the method arguments are not. Even thought we already know that the method is fine we still
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//skip-idempotent
import java.util.function.Consumer;
import java.util.function.Function;

class ExtensionMethodAmbiguousFunctional {
public void test() {
ExtensionMethodAmbiguousFunctional.Extensions.ambiguous("", System.out::println);
}

static class Extensions {
public static <T, R> void ambiguous(T t, Function<T, R> function) {
}

public static <T> void ambiguous(T t, Consumer<T> function) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.function.Consumer;
import java.util.function.Function;
import lombok.experimental.ExtensionMethod;
@ExtensionMethod({ExtensionMethodAmbiguousFunctional.Extensions.class}) class ExtensionMethodAmbiguousFunctional {
static class Extensions {
Extensions() {
super();
}
public static <T, R>void ambiguous(T t, Function<T, R> function) {
}
public static <T>void ambiguous(T t, Consumer<T> function) {
}
}
ExtensionMethodAmbiguousFunctional() {
super();
}
public void test() {
"".ambiguous(System.out::println);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//version 8:
import java.util.function.Consumer;
import java.util.function.Function;

import lombok.experimental.ExtensionMethod;

@ExtensionMethod({ExtensionMethodAmbiguousFunctional.Extensions.class})
class ExtensionMethodAmbiguousFunctional {
public void test() {
"".ambiguous(System.out::println);
}

static class Extensions {
public static <T, R> void ambiguous(T t, Function<T, R> function) {
}

public static <T> void ambiguous(T t, Consumer<T> function) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9 reference to ambiguous is ambiguous both method <T,R>ambiguous(T,java.util.function.Function<T,R>) in ExtensionMethodAmbiguousFunctional.Extensions and method <T>ambiguous(T,java.util.function.Consumer<T>) in ExtensionMethodAmbiguousFunctional.Extensions match
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10 The method ambiguous(String, Function<String,Object>) is ambiguous for the type ExtensionMethodAmbiguousFunctional.Extensions

0 comments on commit 091c9a3

Please sign in to comment.