Repository navigation
GROOVY-12693: STC: misplaced @TypeChecked/@CompileStatic gets the target diagnostic, not "Unimplemented node type" - #3214
Merged
paulk-asert merged 1 commit intoNov 6, 2026
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 57b0bbf Learn more about TestLens at testlens.app/docs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Placing
@TypeCheckedor@CompileStaticon 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 aClassCastException(ForStatement cannot be cast to AnnotatedNode), reported as a general compiler error.Both annotations declare
@Target({TYPE, METHOD, CONSTRUCTOR})andExtendedVerifierchecks that target for every placement, but it runs at class generation.StaticTypesTransformation.visitandStaticCompileTransformation.visitrun earlier, at instruction selection; they castnodes[1]toAnnotatedNodeand 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
ClassNodeorMethodNodeas before and otherwise return without doing anything, so a misplaced annotation reachesExtendedVerifierand is reported as "Annotation @groovy.transform.CompileStatic is not allowed on element LOCAL_VARIABLE" (or FIELD, PARAMETER, STATEMENT), exactly like@Overridein the same position and at the same source position as before.nodes[1]is no longer cast toAnnotatedNode, which also removes the crash for loop statements. The existing special case that ignored the annotation on aPropertyNode(GROOVY-9993) is subsumed by the general case. The unusedSyntaxException,AnnotatedNodeandPropertyNodeimports are removed;STATIC_ERROR_PREFIXstays, 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), whichBugsStaticCompileTestinherits so both transformations are exercised. They fail on master with "Unimplemented node type" and pass with the change. Also run: thegroovy.transform.stc,org.codehaus.groovy.classgen.asm.scandgls.annotationstest packages andcheckstyleMain. Verified with the command line tools on the installed distribution:groovyandgroovyc --check --error-format=shortnow print the target diagnostic for all the placements listed above.Not covered here: inside a
@CompileStaticclass, annotations on a multiple-assignment declaration (@X def (a, b) = [1, 2]) are dropped byBinaryExpressionTransformer.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.