You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/10/20 14:17:00 UTC

[jira] [Created] (GROOVY-9786) SC: cast exception for variable assigned within conditional blocks

Eric Milles created GROOVY-9786:
-----------------------------------

             Summary: SC: cast exception for variable assigned within conditional blocks
                 Key: GROOVY-9786
                 URL: https://issues.apache.org/jira/browse/GROOVY-9786
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 2.5.13
            Reporter: Eric Milles


Consider the following:
{code:groovy}
interface Zero {
  def m()
}
class One implements Zero {
  def m() { 'One' }
}
class Two implements Zero {
  def m() { 'Two' }
}

boolean aaa() { false }
boolean bbb() { true  }

@groovy.transform.CompileStatic
void test() {
  Zero x
  if (aaa()) {
    x = new One()
  } else if (bbb()) {
    x = new Two()
  }
  x.m() // GroovyCastException: Cannot cast object 'Two@654d8173' with class 'Two' to class 'One'
}

test()
{code}

This case was recently converted from dynamic groovy -- where it executes fine -- to {{@CompileStatic}}.



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