Repository navigation
GROOVY-12585 + GROOVY-12643 + GROOVY-12644: consistent collection and map conversions - #3165
Merged
Merged
Conversation
…ions for casting and as Assignment and cast (castToType) and coercion with as (asType) now share one table of default implementations: ArrayList for List, Collection and Iterable; LinkedHashSet for Set; TreeSet for SortedSet and NavigableSet; LinkedList for Queue and Deque; LinkedBlockingQueue for BlockingQueue; TreeMap for SortedMap and NavigableMap; ConcurrentHashMap for ConcurrentMap. So assignments such as SortedSet s = list now succeed, and as returns a real collection for these types instead of a proxy. On assignment, a concrete collection class tries its copy constructor, then its no-arg constructor plus addAll, before the elements as constructor arguments, as as already did. So Vector v = [5] holds the element instead of being an empty Vector of capacity 5. An array coerced with as follows the same table, so anArray as Set keeps element order.
…ction types like the runtime cast Under @TypeChecked and @CompileStatic, a list literal assigned to SortedSet, NavigableSet, Queue, Deque or BlockingQueue, and a map literal assigned to SortedMap, NavigableMap or ConcurrentMap, now compile, as Set already did. The inferred type is the default implementation the runtime cast creates (GROOVY-12585), and the elements are checked against the declared type arguments. A list literal assigned to a concrete collection class with a constructor taking a collection, or a no-arg constructor, now copies the elements instead of passing them as constructor arguments. So LinkedList l = [3, 1] compiles, and under @CompileStatic Vector v = [5] holds the element rather than being empty with capacity 5, matching dynamic code. Using a literal to set a capacity, as in ArrayDeque<String> d = [16], is now an error. Other classes, such as Dimension, keep the list-constructor rule.
…ethod like an assigned one Under @TypeChecked and @CompileStatic, a list or map literal returned from a method with a declared return type was accepted only when the literal's own class (ArrayList or LinkedHashMap) is assignable to the return type, so Set m() { [3, 1] } and TreeMap m() { [b: 2, a: 1] } failed although the same literal assigned to a variable of that type compiles. A returned literal is now checked as "ReturnType x = literal" is: the Set exemption, the interface defaults and copy rule (GROOVY-12643) and the list and map constructor shorthand apply, and the elements are checked against the declared type arguments. Array return types keep their existing check. Closure and lambda results are unchanged, since they are not converted to the expected type at run time.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3165 +/- ##
==================================================
+ Coverage 74.1568% 74.1730% +0.0163%
- Complexity 43757 43792 +35
==================================================
Files 1692 1692
Lines 149517 149584 +67
Branches 28366 28383 +17
==================================================
+ Hits 110877 110951 +74
+ Misses 28446 28440 -6
+ Partials 10194 10193 -1
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: fd35106 Learn more about TestLens at testlens.app/docs. |
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.
Summary
Make collection and map conversions consistent between dynamic code,
@TypeCheckedand@CompileStatic. Three commits, one per JIRA, each with its own tests and COMPATIBILITY entry:as1. GROOVY-12585: runtime
Assignment and cast (
castToType) and coercion withas(asType) now share one table of default implementations:List,Collection,IterableArrayListSetLinkedHashSetSortedSet,NavigableSetTreeSetQueue,DequeLinkedListBlockingQueueLinkedBlockingQueueSortedMap,NavigableMapTreeMapConcurrentMapConcurrentHashMapSortedSet s = list,Deque d = list,Stack s = list,List l = aSet,SortedMap m = mapandIterable i = anArraynow succeed; they threwGroovyCastException.as NavigableSet,as Deque,as BlockingQueue,as SortedMap,as NavigableMapandas ConcurrentMapreturn a real collection (a copy) instead of a proxy that did not behave like the type.anArray as Setkeeps element order (LinkedHashSet, notHashSet).addAll, before passing the elements as constructor arguments. SoVector v = [5],PriorityQueue q = [5]andArrayDeque d = [5]hold the element instead of being empty with that capacity.aswith a concrete class keeps its own path (list as HashSetis aHashSet;HashSet h = listis still aLinkedHashSet). The GROOVY-7867 fallback order and its suppressed exception are also unchanged.2. GROOVY-12643: type checker, assigned literals
SortedSet,NavigableSet,Queue,DequeorBlockingQueue, and a map literal assigned toSortedMap,NavigableMaporConcurrentMap, now compile, asSetalready did. These have been compile errors since at least 2.4. The inferred type is the default implementation, and the elements are checked against the declared type arguments.LinkedList l = [3, 1]andStack s = [3, 1]compile, and under@CompileStatic,Vector v = [5]holds the element, as in dynamic code. Other classes keep the list-constructor rule (Point p = [1, 2],Dimension d = [100, 200]).@CompileStaticneeds no new code generation: the existing indy cast already goes throughcastToType.3. GROOVY-12644: type checker, returned literals
A list or map literal returned from a method was accepted only if the literal's own class was assignable to the return type, so
Set m() { [3, 1] }failed althoughSet s = [3, 1]compiles. A returned literal is now checked asReturnType x = literalis. This only accepts code that did not compile before. Array return types keep their existing check. Closure and lambda results are unchanged, because they are not converted to the expected type at run time.Compatibility
Most of this only turns failures into results. The changes in behaviour that reviewers should look at:
asfor the interfaces above returns a copy instead of a proxy over the original collection or map.anArray as Setkeeps it.Vector v = [10]now holds10, andArrayDeque<String> d = [16]is a compile error; writenew ArrayDeque(16)for a capacity.ArraysAndCollectionsSTCTestpinnedArrayDeque<String> deque = [123] // ArrayDeque(int numElements)from GROOVY-10002, and it now asserts the new behaviour. @eric-milles, does that look right to you?Each change has an entry in
COMPATIBILITY.md. The list and map constructor section ofcore-semantics.adocis updated.Tests
bugs.Groovy12585,bugs.Groovy12643andbugs.Groovy12644, 10 tests each. They cover dynamic code,@TypeCheckedand@CompileStatic, generics, and negative and unchanged-behaviour cases. Each fails without its commit's production change, except the guards for unchanged behaviour.ConstructorsSTCTest.testMapLiteral(SortedMap m = [:]), andArraysAndCollectionsSTCTest(theDeque/Queueliteral rejections and the capacity idiom).:testsuite passes on each commit.Related
castToType. It is independent of this PR and can land in either order. It closes the one indy-only case where assignment andasstill differ: an abstract subclass ofHashSetorArrayList.