[
https://issues.apache.org/jira/browse/GROOVY-12688?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King resolved GROOVY-12688.
--------------------------------
Fix Version/s: 7.0.0-alpha-1
6.0.1
Assignee: Paul King
Resolution: Fixed
> FormatStringChecker fails inside the compiler on enum and record declarations
> -----------------------------------------------------------------------------
>
> Key: GROOVY-12688
> URL: https://issues.apache.org/jira/browse/GROOVY-12688
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 6.0.0
> Reporter: Scott Murphy Heiberg
> Assignee: Paul King
> Priority: Major
> Fix For: 7.0.0-alpha-1, 6.0.1
>
>
> With {{groovy.typecheckers.FormatStringChecker}} enabled, a class that
> declares an enum or a record does not compile: the checker throws a
> {{NullPointerException}} on a constructor call the compiler generated, and
> the compiler reports a general error. The source contains no format call.
> {{Size.groovy}} and {{Point.groovy}}:
> {code:groovy}
> import groovy.transform.TypeChecked
> @TypeChecked(extensions = 'groovy.typecheckers.FormatStringChecker')
> enum Size { S, M }
> {code}
> {code:groovy}
> import groovy.transform.TypeChecked
> @TypeChecked(extensions = 'groovy.typecheckers.FormatStringChecker')
> record Point(int x, int y) {}
> {code}
> {code:sh}
> groovyc --check --error-format=short Size.groovy
> groovyc --check --error-format=short Point.groovy
> {code}
> Actual, for each: exit 1 and
> {code:text}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
> failed:
> error: General error during canonicalization: Cannot get property
> 'annotations' on null object
> 1 error
> {code}
> With {{groovyc -e --check Size.groovy}}:
> {code:text}
> java.lang.NullPointerException: Cannot get property 'annotations' on null
> object
> at
> org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:99)
> at
> groovy.typecheckers.FormatStringChecker$_run_closure8.doCall(FormatStringChecker.groovy:100)
> at groovy.lang.Closure.invokeHandle(Closure.java:647)
> at groovy.lang.Closure.invokeCached(Closure.java:618)
> at groovy.lang.Closure.call(Closure.java:584)
> at
> org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension.safeCall(AbstractTypeCheckingExtension.java:253)
> at
> org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.afterMethodCall(GroovyTypeCheckingExtensionSupport.java:364)
> at
> org.codehaus.groovy.transform.stc.DefaultTypeCheckingExtension.afterMethodCall(DefaultTypeCheckingExtension.java:192)
> at
> org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorCallExpression(StaticTypeCheckingVisitor.java:3288)
> at
> org.codehaus.groovy.ast.expr.ConstructorCallExpression.visit(ConstructorCallExpression.java:74)
> at
> org.codehaus.groovy.ast.CodeVisitorSupport.visitReturnStatement(CodeVisitorSupport.java:181)
> ...
> {code}
> Expected: both compile, as {{@TypeChecked enum PlainEnum { S, M }}} does
> (exit 0). The same failure appears under static compilation applied by a
> config script ({{ast(groovy.transform.CompileStatic, extensions:
> ['groovy.typecheckers.FormatStringChecker'])}}) to a script containing {{enum
> Size { S, M }}} and {{println Size.M}}, and when the enum or record is nested
> in a checked class. 6.0.0-beta-1, 5.0.8 and 5.0.0 fail the same way, and so
> does current master; this is not a late regression.
> Cause: when the type checker cannot resolve a constructor call in generated
> code (an enum's synthetic {{$INIT}} delegation, or a record's named-argument
> constructor delegation) it drops the error silently and stores no target
> method, but still fires {{afterMethodCall}}, where FormatStringChecker reads
> {{annotations}} from that null target (line 100). Methods without a body
> (abstract or interface methods, or an enum with constant bodies) crash a
> second way, {{method.code.visit}} on a null {{code}}, which also affects
> RegexChecker. The other checkers in the module guard their target lookups and
> are not affected.
> A fix that works: return early from {{afterMethodCall}} when the target
> method is null, and visit {{method.code}} only when it exists, in
> FormatStringChecker and RegexChecker. With it, enums, records, nested
> declarations and body-less methods compile cleanly under both checkers, and a
> genuine format-string mismatch in such a class is still reported. A pull
> request with regression tests in {{FormatStringCheckerTest}} and
> {{RegexCheckerTest}} follows.
> Impact: FormatStringChecker was graduated to stable for 6.0.0 (GROOVY-12039).
> Any checked class that declares an enum or record cannot be compiled with it.
> Workaround: leave FormatStringChecker off for such sources.
> Environment: Groovy 6.0.0 on BellSoft JDK 21.0.12, macOS 26.5 (arm64);
> reproduced on master at 9a1a0d0ccb with JDK 21.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)