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

Type checker infers wrong type on compound assignment (e.g. += for collection) for property

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.7
    • 2.4.8
    • Static Type Checker
    • None

    Description

      Given the Java class

      public class ContainsSet extends GroovyObjectSupport {
          private Set<File> files = new HashSet<File>();
      
          public Set<File> getFiles() {
              return files;
          }
      
          public void setFiles(Set<File> files) {
              this.files = files;
          }
      }
      

      the following Groovy code will not type check:

      @TypeChecked
      private Set<File> modifyIdeaModel() {
          set.files += new File('foo')
      }
      

      The failure is:

      Error: Groovyc: [Static type checking] - Cannot assign value of type java.io.File to variable of type java.util.Set <java.io.File>
      

      Without the @TypeChecked the code runs with dynamic Groovy.

      Using the following Groovy class type checking the code above works.

      class ContainsSet {
          Set<File> files = new HashSet<>()
      }
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              wolfs Stefan Wolf
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: