[ 
https://issues.apache.org/jira/browse/GROOVY-12767?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145513#comment-18145513
 ] 

ASF GitHub Bot commented on GROOVY-12767:
-----------------------------------------

paulk-asert opened a new pull request, #3307:
URL: https://github.com/apache/groovy/pull/3307

   Under `@CompileStatic` or `@TypeChecked`, the loop variable of a `for await` 
loop was always typed `Object`: a typed loop variable was rejected and an 
untyped one could not call the element's methods.
   
   ```groovy
   @groovy.transform.CompileStatic
   void m(List<Integer> src) {
       for await (Integer x in src) { }       // Cannot loop with element of 
type java.lang.Integer
                                              //   with collection of type 
java.lang.Iterable<java.lang.Object>
       for await (x in src) { x.intValue() }  // Cannot find matching method 
java.lang.Object#intValue()
   }
   ```
   
   The parser rewrites the loop to iterate over 
`AsyncSupport.toIterable(source)`, declared `<T> Iterable<T> 
toIterable(Object)`, so nothing bound `T`. The type checker now gives that call 
the type `Iterable<E>`, where `E` is the element type of the source, and the 
ordinary `for` loop checks do the rest for typed and untyped loop variables.
   
   Sources covered: `Iterable<E>` (which includes `AsyncChannel<E>`), 
`Iterator<E>`, `E[]` and `Flow.Publisher<T>`. Any other source stays `Object`, 
as before.
   
   Notes for review:
   
   - The issue suggests recording the source expression as metadata on the 
`ForStatement` and reading it in `visitForLoop`. This types the generated call 
instead, because the source expression can be replaced during name resolution, 
which would leave the metadata pointing at a stale node. The cost is that the 
type checker recognises `AsyncSupport.toIterable` by name.
   - `Stream<E>`, listed in the issue, is not included: the runtime conversion 
does not accept a `Stream` without a registered adapter.
   - A mismatch is reported against `java.lang.Iterable<java.lang.Integer>` 
rather than the declared type of the source.
   - No runtime methods or generated calls change, so code compiled with this 
still runs on a 6.0.0 runtime. Candidate for `GROOVY_6_0_X`.
   
   Tests: three in `LoopsSTCTest`, inherited by `LoopsStaticCompileTest`. The 
two element-type tests fail without the change. `groovy.transform.stc.*`, 
`org.codehaus.groovy.classgen.asm.sc.*`, `*Async*` and `groovy.concurrent.*` 
pass locally (6712 tests); the full suite was not run locally.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   




> STC: the element type of a for await loop is always Object
> ----------------------------------------------------------
>
>                 Key: GROOVY-12767
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12767
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Priority: Major
>
> Under {{@CompileStatic}} or {{@TypeChecked}}, the loop variable of a {{for 
> await}} loop is typed {{Object}}, whatever the source. A typed loop variable 
> is rejected, and an untyped one can't call the element's methods. The 
> equivalent plain {{for}} loop compiles.
> {code:groovy}
> @groovy.transform.CompileStatic
> void m() {
>     List<Integer> src = [1, 2]
>     for (Integer x in src) { }          // ok
>     for await (Integer x in src) { }    // [Static type checking] - Cannot 
> loop with element of type java.lang.Integer
>                                         //   with collection of type 
> java.lang.Iterable<java.lang.Object>
>     for await (x in src) { x.intValue() }  // Cannot find matching method 
> java.lang.Object#intValue()
> }
> {code}
> The same happens for {{Iterator<Integer>}}, {{Integer[]}}, 
> {{AsyncChannel<Integer>}} and {{Flow.Publisher<Integer>}} (for example 
> {{SubmissionPublisher<Integer>}}). Seen on 6.0.0 and master.
> Cause: the parser rewrites the loop to iterate over 
> {{AsyncSupport.toIterable(source)}} 
> ({{AsyncTransformHelper.wrapForAwaitLoop}}), declared as {{<T> Iterable<T> 
> toIterable(Object source)}}. Nothing binds {{T}}, so the type checker sees 
> {{Iterable<Object>}}. The error message also names that rewritten type rather 
> than the type of the source.
> Expected: the element type of the source, as for a plain {{for}} loop: {{E}} 
> for an {{Iterable<E>}}, {{Iterator<E>}}, {{E[]}} or {{Stream<E>}} (an 
> {{AsyncChannel<E>}} is an {{Iterable<E>}}), and {{T}} for a 
> {{Flow.Publisher<T>}}. Other sources, such as types handled through an 
> {{AwaitableAdapter}}, stay {{Object}}.
> Possible fixes:
> * *Type checker* (preferred): {{wrapForAwaitLoop}} records the original 
> source expression on the {{ForStatement}} as node metadata, and 
> {{StaticTypeCheckingVisitor.visitForLoop}} takes the element type from that 
> expression's type: {{inferLoopElementType}} as for a plain loop, plus 
> {{Flow.Publisher<T>}}. Covers typed and untyped loop variables and corrects 
> the error message. It changes no runtime methods or generated calls, so code 
> compiled with the fix still runs on a 6.0.0 runtime.
> * *Typed temporary variable*: declare the temporary as {{Iterable<V>}} when 
> the loop variable has a type {{V}}. A smaller change, but an untyped loop 
> variable stays {{Object}}.
> * *Typed {{toIterable}} overloads* ({{Iterable<T>}}, {{Iterator<T>}}, 
> {{T[]}}, {{Flow.Publisher<T>}}): new {{@GroovyABI}} methods, so statically 
> compiled code would need the newer runtime.
> Workaround: use an untyped loop variable and cast in the body.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to