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

Eric Milles commented on GROOVY-5502:
-------------------------------------

This kind of tracking was implemented in Groovy Development Tools for Eclipse 
back in 2020.  When a branch (if-else in this case) is reached, the assignments 
in both are collected and if a variable is assigned in both branches, the 
previous type can be discarded.  See {{updateVariableSoft}} and 
{{bubbleUpdates}}.

https://github.com/groovy/groovy-eclipse/blob/bc05754a9f4f2ae8986fb1ec9dfd78d8ae3bca19/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java#L1314

https://github.com/groovy/groovy-eclipse/commit/48e9a464d56eebe1b42fe7ae07c2468413e44847

> If/else branch does not always infer the variable type
> ------------------------------------------------------
>
>                 Key: GROOVY-5502
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5502
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Static Type Checker
>            Reporter: Ariel Morelli Andres
>            Priority: Minor
>
> Suppose we have the following code:
> {code}
> class A {
>   void m() {  
>   }
> }
> class B extends A {
> }
> class C extends A {
> }
> @groovy.transform.TypeChecked
> class Test {
>    void m() {
>       def var = new Object()   //If instead I put just "def var" it works as 
> spected
>       if (true) {
>         var = new B()
>       }
>       else {
>         var = new C()
>       }
>       var.m()                  //fails here
>    }
> }
> {code}
> In this case, after the if/else structure we can infer that var is instanceOf 
> A.
> But, the initialization seems to confuse the type inference.
> [Static type checking] - Cannot find matching method java.lang.Object#mA()
>  at line: 23, column: 7
> If instead of {code}def var = new Object(){code} we put just {code}def 
> var{code}, then the inference works fine.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to