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

Ben Sherman commented on GROOVY-11863:
--------------------------------------

Actually, the error persists if you extract the inner class and add 
CompileStatic to it. So the error seems to be related to the use of the 
delegate.

Perhaps the type checker is concluding that the type of {{c}} is unknown since 
it could come from the delegate or the withReader closure. But I would expect 
it to still infer the type of {{c}} from the local variable, even with these 
other factors.

> False type checking error in inner class with delegate field
> ------------------------------------------------------------
>
>                 Key: GROOVY-11863
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11863
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 5.0.4
>            Reporter: Ben Sherman
>            Priority: Minor
>
> Full context is in this GitHub PR: 
> [nextflow-io/nextflow#6220|https://github.com/nextflow-io/nextflow/pull/6220/changes#r2666549692]
> I encountered a false type checking error while trying to upgrade Nextflow 
> from Groovy 4 to 5. I have created a minimal example below. The comparison 
> "c++ < MAX_LINES" is reported as a type mismatch even though both are integer 
> types.
> The factors seem to be (1) an inner class, (2) a delegate field, and (3) a 
> while loop. But I'm not sure what the exact cause is.
> -In my case, the inner class is static, so I can work around the error by 
> moving the inner class outside.-
> Minimal example:
> {code:java}
> import java.nio.file.Path
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test {
>     class Inner {
>         static private int MAX_LINES = 100
>         @Delegate
>         private Map<String,Object> delegate = [:]
>         private String fetch(Path path) {
>             int c=0
>             path.withReader { reader ->
>                 // ERROR: Cannot call 
> java.lang.Integer#compareTo(java.lang.Integer) with arguments 
> [java.lang.Object]
>                 while( c++ < MAX_LINES ) {
>                 }
>             }
>         }
>     }
> }{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to