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

@CompileStatic refers to private field in parent class

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.15, 2.6.0-alpha-4, 3.0.0-alpha-3, 2.5.1
    • Fix Version/s: 3.0.0-alpha-4, 2.5.2
    • Component/s: Static compilation
    • Labels:
      None

      Description

      import groovy.transform.CompileStatic
      
      @CompileStatic
      interface Thing {
          void call()
      }
      
      @CompileStatic
      class ThingImpl implements Thing {
          void call() {}
      }
      
      @CompileStatic
      class Parent {
          private final ThingImpl thing
      
          public Thing getThing() { null }
      }
      
      @CompileStatic
      class Child extends Parent {
          public void doSomething() {
              thing.call()
          }
      }
      

      Compile via: groovyc Example.groovy.

      The line thing.call in Child.doSomething() calls getThing(), but then ends up referring to the returned value according to the private field's type from Parent (ThingImpl) instead of the actual returned type (Thing). The private field from Parent (or its type) should not be visible to Child at all.

        public void doSomething();
          descriptor: ()V
          flags: (0x0001) ACC_PUBLIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: invokevirtual #20                 // Method Parent.getThing:()LThing;
               4: checkcast     #22                 // class ThingImpl
               7: invokevirtual #25                 // Method ThingImpl.call:()V
              10: aconst_null
              11: pop
              12: return
      

      This is causing now problems for Gradle plugins compiled against Grade 4.9 or before trying to run on Gradle 4.9, because we've made a change to an internal type (`ProjectInternal`) that ended up being referred to via this bug in compiled and released code. See https://github.com/gradle/gradle/issues/6027

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                lptr Lóránt Pintér
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: