You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Vaidotas Valuckas (Jira)" <ji...@apache.org> on 2020/03/05 08:53:00 UTC

[jira] [Commented] (GROOVY-9424) Incorrect handling of final variables within switch

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

Vaidotas Valuckas commented on GROOVY-9424:
-------------------------------------------

It looks like this change [https://github.com/apache/groovy/commit/cd2b0e42fcf418e16776f34a66ed5cacc7711630#diff-3557aa86d96d4931ca9f64e88bd5f85fR480] introduced a regression in compiling switch statements with clauses containing only a break.

A simple reproducer: 
{code:java}
int i = 42 
switch(i) { 
    case 1: 
        break 
    case 2: 
        println 2 
}

{code}
 

This yields the following with the latest Groovy 2.5.10-SNAPSHOT:
{code:java}
java.lang.ClassCastException: class org.codehaus.groovy.ast.stmt.ReturnStatement cannot be cast to class org.codehaus.groovy.ast.stmt.BlockStatement (org.codehaus.groovy .ast.stmt.ReturnStatement and org.codehaus.groovy.ast.stmt.BlockStatement are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @135490 40) at org.codehaus.groovy.classgen.FinalVariableAnalyzer.fallsThrough(FinalVariableAnalyzer.java:480)
{code}
 

 Adding anything between the first case and break makes it compile again:
{code:java}
int i = 42 
switch(i) { 
    case 1: 
        true
        break 
    case 2: 
        println 2 
}
{code}
 

 

 

> Incorrect handling of final variables within switch
> ---------------------------------------------------
>
>                 Key: GROOVY-9424
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9424
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Daniel Wilmer
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 2.5.10, 3.0.2
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> The following code runs on groovy 2.4.18 but not on groovy 3.0.1.
> {code}
> static void main(String[] args) {
>     final String result
>     switch (2) {
>         case 1: result = "a"; break;
>         case 2: result = "b"; break;
>         default: result = "x"
>     }
>     println result
> }
> {code}
> *Error: Groovyc: The variable [result] is declared final but is reassigned.*
> Since we use final in our codebase a lot (although it seems to be ignored by groovy 2.4), 
> we will have to adjust a lot of places to make our code groovy 3.0 compatible.



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