daniellansun commented on code in PR #3307:
URL: https://github.com/apache/groovy/pull/3307#discussion_r4614518121
##########
src/test/groovy/groovy/transform/stc/LoopsSTCTest.groovy:
##########
@@ -456,4 +456,76 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
}
'''
}
+
+ // GROOVY-12767
+ @Test
+ void testForAwaitLoopElementType() {
+ assertScript '''
+ import java.util.concurrent.Flow
+
+ int fromList(List<Integer> source) {
+ int sum = 0
+ for await (Integer x in source) { sum += x }
+ for await (x in source) { sum += x.intValue() }
+ sum
+ }
+ int fromIterator(Iterator<Integer> source) {
+ int sum = 0
+ for await (x in source) { sum += x.intValue() }
+ sum
+ }
+ int fromArray(Integer[] source) {
+ int sum = 0
+ for await (int x in source) { sum += x }
+ for await (x in source) { sum += x.intValue() }
+ sum
+ }
+ int fromPublisher(Flow.Publisher<Integer> source) {
Review Comment:
Optional, not blocking: this method is type-checked, which is enough to pin
the `Publisher` typings, but it is never called. If you want a runtime case as
well, a tiny re-subscribable stub is safer than `SubmissionPublisher`: the
built-in adapter's iterable is single-use, so two `for await` loops over the
same publisher instance would not be a fair test.
##########
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java:
##########
@@ -3417,6 +3421,36 @@ public static ClassNode inferLoopElementType(final
ClassNode collectionType) {
return componentType;
}
+ /**
+ * Returns {@code Iterable<E>} for the call a {@code for await} loop makes
to
+ * convert its source, {@code AsyncSupport.toIterable(source)}, where
{@code E}
+ * is the element type of an array, {@code Iterable}, {@code Iterator} or
+ * {@code Flow.Publisher} source. The method declares {@code <T>
Iterable<T>}
+ * and takes an {@code Object}, so nothing else gives {@code T} a type.
+ *
+ * @return the type, or {@code null} for another method or kind of source
+ */
+ private static ClassNode forAwaitIterableType(final MethodNode method,
final ClassNode[] argumentTypes) {
+ if (argumentTypes.length != 1 || !"toIterable".equals(method.getName())
+ || !ASYNC_SUPPORT_TYPE.equals(method.getDeclaringClass())) {
+ return null;
+ }
+ ClassNode sourceType = argumentTypes[0];
+ ClassNode elementType;
+ if (sourceType.isArray()) {
+ elementType = sourceType.getComponentType();
+ if (isPrimitiveType(elementType)) return null; // not a source the
conversion accepts
Review Comment:
Optional, not blocking: this fail-closed arm matches `toIterable`'s
`instanceof Object[]` check, so a primitive array correctly stays untyped. A
small negative test (`for await (int x in int[])` rejected / untyped `x` stays
`Object`) would cover the branch Codecov reports as partial. The javadoc's "an
array" is also a little broader than the implementation; "non-primitive array"
would match the code.
--
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]