codeconsole opened a new pull request, #3332:
URL: https://github.com/apache/groovy/pull/3332

   With `Sub s = new Sub1()`, a closure `{ -> s.pm() }` and a later `s = new 
Sub2()`, the closure was compiled for `s` as a `Sub1`, the type flow typing 
gave it where the closure was created. The call cast the value to `Sub1` and 
threw `ClassCastException` at run time once `s` held a `Sub2`, although dynamic 
Groovy and `@TypeChecked` run it.
   
   ```groovy
   @groovy.transform.CompileStatic
   class Outer {
       static class Box {
           String id = 'box'
           String pm() { 'pm:' + id }
           static String t() { Sub s = new Sub1(); def c = { -> s.pm() }; s = 
new Sub2(); c() }
       }
       static class Sub extends Box { }
       static class Sub1 extends Sub { Sub1() { id = 'sub1' } }
       static class Sub2 extends Sub { Sub2() { id = 'sub2' } }
   }
   assert Outer.Box.t() == 'pm:sub2'  // ClassCastException: Outer$Sub2 cannot 
be cast to Outer$Sub1
   ```
   
   The same held for a lambda, a property read or write (`s.id`), an attribute 
(`s.@f`), an overridden method, and `s` passed as an argument, where the 
overload chosen for `Sub1` was called. The type checker's second pass already 
checks a call on such a variable against the common type of its assignments, 
but only to report a missing method.
   
   When a closure or lambda makes the call, the second pass now also takes the 
method it finds on the common type as the target and the class that declares it 
as the type of the receiver. A property or attribute expression on such a 
variable in a closure is checked there too, and is given the common type where 
that type has the property. A call with such a variable as an argument selects 
the method again for the common type, with the common type for a receiver that 
is such a variable as well. Calls outside closures keep the type flow typing 
gives the variable there.
   
   ## Behaviour change
   
   A closure call that passes such a variable, where no method accepts the 
common type of its assignments, is now reported, in both modes, as a call on 
such a variable already is: "The closure shared variable "s" has been assigned 
with various types and the method only1(Sub) does not accept the lowest upper 
bound of those types: Sub". Dynamic Groovy throws `MissingMethodException` for 
it once the variable holds a value the method does not take. Under 
`@CompileStatic`, an argument overload is selected for the common type, as 
javac selects one for the variable's static type.
   
   ## Tests
   
   `ClosuresSTCTest` (also run statically compiled by 
`ClosuresStaticCompileTest`): `testClosureSharedVariable9` covers a call, a 
lambda, a property read and write, an attribute, an overridden method, an 
argument, a receiver and argument that are both such variables, and an 
interface-only common type; `testClosureSharedVariable10` covers the new error. 
Without the change, the first fails with the `ClassCastException` under static 
compilation and the second in both modes. The 18,621 tests in the `groovy`, 
`gls`, `bugs`, `org.codehaus.groovy.classgen`, `transform`, `runtime`, 
`reflection`, `vmplugin`, `control` and `ast` packages and the stub generator 
tests pass. `groovy-contracts`, `groovy-typecheckers`, `groovy-ginq`, 
`groovy-macro-library`, `groovy-json`, `groovy-xml` and `groovy-templates` pass 
their tests, and every subproject compiles its main and test sources. The 
Checkstyle and CodeNarc gates pass.
   
   This branch and #3050 both change 
`StaticTypeCheckingVisitor.performSecondPass`, so whichever is merged second 
needs a small merge there.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to