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 2021/03/05 19:35:00 UTC

[jira] [Assigned] (GROOVY-9968) @TypeChecked Error: Members of Iterable are Foo, not Object

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

Eric Milles reassigned GROOVY-9968:
-----------------------------------

    Assignee: Eric Milles

> @TypeChecked Error: Members of Iterable<T extends Foo> are Foo, not Object
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-9968
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9968
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 3.0.7
>         Environment: Windows 10
> jdk-11.0.10.9-hotspot
> IntelliJ 2020.3.2
>            Reporter: mgroovy
>            Assignee: Eric Milles
>            Priority: Major
>
> *Problem*
> Trying to build the sample test code below fails with:
> {code:java}
> Groovyc: [Static type checking] - No such property: name for class: java.lang.Object
> Groovyc: Expected parameter of type java.lang.Object but got simple.groovy.bugs.groovy3.gb_2021_03_05.iterable_t_extends.Foo
> Groovyc: [Static type checking] - No such property: name for class: java.lang.Object
> {code}
> If we do not explicitly give the member type of the IterableTExtendsFoo iterable, Groovy assumes that the type is Object, even though it has been defined as being a subclass of Foo in the definition of IterableTExtendsFoo<T extends Foo>.
> *Expected*
>  * Code should compile.
>  * If Groovy could not deduce that IterableTExtendsFoo has been initialized with a List<T extends Foo>, then it should reject the line where the IterableTExtendsFoo ctor is called.
>  * Note: Works as expected in Groovy 2.5.x (at least up to 2.5.14)
> *Sample Code*
> {code:java}
> import groovy.transform.TypeChecked
> import org.junit.Ignore
> import org.junit.Test
> @TypeChecked
> class Groovy3_Iterable_T_extends_Bug {
> 	@Test
> 	@Ignore
> 	void 'Groovy 3-0-7 Members of Iterable T extends Foo are Foo'() {
> 		// Compiler should reject the following line if the ctor arg would not of type List<T extends Foo>
> 		final iterableTExtendsFoo = new IterableTExtendsFoo([new Foo('Sli'),new Foo('Msha'),new Foo('Dy'),])
> 		//final iterableTExtendsFoo = new IterableTExtendsFoo<Foo>([new Foo('Sli'),new Foo('Msha'),new Foo('Dy'),]) // Explicitly giving the iterable member type makes the code compile
> 		println iterableTExtendsFoo.collect { "Hi, my name is: $it.name" }
> 		println iterableTExtendsFoo.collect { Foo f -> "name=$f.name" }
> 	}
> }
> {code}
> {code:java}
> @Canonical class Foo { String name }
> {code}
> {code:java}
> @Canonical
> class IterableTExtendsFoo<T extends Foo> implements Iterable<T> {
> 	List<T> foos
> 	@Override Iterator<T> iterator() { foos.iterator() }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)