You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/04/19 15:31:00 UTC

[jira] [Resolved] (GROOVY-10972) Issue in resolving overloaded method reference

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

Eric Milles resolved GROOVY-10972.
----------------------------------
    Resolution: Fixed

https://github.com/apache/groovy/commit/313f2bf3c0fa70d7a9fdc105bf42f96ccbab4894

> Issue in resolving overloaded method reference
> ----------------------------------------------
>
>                 Key: GROOVY-10972
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10972
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Assignee: Eric Milles
>            Priority: Minor
>
> I have the following program
> {code}
> import java.util.function.Function;
> import java.util.LinkedList;
> class Foo {}
> class Test {
>     public static void main(String[] args) {
>         LinkedList<Foo> x = new LinkedList();
>         x.add(new Foo());
>         Function<Integer, Foo> z = x::remove;
>         Foo k = z.apply(0); // ClassCastException boolean cannot be cast to Foo.
>     }
> }
> {code}
> h3. Actual behavior
> The compiler accepts the code, but the following exception is triggered at runtime.
> {code}
> Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'false' with class 'java.lang.Boolean' to class 'Foo'
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:425)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:336)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:256)
>         at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
>         at Test.main(test.groovy:14)
> {code}
> There problem happens because there are two overloaded methods inside LinkedList:
>   * E remove(int x)
>   * boolean remove(Object d)
> The compiler seems to resolve the second method (i.e., boolean remove (Object)) although its return type does not match with Function<Integer, Foo>.
> Based only on the argument type, both methods are applicable. If the second "remove" method is more specific than the first one, then a compiler error should be raised at compile-time, because boolean does not match Foo. If this is not the case, then the program should call the first "remove" method.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)