[ 
https://issues.apache.org/jira/browse/GROOVY-12783?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12783:
-------------------------------
    Component/s: Static compilation

> SC: a lambda or method reference returning to a functional interface that 
> declares a String does not convert the value
> ----------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12783
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12783
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>            Reporter: Paul King
>            Priority: Major
>
> The type checker accepts a value of another type where a lambda, closure, 
> method reference or method pointer returns to a functional interface that 
> declares a {{String}}, as it does for an assignment to a {{String}} or the 
> {{return}} of a method declared to return one. For an assignment or a 
> {{return}} the value is then converted. For a lambda or a method reference 
> under {{@CompileStatic}} it is not, so the functional interface gives a value 
> that is not a {{String}}.
> {code:groovy}
> import java.util.function.Supplier
> @groovy.transform.CompileStatic
> class C {
>     // reads the value with no static type for it
>     static String given(Supplier s) { Object o = s.get(); 
> o.getClass().simpleName }
>     static main(args) {
>         def n = 1
>         Supplier<String> v = () -> "a$n"
>         println given(v)
>     }
> }
> {code}
> *Actual*, with 6.0.0 and master: {{GStringImpl}}.
> *Expected*: {{String}}, as for {{String s = "a$n"}} and for {{String m() { 
> "a$n" } }}.
> The same holds for a value that is not a {{GString}}:
> {code:groovy}
> static <T extends Number> T zero() { (T) Integer.valueOf(0) }
> ...
> Supplier<String> v = () -> C.zero()     // gives an Integer
> Supplier<String> w = C::zero            // gives an Integer
> {code}
> What a caller then sees depends on how it reads the value:
> ||Read||6.0.0||master||
> |{{given(v)}}, no static type|{{GStringImpl}}|{{GStringImpl}}|
> |{{Object o = v.get()}}|{{String}}|{{GStringImpl}}|
> |{{String s = v.get()}}|{{String}}|{{String}}|
> |{{v.get().length()}}|{{ClassCastException}}|2|
> The two rows that differ follow from GROOVY-12469, which no longer converts a 
> value on assignment to a wider variable and converts a {{GString}} where a 
> method of {{String}} is called. That change is to how such a value is read. 
> This issue is that the value is produced with the wrong type. For a value 
> that is not a {{GString}}, {{v.get().length()}} is a {{ClassCastException}} 
> in both.
> By form, under {{@CompileStatic}} / {{@TypeChecked}}, for {{Supplier<String> 
> v = ...}} giving a {{Number}}:
> ||Form||@CompileStatic||@TypeChecked||
> |closure, {{ { -> C.zero() } }}|{{String}}|{{String}}|
> |lambda, {{() -> C.zero()}}|{{Integer}}|{{String}}|
> |method reference, {{C::zero}}|{{Integer}}|{{Integer}}|
> |method pointer, {{C.&zero}}|{{Integer}}|{{Integer}}|
> h3. Proposal
> Treat the return to a functional interface as the conversion that the type 
> checker already takes it to be: the value is converted as by a method 
> declared to return the type that the functional interface declares. 
> GROOVY-12616, GROOVY-12617 and GROOVY-12623 did this for a functional 
> interface whose method returns a primitive type.
> A prototype for lambdas declares the generated {{doCall}} method to return 
> {{String}} where the functional interface type that the lambda is compiled 
> for declares one, so the existing conversion of a returned value applies. 
> With it:
> * each lambda above gives a {{String}}, as do {{Function<Integer, String>}}, 
> {{UnaryOperator<String>}}, {{Callable<String>}}, a lambda passed as an 
> argument, a block body with several {{return}} statements and a serializable 
> lambda;
> * a {{String}} and {{null}} are returned as they are;
> * {{Supplier<CharSequence>}} and {{Supplier<Object>}} still give the 
> {{GString}};
> * {{Supplier<List<String>> v = () -> ["a$n"]}} is still a compile error: a 
> type argument within the returned type is not converted.
> Of 9,324 tests in the type checking, static compilation, lambda and method 
> reference suites, the 21 that fail assert the descriptor of the generated 
> method, {{doCall()Ljava/lang/Object;}}.
> h3. To decide
> * *Method references and pointers.* Either convert, with the adapter that 
> GROOVY-12617 uses for a primitive return type, or reject them, as the method 
> does not return what the functional interface declares. A lambda has a body 
> in which to convert; a reference does not.
> * *{{Boolean}}.* {{Supplier<Boolean> v = () -> 'x'}} is a compile error, 
> while {{Predicate<String> p = s -> s}} and {{boolean b = 'x'}} convert by 
> Groovy truth, and {{Supplier<Boolean> v = 'x'::toString}} compiles and gives 
> a {{String}}. The same rule would cover these.
> * *Through a generic call.* {{Supplier<String> v = toSupplier(() -> 
> C.zero())}} is a compile error in 6.0.0 and master, as the target gives the 
> type parameter of {{toSupplier}} a {{String}} and the result of the lambda is 
> then checked against it as a type argument. If the return is a conversion, 
> this would compile as the direct assignment does.
> * *GROOVY-12469.* For a lambda, {{Object o = v.get()}} would be a {{String}} 
> again, as the value would be one.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to