Repository navigation
GROOVY-12821: SC: use a closure shared variable in a closure through the common type of its assignments - #3332
Open
codeconsole wants to merge 2 commits into
Open
codeconsole wants to merge 2 commits into
codeconsole wants to merge 2 commits into
Conversation
…the common type of its assignments
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. 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 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, and is reported as the receiver case is when no method accepts
it. Calls outside closures keep the type flow typing gives the
variable there.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3332 +/- ##
==================================================
+ Coverage 74.9518% 74.9599% +0.0081%
- Complexity 48908 48962 +54
==================================================
Files 1768 1768
Lines 161213 161325 +112
Branches 31728 31767 +39
==================================================
+ Hits 120832 120929 +97
- Misses 28968 28975 +7
- Partials 11413 11421 +8
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 0d8c333 Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
Sub s = new Sub1(), a closure{ -> s.pm() }and a laters = new Sub2(), the closure was compiled forsas aSub1, the type flow typing gave it where the closure was created. The call cast the value toSub1and threwClassCastExceptionat run time oncesheld aSub2, although dynamic Groovy and@TypeCheckedrun it.The same held for a lambda, a property read or write (
s.id), an attribute (s.@f), an overridden method, andspassed as an argument, where the overload chosen forSub1was 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
MissingMethodExceptionfor 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 byClosuresStaticCompileTest):testClosureSharedVariable9covers 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;testClosureSharedVariable10covers the new error. Without the change, the first fails with theClassCastExceptionunder static compilation and the second in both modes. The 18,621 tests in thegroovy,gls,bugs,org.codehaus.groovy.classgen,transform,runtime,reflection,vmplugin,controlandastpackages and the stub generator tests pass.groovy-contracts,groovy-typecheckers,groovy-ginq,groovy-macro-library,groovy-json,groovy-xmlandgroovy-templatespass 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.