You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jochen Theodorou (JIRA)" <ji...@apache.org> on 2018/02/07 02:01:00 UTC

[jira] [Commented] (GROOVY-8472) Final variable analysis doesn't account for early exit

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

Jochen Theodorou commented on GROOVY-8472:
------------------------------------------

to bad that the issue does not really describe the error and what is expected. So there is plenty room for misunderstanding things here... but in general... isn't it good enough for our cases to know if a variable is quasi-final from a certain point on? The issue shines through that initialization is required for that. I think it is actually not. If we define final as "we allow one single assignment per code path after the default value", then we have a much more easy life imho.  So in your first example let me totally ignore the return in the catch, but let us assume, that there is a caught exception... then yes str will be of the default value. But the value is still final after the try block has been handled. In your second example I see index, val and result as final after the while block. The much more difficult to answer question is if we ignore that we have a while here and assume the while is actually a method call with attached block, how do we then handle the final states? Nothing guarantees the block will actually be executed and the value be initialized at all. The execution might be even asynchronously. Java prevents the problem by requiring the final state before such blocks (anonymous inner classes and lambdas). We do not enforce this and I think we really need a different understanding of final than Java here.

> Final variable analysis doesn't account for early exit
> ------------------------------------------------------
>
>                 Key: GROOVY-8472
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8472
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Paul King
>            Priority: Major
>
> An example with early return from a method:
> {code}
> def method(String foo) {
>     def str
>     try {
>         str = foo.trim()
>     }
>     catch(e) {
>         println e
>         return null
>     }
>     int exitCode = str.isInteger() ? str.toInteger() : null
>     exitCode
>     //str.isInteger() ? str.toInteger() : null // this doesn't trigger the error
> }
> println method(null)
> {code}
> Another example with a while loop:
> {code}
> int index
> def result = -1
> int val = 2
> while(true) {
>     try {
>         index = 12/val--
>     }
>     catch(e) {
>         break
>     }
>     println index // okay
>     result = index // The variable [index] may be uninitialized
> }
> println result // should be found but isn't if -1 above is commented out
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)