Skip to content

GROOVY-12713: type check dotted named arguments of nested copyWith - #3244

Open
codeconsole wants to merge 3 commits into
apache:masterfrom
codeconsole:fix/groovy-12713-copywith-nested-stc
Open

codeconsole wants to merge 3 commits into
apache:masterfrom
codeconsole:fix/groovy-12713-copywith-nested-stc

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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 @NamedParams, 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.

Dotted keys are read as paths only for that generated copyWith(Map) (a @Generated method named copyWith). For any other @NamedParam method, 'a.b' is still unexpected named arg: a.b, as before; RecordNestedCopyWithTest.dotted_key_is_not_a_path_for_other_named_param_methods covers that.

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.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@codeconsole
codeconsole force-pushed the fix/groovy-12713-copywith-nested-stc branch from f3e04f7 to ffe0276 Compare November 6, 2026 21:57
@codeconsole

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (5170b46, which has #3247) and pushed; the rebase was clean and RecordNestedCopyWithTest, ImmutableNestedCopyWithTest and NamedParameterTest pass locally on the rebased branch.

@codecov-commenter

codecov-commenter commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.10169% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4140%. Comparing base (5170b46) to head (e58dc69).
⚠️ Report is 32 commits behind head on master.

Files with missing lines Patch % Lines
...roovy/transform/stc/StaticTypeCheckingVisitor.java 66.1017% 3 Missing and 17 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                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     
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.9184% <66.1017%> (-0.1269%) ⬇️

... and 44 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@testlens-app

testlens-app Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: e58dc69
▶️ Tests: 36395 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants