Repository navigation
GROOVY-12713: type check dotted named arguments of nested copyWith - #3244
codeconsole wants to merge 3 commits into
Conversation
daniellansun
left a comment
There was a problem hiding this comment.
Build-and-test is red with the two ParserNegativeSyntaxTest failures that were broken on master after GROOVY-12641 (GEP-19). Those fixtures have been fixed on current master (see #3247).
Please rebase onto the latest master and push so CI can re-run against that tip — that should clear this noise. If anything is still red after the rebase, we can dig into it then.
The copyWith(Map) that @RecordType and @immutable generate accepts dotted keys ('address.city') since GROOVY-12015, but the named-argument check of the static type checker reported every key that is not one of the parameter's @NamedParams, so the nested form failed with "unexpected named arg" under @TypeChecked and @CompileStatic while it worked dynamically. checkNamedParamsAnnotation now walks a dotted key: each segment must be a named parameter of the type reached so far (the @NamedParam type of the previous segment, whose own copyWith(Map) declares the next level), an unknown segment is reported as before, and the value is checked against the last segment's declared type. The walk stops, accepting the key, at a type whose copyWith(Map) declares no named parameters; the runtime checks the rest. Keys without a dot are checked exactly as before. Tests in RecordNestedCopyWithTest and ImmutableNestedCopyWithTest: a statically compiled method using two nested keys, an unknown nested segment ("unexpected named arg: address.state") and a wrongly typed value for a nested key. The block form (copyWith { address.city = 'x' }) is still dynamic only; the issue tracks it.
f3e04f7 to
ffe0276
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3244 +/- ##
==================================================
+ Coverage 74.3428% 74.4140% +0.0712%
- Complexity 44310 44392 +82
==================================================
Files 1699 1699
Lines 150862 151098 +236
Branches 28756 28802 +46
==================================================
+ Hits 112155 112438 +283
+ Misses 28435 28382 -53
- Partials 10272 10278 +6
🚀 New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for the rebase onto current master. Tip ffe027608665 is green, and the GROOVY-12713 dotted-key path looks solid.
Refactoring @NamedParam collection into namedParamAnnotations / a name→type map, then walking dotted keys with checkNestedNamedParam + copyWithNamedParams, matches how nested copyWith(Map) already behaves at run time. Stopping the walk when a segment’s type has no named-param copyWith (and letting the runtime finish the check) is a sensible boundary. The record and @Immutable STC tests cover acceptance, unknown segments, and leaf-type mismatch.
Happy to approve this tip; please land it before #3245, which stacks on top.
✅ All tests passed ✅🏷️ Commit: e58dc69 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks, Scott. I re-reviewed the unique static type checking change on tip e58dc69004fd.
Walking a dotted key as a nested path only for a generated copyWith(Map) is the right narrowing. The earlier walk would have treated 'a.b' as a path for any @NamedParam method, which those methods do not accept at run time. isNestedCopyWith (the name copyWith plus @Generated) keeps the check aligned with the record and @Immutable transforms. Unknown segments and leaf types are still reported as before, and the new test covers a plain @NamedParam method.
Looks good to me.
The
copyWith(Map)that@RecordTypeand@Immutablegenerate 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@NamedParams, so the nested form failed withunexpected named arg: address.cityunder@TypeCheckedand@CompileStaticwhile it worked dynamically.The check now walks a dotted key: each segment must be a named parameter of the type reached so far (the
typemember of the previous segment's@NamedParam, whose owncopyWith(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 whosecopyWith(Map)declares no named parameters, and the runtime (NestedCopyWithSupport.flatten) checks the rest. Keys without a dot are checked exactly as before; the@NamedParamcollection is factored into a helper shared by both paths.Dotted keys are read as paths only for that generated
copyWith(Map)(a@Generatedmethod namedcopyWith). For any other@NamedParammethod,'a.b'is stillunexpected named arg: a.b, as before;RecordNestedCopyWithTest.dotted_key_is_not_a_path_for_other_named_param_methodscovers that.Tests:
RecordNestedCopyWithTestandImmutableNestedCopyWithTestgain a statically compiled method that uses two nested keys, an unknown nested segment and a wrongly typed nested value; those two classes andNamedParameterTestpass 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 aCopyWithRecorderdelegate the type checker does not model. GROOVY-12713 keeps that part open.Candidate for
GROOVY_6_0_Xas well.