Georgii Ustinov created GROOVY-11698:
----------------------------------------

             Summary: Instanceof pattern variable doesn't respect scope
                 Key: GROOVY-11698
                 URL: https://issues.apache.org/jira/browse/GROOVY-11698
             Project: Groovy
          Issue Type: Bug
          Components: class generator
            Reporter: Georgii Ustinov


Hi, I am main supporter of Groovy plugin inside IntelliJ IDEA IDE and currently 
processing 
[instanceof variable 
support.|https://youtrack.jetbrains.com/issue/IDEA-371239/Groovy-Support-instanceof-variable]
I have identified several cases in which pattern matching inside instanceof 
doesn't respect the scope and behaviour differs from Java.

See the examples:

1. Instanceof in else branch
Java:
{code:java}
package org.java;


public class MainJava {
    static class A {}
    static class B extends A{}
    public static void main(String[] args) {
        A a = new B();
        if (!(a instanceof B b)) {

        } else {
            System.out.println(b);
        }
    }
}
 {code}
Groovy (doesn't compile):
{code:java}
package org.groovy


class Main {
    static class A {}
    static class B extends A {}
    static void main(String[] args) {
        A a = new B();
        if (!(a instanceof B b)) {

        } else {
            System.out.println(b)
        }
    }
}
 {code}
2. Instanceof in return statements:
Java
{code:java}
package org.java;


public class MainJava {
    static class A {}
    static class B extends A{}
    public static void main(String[] args) {
        A a = new B();
        if (!(a instanceof B b)) {
            return;
        }
        System.out.println(b);
    }
}{code}
Groovy
{code:java}
package org.groovy


class Main {
    static class A {}
    static class B extends A {}
    static void main(String[] args) {
        A a = new B();
        if (!(a instanceof B b)) {
            return
        }
        println b
    }
}
 {code}
 

Could you, please, fix these issues?

 

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to