codeconsole opened a new pull request, #3290: URL: https://github.com/apache/groovy/pull/3290
https://issues.apache.org/jira/browse/GROOVY-12757 When a generic method of a generic receiver is called with a lambda whose result names a type parameter of the calling method, and the receiver's class has a type parameter of the same name, static type checking replaced the caller's type parameter with the receiver's type argument: ```groovy static <T> Optional<List<T>> wrap(Optional<String> value, T item) { Optional<List<T>> result = value.map((String s) -> [item] as List<T>) result } ``` ``` [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Optional<java.util.List<java.lang.String>> to: java.util.Optional<java.util.List<T>> ``` `map`'s `U` is inferred from the lambda as `List<T>`, with the calling method's `T`, and then `Optional`'s own `T`, here `String`, was applied to the result. Groovy before GROOVY-12714 inferred the same wrong type, but its assignment check accepted it. javac accepts the equivalent Java. Grails no longer compiles against GROOVY_6_0_X because of this: `AwaitablePromiseFactory` calls `groovy.concurrent.Awaitable#then` that way. `inferReturnTypeGenerics` resolved the method's type parameters from the arguments first, and then applied the receiver's type arguments to the result. It now applies the receiver's type arguments to the declared return type first, which keeps them away from the types the arguments supply. `extractPlaceHoldersVisibleToDeclaration` already leaves out the method's own type parameters (GROOVY-10322), so a method type parameter that shadows the class's is unaffected. `GenericsJavaCompatibilityTest` gets a case checked against javac and `GenericsSTCTest`, and so `GenericsStaticCompileTest`, the Groovy form. Both fail without the change. Please also apply this to GROOVY_6_0_X, where it cherry-picks cleanly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
