You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Dan Ziemba (Jira)" <ji...@apache.org> on 2020/02/27 09:06:00 UTC

[jira] [Commented] (GROOVY-8386) Final variable analysis broken with try/catch/finally

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

Dan Ziemba commented on GROOVY-8386:
------------------------------------

This is still broken with 2.5.9:
{code:groovy}
import groovy.transform.CompileStatic
import java.nio.file.Files
import java.nio.file.Path

@CompileStatic
class Test {
    static void main(String... args) {
        new Test().run()
    }

    void run() {
        // Removing 'final' prevents error
        final Path x
        try {
            x = Files.createTempDirectory('delete-me')
            println "Created temp dir: $x"
            // Some stuff with x that might throw
            throw new IOException('poo')
        } finally {
            println 'in finally'
            if (x) {
                // This works without println below. Maybe because it's a groovy-added method?
                x.deleteDir()
                // Error on this println
                // When removed, no error and code runs to completion
                println "Deleted temp dir: $x"
            }
            println 'done'
        }
    }
}
{code}

Error message:

{noformat}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/Test.groovy: 26: The variable [x] may be uninitialized
. At [26:45]  @ line 26, column 45.
     println "Deleted temp dir: $x"
                                 ^

1 error
{noformat}




> Final variable analysis broken with try/catch/finally
> -----------------------------------------------------
>
>                 Key: GROOVY-8386
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8386
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.5.x
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 2.5.0-beta-3
>
>
> In this code:
> {code}
> final begin
> try {
>   begin = new Date()
> } finally {
>   println 'done'
> }
> {code}
> The current error is:
> {noformat}
> The variable [begin] may be uninitialized
> {noformat}
> But this should only happen if begin is used in the catch or finally blocks or prior to the first assignment.
> This impacts Spock usage since it converts the following into something similar to above:
> {code}
> @Grab('org.spockframework:spock-core:1.1-groovy-2.4-SNAPSHOT')
> @GrabExclude('org.codehaus.groovy:groovy-all')
> import spock.lang.Specification
> class DummySpec extends Specification {
>     def 'FVA when using Spock'() {
>         given:
>         final begin = new Date()
>         cleanup:
>         println 'done'
>     }
> }
> {code}



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