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 2021/03/05 14:58:00 UTC

[jira] [Assigned] (GROOVY-9967) STC: Access to field/property is forbidden after instanceof.

     [ https://issues.apache.org/jira/browse/GROOVY-9967?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles reassigned GROOVY-9967:
-----------------------------------

    Assignee: Eric Milles

> STC: Access to field/property is forbidden after instanceof.
> ------------------------------------------------------------
>
>                 Key: GROOVY-9967
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9967
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 4.0.0-alpha-2
>            Reporter: Stefanos Chaliasos
>            Assignee: Eric Milles
>            Priority: Major
>
> I have the following Groovy program.
> {code:groovy}
> class Test {
>   static void main(String[] args) {
>     println(C.func4(new B()))
>   }
> }
> class A {}
> class B extends A {
>   // It has the same behavior if f is a field instead of a property.
>   String f = "foo"
> }
> @groovy.transform.CompileStatic
> class C {
>   static String func1(Object x) {
>     (x instanceof String) ? x : "foo" // Compiles
>   }
>   static String func2(B x) {
>     x.f // Compiles
>   }
>   static String func3(B x) {
>     (x instanceof B) ? x.f : "foo" // Compiles
>   }
>   static String func4(A x) {
>     (x instanceof B) ? x.f : "foo" // Does not compile
>   }
> }
> {code}
> h2. Actual Behavior
> The program does not compile, and I get the following error.
> {code:java}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Test.groovy: 28: Access to A#f is forbidden @ line 28, column 24.
>        (x instanceof B) ? x.f : "foo" 
>                           ^
> 1 error
> {code}
> h2. Expected Behavior
> Compile successfully.
> h2. Comment
> This should be a regression bug because it compiles with the 3.0.7 compiler.
>  If we declared classes A and B after C, then the program compiles successfully.
>  Specifically, the following program compiles without any errors.
> {code:groovy}
> class Test {
>   static void main(String[] args) {
>     println(C.func4(new B()))
>   }
> }
> @groovy.transform.CompileStatic
> class C {
>   static String func1(Object x) {
>     (x instanceof String) ? x : "foo" 
>   }
>   static String func2(B x) {
>     x.f 
>   }
>   static String func3(B x) {
>     (x instanceof B) ? x.f : "foo"
>   }
>   static String func4(A x) {
>     (x instanceof B) ? x.f : "foo" 
>   }
> }
> class A {}
> class B extends A {
>   String f = "foo"
> }
> {code}



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