Paul King created GROOVY-12766:
----------------------------------

             Summary: STC: arguments of a generic method call are not checked 
against an explicit or inferred type argument
                 Key: GROOVY-12766
                 URL: https://issues.apache.org/jira/browse/GROOVY-12766
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation, Static Type Checker
    Affects Versions: 6.0.0, 5.1.3, 4.0.33
            Reporter: Paul King


Under {{@TypeChecked}} and {{@CompileStatic}}, a call of a generic method such 
as {{<T> void add(List<T> list, T value)}} is accepted when its arguments do 
not agree on {{T}}, whether {{T}} is inferred or given explicitly. The list 
then holds a value of the wrong type:

{code:groovy}
@groovy.transform.CompileStatic
class Lists {
    static <T> void add(List<T> list, T value) { list.add(value) }
    static <E> E first(List<E> list) { list.get(0) }

    static void test(List<StringBuilder> builders, List<CharSequence> chars, 
List<? extends CharSequence> wild) {
        add(builders, 'hello')                   // T inferred as StringBuilder 
from the list
        add(chars, 42)                           // T inferred as CharSequence 
from the list
        Lists.<CharSequence>add(builders, 'x')   // explicit T: a 
List<StringBuilder> is not a List<CharSequence>
        String s = Lists.<String>first(wild)     // explicit T: a List<? 
extends CharSequence> is not a List<String>
    }
}

List<StringBuilder> builders = [new StringBuilder('sb')]
List<CharSequence> chars = []
Lists.test(builders, chars, [new StringBuilder('sb')])
println builders*.getClass()*.simpleName   // [StringBuilder, String, String]
println chars*.getClass()*.simpleName      // [Integer]
println Collections.max(builders)          // ClassCastException
{code}

Output with 4.0.33, 6.0.0 and current master:
{noformat}
[StringBuilder, String, String]
[Integer]
java.lang.ClassCastException: class java.lang.StringBuilder cannot be cast to 
class java.lang.String
{noformat}

All four calls compile. javac rejects each of them:
{noformat}
add(builders, "hello")                  method add in class Lists cannot be 
applied to given types
add(chars, 42)                          method add in class Lists cannot be 
applied to given types
Lists.<CharSequence>add(builders, "x")  incompatible types: List<StringBuilder> 
cannot be converted to List<CharSequence>
Lists.<String>first(wild)               incompatible types: List<CAP#1> cannot 
be converted to List<String>
{noformat}

A wildcard argument is already checked: {{add(wild, 'hello')}} and 
{{Lists.<CharSequence>add(wild, 'hello')}} for a {{List<? extends 
CharSequence>}} parameter are rejected ({{Cannot call <T> 
Lists#add(java.util.List<T>, T) with arguments [java.util.List<? extends 
java.lang.CharSequence>, java.lang.String]}}).

*Expected:*
* An explicit type argument instantiates the type parameter, and each argument 
must then fit its parameter, as for a method that is not generic.
* An inferred type argument must fit every argument that mentions it. For 
{{add(builders, 'hello')}}, {{T}} is {{StringBuilder}} from the list, and a 
{{String}} is not a {{StringBuilder}}.

*To decide:* {{Lists.<Integer>first(ints)}} for a {{List<? extends Integer>}} 
compiles and is safe, since {{Integer}} is final, but javac rejects it. 
Rejecting it would match Java. A test currently records that it compiles.

*Related:* under {{@CompileStatic}}, a local declared {{List<? extends 
CharSequence> local = new ArrayList<StringBuilder>()}} is typed 
{{ArrayList<StringBuilder>}} by flow typing. {{Lists.<CharSequence>add(local, 
'hello')}} then compiles and fails at run time with {{GroovyCastException: 
Cannot cast object 'hello' ... to class 'java.lang.StringBuilder'}}. Under 
{{@TypeChecked}} it compiles, runs, and the list holds the {{String}}.

Tests marked {{@NotYetImplemented}}, 
{{GenericsSTCTest#testExplicitTypeArgumentIsCheckedAgainstArguments}} and 
{{#testInferredTypeArgumentIsCheckedAgainstArguments}}, record these; they come 
with the GROOVY-12754/12755/12756 changes (PR #3293).




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

Reply via email to