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

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 2.4.8
    • Component/s: Static Type Checker
    • Labels:
      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

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

                Dates

                • Created:
                  Updated:
                  Resolved: