You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2023/06/08 11:21:00 UTC

[jira] [Created] (GROOVY-11086) Wrong method is chosen when having overloaded methods with bounded type parameters

Thodoris Sotiropoulos created GROOVY-11086:
----------------------------------------------

             Summary: Wrong method is chosen when having overloaded methods with bounded type parameters
                 Key: GROOVY-11086
                 URL: https://issues.apache.org/jira/browse/GROOVY-11086
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program

{code}
import java.util.*;

class Foo<T> {}
class Bar<T> {}

class Main {
  static final void test() {
    LinkedList<Number> x = null;
    Foo<Iterable<Number>> y = Main.<Number, Iterable<Number>>m(x);

  }

  static <X, T extends Iterable<? extends X>> Foo<T> m(Iterable<? extends X> x) {return null;}
  static <X, T extends List<? extends X>> Bar<T> m(List<? extends X> x) { return null; }
}
{code}

h3. Actual behaviour

{code}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Main.groovy: 10: [Static type checking] - Cannot assign value of type Bar<T extends java.util.List<? extends X>> to variable of type Foo<java.lang.Iterable<java.lang.Number>>
 @ line 10, column 31.
       Foo<Iterable<Number>> y = Main.<Number, Iterable<Number>>m(x);
                                 ^

1 error
{code}

h3. Expected behaviour

Compile successfully

In particular, the chosen method is wrong. It's not even applicable as the provided type argument (at the call-site) does not respect the upper bound of the chosen method.
h3. Notes

Test case adapted from the following code that uses the assertj-core lib:

{code}
import java.util.*;
import org.assertj.core.api.*;

class Foo<T> {}
class Bar<T> {}

class Main {
  static final void test() {
    LinkedList<Number> x = null;

    FactoryBasedNavigableIterableAssert<?, Iterable<Number>, Number, ObjectAssert<Number>> res = Java6Assertions.assertThat(x, (y) -> x);
  }
{code}



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