Skip to content

GROOVY-12693: STC: misplaced @TypeChecked/@CompileStatic gets the target diagnostic, not "Unimplemented node type" - #3214

Merged
paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:fix/groovy-12693-stc-annotation-on-local
Nov 6, 2026
Merged

paulk-asert merged 1 commit into
apache:masterfrom
codeconsole:fix/groovy-12693-stc-annotation-on-local

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Placing @TypeChecked or @CompileStatic on a local variable declaration failed with "[Static type checking] - Unimplemented node type" instead of the standard annotation target diagnostic. The same happened on a field, a property or a parameter, and on a loop statement the transformation threw a ClassCastException (ForStatement cannot be cast to AnnotatedNode), reported as a general compiler error.

Both annotations declare @Target({TYPE, METHOD, CONSTRUCTOR}) and ExtendedVerifier checks that target for every placement, but it runs at class generation. StaticTypesTransformation.visit and StaticCompileTransformation.visit run earlier, at instruction selection; they cast nodes[1] to AnnotatedNode and added their own "Unimplemented node type" error for anything other than a class, method or property node, so compilation stopped before the verifier could report the actual mistake.

The change makes the two transformations handle a ClassNode or MethodNode as before and otherwise return without doing anything, so a misplaced annotation reaches ExtendedVerifier and is reported as "Annotation @groovy.transform.CompileStatic is not allowed on element LOCAL_VARIABLE" (or FIELD, PARAMETER, STATEMENT), exactly like @Override in the same position and at the same source position as before. nodes[1] is no longer cast to AnnotatedNode, which also removes the crash for loop statements. The existing special case that ignored the annotation on a PropertyNode (GROOVY-9993) is subsumed by the general case. The unused SyntaxException, AnnotatedNode and PropertyNode imports are removed; STATIC_ERROR_PREFIX stays, it is used elsewhere.

Tests: two regression tests in BugsSTCTest (local variable at script level and in a method body; field, parameter and loop statement), which BugsStaticCompileTest inherits so both transformations are exercised. They fail on master with "Unimplemented node type" and pass with the change. Also run: the groovy.transform.stc, org.codehaus.groovy.classgen.asm.sc and gls.annotations test packages and checkstyleMain. Verified with the command line tools on the installed distribution: groovy and groovyc --check --error-format=short now print the target diagnostic for all the placements listed above.

Not covered here: inside a @CompileStatic class, annotations on a multiple-assignment declaration (@X def (a, b) = [1, 2]) are dropped by BinaryExpressionTransformer.transformMultipleAssignment, which rebuilds the declaration without them, so no target error is reported for that form even for @Override; that is an existing, separate defect.

Suggested backport: GROOVY_6_0_X.

…get diagnostic, not "Unimplemented node type"

Both annotations target types, methods and constructors, and ExtendedVerifier
reports any other placement, but it runs at class generation. The two
transformations run at instruction selection, cast the annotated node to
AnnotatedNode and added their own "Unimplemented node type" error for anything
but a class, method or property node, so a local variable, field or parameter
never reached the verifier, and a loop statement threw a ClassCastException.

Handle class and method nodes as before and otherwise return, so the verifier
reports "Annotation @groovy.transform.CompileStatic is not allowed on element
LOCAL_VARIABLE" (or FIELD, PARAMETER, STATEMENT) at the same position, as it
does for every other annotation.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.1620%. Comparing base (d86ea7e) to head (57b0bbf).

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3214        +/-   ##
==================================================
+ Coverage     74.1563%   74.1620%   +0.0057%     
- Complexity      43738      43745         +7     
==================================================
  Files            1692       1692                
  Lines          149549     149543         -6     
  Branches        28371      28367         -4     
==================================================
+ Hits           110900     110904         +4     
+ Misses          28437      28433         -4     
+ Partials        10212      10206         -6     
Files with missing lines Coverage Δ
...us/groovy/transform/StaticTypesTransformation.java 96.2963% <100.0000%> (+8.3653%) ⬆️
...oovy/transform/sc/StaticCompileTransformation.java 97.5610% <100.0000%> (+9.1889%) ⬆️

... and 16 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.

@testlens-app

testlens-app Bot commented Nov 5, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 57b0bbf
▶️ Tests: 127104 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@paulk-asert
paulk-asert merged commit 8263945 into apache:master Nov 6, 2026
27 checks passed
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