[
https://issues.apache.org/jira/browse/GROOVY-12713?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18139729#comment-18139729
]
ASF GitHub Bot commented on GROOVY-12713:
-----------------------------------------
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.
> Static type checking rejects nested copyWith: dotted keys are "unexpected
> named arg" and the block form's properties are undeclared
> -----------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12713
> URL: https://issues.apache.org/jira/browse/GROOVY-12713
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 6.0.0
> Environment: Groovy 6.0.0 binary distribution and master 7379c5720d;
> BellSoft JDK 21.0.12; macOS 15 arm64
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> GROOVY-12015 (6.0.0-alpha-2) gave the generated {{copyWith}} of records and
> {{@Immutable}} classes dotted-path keys ({{copyWith('address.city':
> 'Paris')}}) and a block form ({{copyWith { address.city = 'Paris' }}}). Both
> work in dynamic code; neither passes static type checking, while the
> single-level named-argument form does. On 6.0.0 and master:
> {code:groovy}
> import groovy.transform.CompileStatic
> import groovy.transform.RecordOptions
> @RecordOptions(copyWith = true)
> record Address(String street, String city) {}
> @RecordOptions(copyWith = true)
> record Customer(String name, Address address) {}
> @CompileStatic
> class Demo {
> static void main(String[] args) {
> Customer c = new Customer('Ada', new Address('1 Main St', 'Lyon'))
> println c.copyWith(name: 'Bob') // accepted
> println c.copyWith('address.city': 'Paris') // line 14
> println c.copyWith { address.city = 'Rome' } // line 15
> }
> }
> {code}
> {noformat}
> Copy.groovy: 14: [Static type checking] - unexpected named arg: address.city
> Copy.groovy: 15: [Static type checking] - The variable [address] is
> undeclared.
> {noformat}
> Without {{@CompileStatic}} the same file prints the three copies.
> Causes: the record and immutable transforms annotate the generated
> {{copyWith(Map)}} parameter with one {{@NamedParam}} per component, and
> {{StaticTypeCheckingVisitor.checkNamedParamsAnnotation}} reports any key that
> is not one of them, so a dotted path is "unexpected" although the generated
> method accepts it ({{NestedCopyWithSupport.flatten}} resolves it at run
> time). The block form's closure runs with a {{CopyWithRecorder}} delegate
> that the type checker knows nothing about, so the assignments' targets are
> undeclared variables. The documentation of {{RecordOptions.copyWith}}
> describes both forms without a static-typing caveat.
> Suggested fix for the dotted keys: in the named-argument check, accept a
> dotted key when each segment is a named parameter of the type reached so far
> (the {{type}} member of the previous segment's {{@NamedParam}}, whose own
> {{copyWith(Map)}} declares the next level), report an unknown segment as
> before, and check the value against the last segment's declared type. Pull
> request to follow. The block form needs the type checker to model the
> recorder (the record type as the closure's delegate, with writes to its
> read-only properties and {{old}} allowed inside that block); that is a
> follow-up.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)