Paul King created GROOVY-12774:
----------------------------------

             Summary: STC: a Class<? extends List> argument is accepted for a 
Class<? extends List<String>> type argument of a parameterized receiver
                 Key: GROOVY-12774
                 URL: https://issues.apache.org/jira/browse/GROOVY-12774
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
    Affects Versions: 6.0.0, 5.1.3, 4.0.33
            Reporter: Paul King


{code:groovy}
@groovy.transform.CompileStatic
class C {
    static class IntList extends ArrayList<Integer> { }
    static class Box<T> { T value; void set(T value) { this.value = value } }

    static void put(Map<String, Class<? extends List<String>>> map, Box<Class<? 
extends List<String>>> box, Class<? extends List> raw) {
        map.put('k', raw)
        box.set(raw)
    }
    static List<String> strings(Class<? extends List<String>> type) {
        List<String> list = type.getConstructor().newInstance()
        list.add('x')
        list
    }
    static main(args) {
        Map<String, Class<? extends List<String>>> map = [:]
        Box<Class<? extends List<String>>> box = new Box<>()
        put(map, box, IntList)
        IntList ints = (IntList) strings(map['k'])
        println ints.get(0).intValue()
    }
}
{code}

This compiles with 4.0.33, 5.1.3, 6.0.0 and master, and then fails:
{noformat}
java.lang.ClassCastException: class java.lang.String cannot be cast to class 
java.lang.Integer
{noformat}
(a {{MissingMethodException}} for {{intValue}} under {{@TypeChecked}}). The 
{{Class}} of an {{IntList}} is stored as a {{Class<? extends List<String>>}}, 
so a {{String}} is added to an {{IntList}}.

javac rejects both calls: "Class<CAP#1> cannot be converted to Class<? extends 
List<String>>".

An assignment, {{Class<? extends List<String>> c = raw}}, is already rejected. 
For {{map.put}} and {{box.set}}, the parameter type, {{V}} or {{T}}, takes the 
receiver's type argument, which has no unresolved generics, so 
{{StaticTypeCheckingSupport#typeCheckMethodsWithGenerics}} skips the check that 
an assignment gets (the {{hasUnresolvedGenerics(resolvedType)}} guard).

*Expected:* a compile error, as for the assignment. A {{@NotYetImplemented}} 
test, {{GenericsSTCTest#testRawBoundArgumentOfParameterizedReceiver}}, records 
the {{map.put}} case in PR #3293.



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

Reply via email to