codeconsole opened a new pull request, #3244:
URL: https://github.com/apache/groovy/pull/3244
The `copyWith(Map)` that `@RecordType` and `@Immutable` generate accepts
dotted keys such as `'address.city'` since GROOVY-12015, but the named-argument
check of the static type checker
(`StaticTypeCheckingVisitor.checkNamedParamsAnnotation`) reported every key
that is not one of the parameter's `@NamedParam`s, so the nested form failed
with `unexpected named arg: address.city` under `@TypeChecked` and
`@CompileStatic` while it worked dynamically.
The check now walks a dotted key: each segment must be 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), an unknown
segment is reported as before (`unexpected named arg: address.state`), and the
value is checked against the last segment's declared type (`argument for named
param 'company.address.zip' has type 'int' but expected 'java.lang.String'`).
The walk stops, accepting the key, at a type whose `copyWith(Map)` declares no
named parameters, and the runtime (`NestedCopyWithSupport.flatten`) checks the
rest. Keys without a dot are checked exactly as before; the `@NamedParam`
collection is factored into a helper shared by both paths.
Tests: `RecordNestedCopyWithTest` and `ImmutableNestedCopyWithTest` gain a
statically compiled method that uses two nested keys, an unknown nested segment
and a wrongly typed nested value; those two classes and `NamedParameterTest`
pass on JDK 21.
Not in this change: the block form, `copyWith { address.city = 'x' }`, still
fails static type checking ("The variable [address] is undeclared"), because
the closure runs with a `CopyWithRecorder` delegate the type checker does not
model. GROOVY-12713 keeps that part open.
Candidate for `GROOVY_6_0_X` as well.
--
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]