You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR improves lambda-related method resolution by using the lambda parameter count and body for disambiguation in two cases described below. I implemented this by adding the required information to the LambdaArgumentTypePlaceholder class, but made sure that, if this information is not provided (such as for MethodReferenceExpr, type resolution behaviour will be the same as before this PR.
Consumer.accept has one parameter while Runnable.run has zero, so since the lambda has one parameter, we know this must resolve to foo(Consumer<String>)
In this example, the body of the lambda is a block statement which does not contain any return statements. This means that the lambda can only define a method with a void return type, so it must define Consumer<String> (the same would be true for foo(input -> { return; }).
If it were foo(input -> { return ""; }) instead, then the reverse logic would apply. Since the return type of the lambda is not void, it cannot define Consumer and must therefore define Function.
I don't understand why the codecov/patch check is failing. The tests are covered, so the logic it's complaining about must be too, otherwise the tests don't pass
johannescoetzee
changed the title
Use lambda parameter counts and block bodies for improved resolution
Draft: Use lambda parameter counts and block bodies for improved resolution
May 19, 2025
This PR might introduce a new crash. I've changed it to a draft while I look into this, so this shouldn't be merged until I've either fixed the new crash or it turns out to be something else.
johannescoetzee
changed the title
Draft: Use lambda parameter counts and block bodies for improved resolution
Use lambda parameter counts and block bodies for improved resolution
May 20, 2025
I've found the source of the new crash and added the lambdaUsedAsPolymorphicArgument to test it. The issue was that by the time resolution got to the isConflictingLambdaType method, type variables have not been substituted yet, so LambdaInterfaceLogic.getFunctionalMethod crashed with an UnsupportedOperationException when attempting to find the functional interface for the type variable. I've fixed this by simply not rejecting the candidate in this case (which would be the behaviour before this PR), but this does still result in a MethodAmbiguityException in some cases. I've added a disabled test lambdaUsedAsOverloadedPolymorphicArgument1 to illustrate this. In my opinion, handling this case as well would be better left for a separate PR as it would require a deeper dive into the whole resolution process to avoid doing something that would result in duplicate solving tasks.
I found another deeper issue that comes up when trying to find the corresponding functional interface, so I'm going to close this PR until I've fixed this one and am confident that there aren't any more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves lambda-related method resolution by using the lambda parameter count and body for disambiguation in two cases described below. I implemented this by adding the required information to the
LambdaArgumentTypePlaceholderclass, but made sure that, if this information is not provided (such as forMethodReferenceExpr, type resolution behaviour will be the same as before this PR.Different parameter counts
Consumer.accepthas one parameter whileRunnable.runhas zero, so since the lambda has one parameter, we know this must resolve tofoo(Consumer<String>)Disambiguation by return type
In this example, the body of the lambda is a block statement which does not contain any return statements. This means that the lambda can only define a method with a
voidreturn type, so it must defineConsumer<String>(the same would be true forfoo(input -> { return; }).If it were
foo(input -> { return ""; })instead, then the reverse logic would apply. Since the return type of the lambda is notvoid, it cannot defineConsumerand must therefore defineFunction.