[
https://issues.apache.org/jira/browse/GROOVY-12821?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149421#comment-18149421
]
ASF GitHub Bot commented on GROOVY-12821:
-----------------------------------------
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.
> SC: ClassCastException when a closure shared variable is assigned instances
> of two different subclasses
> -------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12821
> URL: https://issues.apache.org/jira/browse/GROOVY-12821
> Project: Groovy
> Issue Type: Bug
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> On master (09e84fa), JDK 25.0.4:
> {code: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'
> {code}
> Dynamic Groovy and {{@TypeChecked}} return {{pm:sub2}}. Under
> {{@CompileStatic}} it compiles and throws ClassCastException at run time
> ({{Outer$Sub2}} cannot be cast to {{Outer$Sub1}}): the shared variable is
> cast to the type of its first assignment.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)