[ 
https://issues.apache.org/jira/browse/GROOVY-5839?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15068184#comment-15068184
 ] 

Frank Pavageau commented on GROOVY-5839:
----------------------------------------

I'm not sure that's fixed actually, because the test case tests a different 
scenario:
{code}
public class GoodCodeRed<T> {
    Collection<GoodCodeRed<T>> attached = []
    public <T> void attach(GoodCodeRed<T> toAttach) {
        attached.add(toAttach)
   }
   static void foo() {
       def g1 = new GoodCodeRed<Long>()
       def g2 = new GoodCodeRed<Integer>()
       g1.attach(g2);
    }
}
GoodCodeRed.foo()
{code}

The point is not that shouldn't be able to call {{attach()}} on an instance 
parameterized by {{Long}} with a argument parameterized by {{Integer}}, it's 
that {{T}} in the context of the method is not the same as {{T}} in the context 
of the class.

The same class in Java (without the static method):
{code}
import java.util.ArrayList;
import java.util.Collection;
public class GoodCodeRed<T> {
    private Collection<GoodCodeRed<T>> attached = new ArrayList<>();
    public <T> void attach(GoodCodeRed<T> toAttach) {
        attached.add(toAttach);
    }
}
{code}
fails to compile the {{attach()}} method itself:
{noformat}
GoodCodeRed.java:6: error: incompatible types: GoodCodeRed<T#1> cannot be 
converted to GoodCodeRed<T#2>
        attached.add(toAttach);
                     ^
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>attach(GoodCodeRed<T#1>)
    T#2 extends Object declared in class GoodCodeRed
{noformat}

It's not the compilation of the usage of {{GoodCodeRed}} which should fail, but 
of its implementation, as if it were written like:
{code}
public class GoodCodeRed<T> {
    Collection<GoodCodeRed<T>> attached = []
    public <U> void attach(GoodCodeRed<U> toAttach) {
        attached.add(toAttach)
   }
}
{code}
without the type shadowing. {{T}} and {{U}} are not related, neither are 
{{T#1}} and {{T#2}}.

Should I open a new bug?

> Typechecker does not detect shadowed generic parameter
> ------------------------------------------------------
>
>                 Key: GROOVY-5839
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5839
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 2.0.5
>            Reporter: Dimitar Dimitrov
>            Assignee: Jochen Theodorou
>             Fix For: 2.1.7, 2.2.0-beta-2
>
>
> The typechecker does not realize that the <T> of the method shadows the <T> 
> of the class and compiles this code without warning. The same code does not 
> typecheck in Java.
> {code}
> public class GoodCodeRed<T> {
>     Collection<GoodCodeRed<T>> attached
>     @groovy.transform.CompileStatic
>     public <T> void attach(GoodCodeRed<T> toAttach) {
>         attached.add(toAttach)
>     }
> }
> {code}
> See also http://youtrack.jetbrains.com/issue/IDEA-96606



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to