[
https://issues.apache.org/jira/browse/GROOVY-12822?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149478#comment-18149478
]
ASF GitHub Bot commented on GROOVY-12822:
-----------------------------------------
codeconsole opened a new pull request, #3336:
URL: https://github.com/apache/groovy/pull/3336
[GROOVY-12822](https://issues.apache.org/jira/browse/GROOVY-12822)
Under `@CompileStatic`, a conditional expression whose branches are lambdas
or method references compiled and then failed when assigned or returned to a
functional interface:
```groovy
@CompileStatic
Supplier<String> make(boolean flag) {
Supplier<String> s = flag ? () -> 'a' : () -> 'b'
s
}
make(true).get() // GroovyCastException: Cannot cast object
'...$$Lambda/...' to class 'groovy.lang.Closure'
```
The same held for a conditional expression returned or assigned to a
subscript, property, field or list element, for method reference branches and
for a lambda with a closure. Passed as a method argument it worked.
The type checker types the conditional expression as the interface and gives
each lambda the interface, so each lambda is compiled as an instance of it.
`BinaryExpressionHelper` takes the common type of the branches from the
`TypeChooser`, which answered with their inferred type, `Closure` for a lambda,
and cast each branch to it. `StaticTypesTypeChooser` now resolves a lambda or
method reference that has a functional interface as its parameter type, which
is what `StaticTypesLambdaWriter` and
`StaticTypesMethodReferenceExpressionWriter` compile it to, to that interface.
Assigned to a subscript, a conditional expression with a branch of the
interface type and a lambda or method reference, such as `map['k'] = flag ? f :
(String s) -> s.trim()` or `map['k'] = f ?: (String s) -> s.trim()`, did not
compile: `putAt` was checked with `Object`. The type checker visits the value
of a subscript assignment before the assignment, and there the lambda got no
target type, so the common type of the interface and `Closure` was `Object`.
That visit now takes the subscript's type as the target type of a branch, and a
method reference branch takes the interface as its type, as a lambda branch
does.
## Tests
`TernaryOperatorStaticCompileTest` gets two tests, which fail without the
change: lambda, method reference and mixed branches assigned to a local
variable, subscript, property, field and list element, returned, and through an
Elvis operator; and a branch of the interface type with a lambda assigned to a
subscript and a property. The full `:test` suite and the lint gate pass. The
ternary tests also pass with `-Pgroovy.target.lambda.hoist=true
-Pgroovy.target.closure.pack=true`.
Seen in 6.0.0 and master.
> SC: a conditional expression whose branches are lambdas or method references
> fails when assigned or returned to a functional interface
> --------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12822
> URL: https://issues.apache.org/jira/browse/GROOVY-12822
> Project: Groovy
> Issue Type: Bug
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.Supplier
> @groovy.transform.CompileStatic
> Supplier<String> make(boolean flag) {
> Supplier<String> s = flag ? () -> 'a' : () -> 'b'
> s
> }
> assert make(true).get() == 'a'
> {code}
> This compiles with 6.0.0 and master, and then fails:
> {noformat}
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
> object 'Script1$$Lambda/0x...' with class 'Script1$$Lambda/0x...' to class
> 'groovy.lang.Closure'
> {noformat}
> The same happens when such a conditional expression is returned, or assigned
> to a subscript, a property, a field or a list element, and when its branches
> are method references or a lambda and a closure:
> {code:groovy}
> flag ? String::toUpperCase : String::trim
> flag ? (String s) -> s + '!' : { String s -> s }
> {code}
> Passed as a method argument, it works.
> The type checker types the conditional expression as the interface and gives
> each lambda the interface, so each lambda is compiled as an instance of it.
> The class generator takes the common type of the branches from their own
> inferred types, which is Closure for a lambda, and casts each branch to it.
> Assigned to a subscript, a conditional expression with a branch of the
> interface type and a lambda or method reference does not compile:
> {code:groovy}
> import java.util.function.Function
> @groovy.transform.CompileStatic
> Map<String, Function<String, String>> make(Function<String, String> f,
> boolean flag) {
> Map<String, Function<String, String>> map = [:]
> map['ternary'] = flag ? f : (String s) -> s.trim()
> map['elvis'] = f ?: (String s) -> s.trim()
> map
> }
> {code}
> {noformat}
> [Static type checking] - Cannot call <V>
> org.codehaus.groovy.runtime.DefaultGroovyMethods#putAt(java.util.Map<? super
> java.lang.String, V>, java.lang.String, V) with arguments
> [java.util.LinkedHashMap<java.lang.String,
> java.util.function.Function<java.lang.String, java.lang.String>>,
> java.lang.String, java.lang.Object]
> {noformat}
> For a subscript, the type checker visits the value before the assignment,
> where the lambda gets no target type, so the branches' common type is Object.
> Assigned to a local variable, the same expression compiles.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)