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)
   


-- 
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]

Reply via email to