You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Renato Athaydes (Jira)" <ji...@apache.org> on 2021/05/21 10:22:00 UTC

[jira] [Updated] (GROOVY-10102) Type checking fails when calling trait method using indirect SelfType relation

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

Renato Athaydes updated GROOVY-10102:
-------------------------------------
    Description: 
I am having trouble migrating a very large project from Groovy 2.4.x to 2.5.14.

I was able to reproduce the problem with this small example:
  
{quote}import groovy.transform.CompileStatic
 import groovy.transform.SelfType

import java.util.function.Supplier

@CompileStatic
 @SelfType([C])
 trait D {
   def go() {
     String s = foo
     withObject(s) \{       s.toUpperCase()     }

  }
 }

@SelfType(B)
 @CompileStatic
 trait C {}

@SelfType([A])
 @CompileStatic
 trait B {}

@CompileStatic
 trait A {
   String foo = 'foo'

  def withObject(String s, Supplier<Object> g) \{     s + g.get()   }

}

class Impl implements A, B, C, D {
   def go() {

     println withObject(foo) \{ ' bar' }

  }
 }

 
{quote}
 
 Compiling this code results in this error:
{quote}src/main/groovy/bar/Foo.groovy: 13: [Static type checking] - Cannot find matching method <UnionType:bar.C+bar.D>#withObject(java.lang.String, groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
 @ line 13, column 9.
 withObject(s) {
 ^

1 error
{quote}
It's easy to work around once you know how: just make the trait that calls `withObject` list the trait that provides that method in its `SelfType` directly, i.e. in this case:
{quote}@SelfType([A, C])
 trait D \{ ... }
{quote}
> Interestingly, changing D's self-type to `B` also works! It looks like the 2nd indirection is needed to reproduce the issue.

However, figuring it out can take a long time! Would be nice to make this work automatically, maybe by just making `@SelfType(C)` equivalent to `@SelfType(C, B, A)` (i.e. merge the types by recursively applying other's self-types).

  was:
I am having trouble migrating a very large project from Groovy 2.4.x to 2.5.14.

I was able to reproduce the problem with this small example:
 
{quote}
import groovy.transform.CompileStatic
import groovy.transform.SelfType

import java.util.function.Supplier

@CompileStatic
@SelfType([C])
trait D {
  def go() {
    String s = foo
    withObject(s) {
      s.toUpperCase()
    }
  }
}

@SelfType(B)
@CompileStatic
trait C {}

@SelfType([A])
@CompileStatic
trait B {}

@CompileStatic
trait A {
  String foo = 'foo'

  def withObject(String s, Supplier<Object> g) {
    s + g.get()
  }
}

class Impl implements A, B, C, D {
  def go() {
    println withObject(foo) \{ ' bar' }
  }
}

 
{quote}
 
Compiling this code results in this error:


{quote}
src/main/groovy/bar/Foo.groovy: 13: [Static type checking] - Cannot find matching method <UnionType:bar.C+bar.D>#withObject(java.lang.String, groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
 @ line 13, column 9.
           withObject(s) {
           ^

1 error
{quote}

It's easy to work around once you know how: just make the trait that calls `withObject` list the trait that provides that method in its `SelfType` directly, i.e. in this case:

{quote}
@SelfType([A, C])
trait D { 
  ...
}
{quote}

> Interestingly, changing D's self-type to `B` also works! It looks like the 2nd indirection is needed to reproduce the issue.

However, figuring it out can take a long time! Would be nice to make this work automatically, maybe by just making `@SelfType(C)` equivalent to `@SelfType(C, B, A)` (i.e. merge the types by recursively applying other's self-types).


> Type checking fails when calling trait method using indirect SelfType relation
> ------------------------------------------------------------------------------
>
>                 Key: GROOVY-10102
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10102
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler, Static Type Checker
>    Affects Versions: 2.5.14
>            Reporter: Renato Athaydes
>            Priority: Major
>
> I am having trouble migrating a very large project from Groovy 2.4.x to 2.5.14.
> I was able to reproduce the problem with this small example:
>   
> {quote}import groovy.transform.CompileStatic
>  import groovy.transform.SelfType
> import java.util.function.Supplier
> @CompileStatic
>  @SelfType([C])
>  trait D {
>    def go() {
>      String s = foo
>      withObject(s) \{       s.toUpperCase()     }
>   }
>  }
> @SelfType(B)
>  @CompileStatic
>  trait C {}
> @SelfType([A])
>  @CompileStatic
>  trait B {}
> @CompileStatic
>  trait A {
>    String foo = 'foo'
>   def withObject(String s, Supplier<Object> g) \{     s + g.get()   }
> }
> class Impl implements A, B, C, D {
>    def go() {
>      println withObject(foo) \{ ' bar' }
>   }
>  }
>  
> {quote}
>  
>  Compiling this code results in this error:
> {quote}src/main/groovy/bar/Foo.groovy: 13: [Static type checking] - Cannot find matching method <UnionType:bar.C+bar.D>#withObject(java.lang.String, groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
>  @ line 13, column 9.
>  withObject(s) {
>  ^
> 1 error
> {quote}
> It's easy to work around once you know how: just make the trait that calls `withObject` list the trait that provides that method in its `SelfType` directly, i.e. in this case:
> {quote}@SelfType([A, C])
>  trait D \{ ... }
> {quote}
> > Interestingly, changing D's self-type to `B` also works! It looks like the 2nd indirection is needed to reproduce the issue.
> However, figuring it out can take a long time! Would be nice to make this work automatically, maybe by just making `@SelfType(C)` equivalent to `@SelfType(C, B, A)` (i.e. merge the types by recursively applying other's self-types).



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