codeconsole opened a new pull request, #3245:
URL: https://github.com/apache/groovy/pull/3245

   Second part of GROOVY-12713, on top of #3244 (whose commit this branch 
contains; the diff against master shows both until that one is merged).
   
   `copyWith { name = old.name.toUpperCase(); address.city = 'x' }` runs its 
closure against a `CopyWithRecorder` (`NestedCopyWithSupport.applyBlock`) that 
turns the assignments into the map form, so under `@TypeChecked` and 
`@CompileStatic` every name in the block was "The variable [address] is 
undeclared.", while the same block worked dynamically.
   
   The type checker now recognises the generated `copyWith(Closure)` of a type 
whose `copyWith(Map)` declares its components as `@NamedParam`s (the same test 
as for the dotted keys), makes that type the closure's delegate 
(`DELEGATE_FIRST`), and inside the block:
   
   - a bare component name (`name`), a component path (`company.address.zip`) 
and `old` are typed as the declared component types and the copied type, so an 
assignment is checked against the component's type (`Cannot assign value of 
type java.lang.String to variable of type int`) and an unknown component is 
still `No such property: state for class: Address`;
   - `path.modify { it ... }` is accepted, with the component's type as the 
closure's parameter type;
   - all of those are marked for dynamic resolution, so static compilation 
dispatches them through the closure at run time, where the recorder records 
them; reads through `old` stay static, since `old` is the real object.
   
   Nothing changes outside such a block: the marker lives on the block's 
closure for the duration of its visit, and the lookup of the type's named 
parameters is the helper #3244 added.
   
   Tests: `RecordNestedCopyWithTest` gains the block under `@CompileStatic` 
with writes at three depths, `old` and `modify` (and runs the result), a 
wrongly typed value and an unknown component; `ImmutableNestedCopyWithTest` the 
block under `@TypeChecked`. On JDK 21 those two classes, 
`groovy.transform.stc.*`, `org.codehaus.groovy.classgen.asm.sc.*`, 
`NamedParameterTest`, `ImmutableTransformTest` and `RecordTest` pass (4,845 
tests).
   
   Candidate for `GROOVY_6_0_X` together with #3244.
   


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