codeconsole opened a new pull request, #15720:
URL: https://github.com/apache/grails-core/pull/15720

   ## What
   
   `CriteriaTypeCheckingExtension` exists in grails-core but is **not 
registered** in `@GrailsCompileStatic`'s extension list, and `isCriteriaCall` 
only recognizes the `Domain.withCriteria { … }` / `Domain.createCriteria()` 
forms (closure as the direct argument). The chained 
`Domain.createCriteria().<terminal> { … }` form — where the closure is the 
argument to a terminal called *on the builder* — is never matched, so a 
criteria scope is never opened for it.
   
   ## Why it matters
   
   Most chained terminals (`list`, `listDistinct`, `get`, `scroll`) still 
type-check today because `BuildableCriteria` declares them with a `Closure` 
parameter and `@DelegatesTo` resolves the closure body. But `count(Closure)` is 
**not** declared on `BuildableCriteria`, so:
   
   ```groovy
   @GrailsCompileStatic
   class Foo {
       def c() { Person.createCriteria().count { eq 'name', 'Anakin' } }
   }
   ```
   
   fails static compilation on stock 8.0.x:
   
   ```
   [Static type checking] - Cannot find matching method
   
org.grails.datastore.mapping.query.api.BuildableCriteria#count(groovy.lang.Closure)
   ```
   
   forcing a `@CompileDynamic` escape hatch.
   
   ## Change
   
   1. Extend `isCriteriaCall` to also open a criteria scope when the call is a 
terminal (`list`, `listDistinct`, `get`, `count`, `scroll`) chained directly on 
`Domain.createCriteria()`. Registration alone is not enough — the 
`createCriteria()` scope is exited (via `afterMethodCall`) before the terminal 
call is visited, so the terminal needs to be recognized in its own right.
   2. Add `org.grails.compiler.CriteriaTypeCheckingExtension` to the 
`@CompileStatic(extensions=[…])` list on `@GrailsCompileStatic`.
   
   ## Test
   
   Adds `'Test compiling a class which invokes a chained createCriteria() 
terminal on a domain class'` to `GrailsCompileStaticCompilationErrorsSpec`, 
covering all six chained terminals. It **fails on stock 8.0.x** (on the `count` 
terminal) and **passes with this change**. The existing `GRAILS-11255` criteria 
spec continues to pass.
   
   ## Scope / limits
   
   The extension makes the *direct children* of the criteria closure dynamic. 
Deeply nested builder closures (e.g. `projections { property '…' }`) and 
post-processing that indexes the result (`withCriteria { … }[0]`) still require 
`@CompileDynamic` — out of scope here.


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