Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6207

Incorrect flow typing reset after if branch

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.4
    • 2.1.5
    • Static Type Checker
    • None

    Description

      The following code produces a runtime error due to incorrect flow typing inference:

      import groovy.transform.*
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      @Retention(RetentionPolicy.RUNTIME)
      @Target([ElementType.FIELD])
      @interface Ann1 {}
      @Retention(RetentionPolicy.RUNTIME)
      @Target([ElementType.FIELD])
      @interface Ann2 {}
      
                  class A {
                      @Ann2
                      String field
                  }
                  
                  @CompileStatic
                  def doit(obj, String propName) {
                      def field = obj.getClass().getDeclaredField propName
                      println field
                      if(field) {
                          def annotation = field.getAnnotation Ann1
                          if(true) {
                              annotation = field.getAnnotation Ann2
                          }
                          return annotation
                      }
                      return null
                  }
                  
                  println doit(new A(), "field")
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            melix Cédric Champeau
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: