You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jan Hackel (Jira)" <ji...@apache.org> on 2022/06/27 07:13:00 UTC

[jira] [Created] (GROOVY-10667) Wrong Type-Inference in `instanceof` block

Jan Hackel created GROOVY-10667:
-----------------------------------

             Summary: Wrong Type-Inference in `instanceof` block
                 Key: GROOVY-10667
                 URL: https://issues.apache.org/jira/browse/GROOVY-10667
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation
    Affects Versions: 4.0.3
         Environment: I tried Java 8 and 17. Both from a Gradle 7.1 project.
            Reporter: Jan Hackel


Groovy 4.0.3 produces the error

{{[Static type checking] - Cannot find matching method de.jhunovis.instance_of.MethodArgumentTypeInferenceErrorTest#doSomethingWithException(de.jhunovis.instance_of.MethodArgumentTypeInferenceErrorTest$Tagged). Please check if the declared type is correct and if the method exists.}}

for this piece of code:
{code:groovy}
import groovy.transform.CompileStatic

@CompileStatic
class MethodArgumentTypeInferenceErrorTest {

  private static interface Tagged {
    String getTag()
  }

  private static class TaggedException extends RuntimeException implements Tagged {
    String tag
  }

  void cannotFindMatchingMethod() {
    doSomethingWith(new TaggedException(tag: 'Test'))
  }

  private static void doSomethingWith(Exception e) {
    if (e instanceof Tagged) {
      println e.tag
      doSomethingWithException(e)
    }
  }

  private static void doSomethingWithException(Exception exception) {
  }
}
{code}
It seems the `instanceof` confuses the type-inference.



--
This message was sent by Atlassian Jira
(v8.20.7#820007)