GROOVY-7971, GROOVY-8411, GROOVY-8965: logical-or instanceof guards
#2353
+230
−181
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.
NOTE: I still have some refactoring to do to make the push/pop/peek interactions with the temp type info data structure a bit better. I thought I would get this in front of you to see the solution design. I also try not to use union and intersection to maintain clarity.
see also #1293 and #1269
Treat both of these cases as typeof(x) is (String | Number | Pattern) for STC. Maybe extend to
catch (Exception | Error e).The
UnionTypeClassNoderepresents the any-of type alternatives, as described in its javadoc. So use it for that, not for all-of types.LowestUpperBoundClassNodeis one type that represents (Type & Serializable) for example. I did not want to overload its usage, so I opted to create aClassNodeinstance with abstract modifier, Type as the super class, and Serializable as one interface. SeegetInferredTypeFromTempInfo-- this is the location whereUnionTypeClassNodewas being used to represent&relationship.The duck-typing is in place for a
UnionTypeClassNode. Thus 8965 (Deque and Stack peek) keeps working. I modified the switch case handling to collect types together in a union so the temp types list represents the and relationship and any element of the list may represent an or relationship (that is be anUnionTypeClassNode).When processing a logical-or binary expression, temp types are collected for left and right sides. If an
instanceofexpression appears on either side, a (T | U) type is created and added to the temp type list. The logical-and binary expression remanins as before, adding anyinstanceoftypes from left or right expression directly to the temp type list as strong guarantees.