[
https://issues.apache.org/jira/browse/GROOVY-12688?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137754#comment-18137754
]
ASF GitHub Bot commented on GROOVY-12688:
-----------------------------------------
codeconsole opened a new pull request, #3207:
URL: https://github.com/apache/groovy/pull/3207
JIRA: [GROOVY-12688](https://issues.apache.org/jira/browse/GROOVY-12688)
### The bug
With `groovy.typecheckers.FormatStringChecker` enabled, a class that
declares an enum or a record does not compile. The checker throws a
`NullPointerException` inside the compiler and compilation ends with a general
error, even though the source contains no format call:
```text
error: General error during canonicalization: Cannot get property
'annotations' on null object
```
The crash also stops the checker before it reaches the rest of the class, so
a real format-string mistake in a record method goes unreported. A second,
related failure happens in methods without a body: an abstract method, an
interface method, or the abstract method of an enum with constant bodies fails
with `Cannot invoke method visit() on null object`. That one affects
`RegexChecker` as well.
### Reproducer
```groovy
import groovy.transform.TypeChecked
@TypeChecked(extensions = 'groovy.typecheckers.FormatStringChecker')
enum Size { S, M }
```
```groovy
import groovy.transform.TypeChecked
@TypeChecked(extensions = 'groovy.typecheckers.FormatStringChecker')
record Point(int x, int y) {}
```
`groovyc --check Size.groovy` (or `Point.groovy`) exits 1 with the error
above, while `@TypeChecked enum Size { S, M }` without the extension compiles.
The same failure happens when the enum or record is nested in a checked class.
It also happens when static compilation with the extension is applied from a
config script (`ast(CompileStatic, extensions:
['groovy.typecheckers.FormatStringChecker'])`) to a script that declares an
enum.
The failure in methods without a body:
```groovy
@groovy.transform.TypeChecked(extensions =
'groovy.typecheckers.RegexChecker')
enum Op {
PLUS { int apply(int a, int b) { a + b } }
abstract int apply(int a, int b)
}
```
### Cause
When the type checker cannot resolve a constructor call in generated code,
it stores no target method: it drops errors for nodes that have no source
position. It still fires `afterMethodCall`, and `getTargetMethod` is documented
to return the selected target "if any". In an enum, that call is the synthetic
`$INIT` helper's `this(*para)`. In a record, it is the `this(...)` call by
which the named-argument constructor delegates. `FormatStringChecker` read
`annotations` from the null target.
Separately, `beforeVisitMethod` and `afterVisitMethod` fire for every
method, including abstract and interface methods, which have no code.
`FormatStringChecker` and `RegexChecker` called `method.code.visit(...)`
without a null check.
### Fix
- `FormatStringChecker`: return from the `afterMethodCall` handler when the
call has no target method, and visit `method.code` null-safely.
- `RegexChecker`: visit `method.code` null-safely in both hooks.
In both checkers this just skips what they cannot analyse. `NullChecker`,
`SqlInjectionChecker` and `PurityChecker` already call
`method.code?.visit(...)` and check their target lookups. I ran every other
checker in the module (`NullChecker`, `SqlInjectionChecker`, `PurityChecker`,
`ModifiesChecker`, `CombinerChecker`, `MonadicChecker`, `MonadicShapeChecker`)
against an enum, a record, an enum with constant bodies and an interface. None
of them fails, before or after this change.
### Tests
New tests in `FormatStringCheckerTest` and `RegexCheckerTest`:
- `testEnumAndRecordDeclarations`: an enum and a record compile in two
forms: at script level, with the extension applied by the test shell's
customizer, and nested in a class annotated `@TypeChecked(extensions =
'groovy.typecheckers.FormatStringChecker')`.
- `testFormatErrorsReportedWithEnumAndRecordDeclarations`: in such a class,
mismatches in the class itself, in the enum and in the record are all reported,
with no general error. Before the fix, the record's mismatch was not reported.
- `testMethodsWithoutBody` (in both test classes): an interface and an enum
with constant bodies compile, and a mistake inside a constant body is still
reported.
All four tests failed on master before the change and pass after it.
```text
./gradlew :groovy-typecheckers:test --rerun 390 tests, 0
failures
./gradlew :groovy-typecheckers:lintGate :groovy-typecheckers:rat BUILD
SUCCESSFUL
```
### Affected versions
Reproduced with JDK 21 on the Groovy 6.0.0 and 6.0.0-beta-1 distributions
and on master (7.0.0-SNAPSHOT at 9a1a0d0ccb). The same failures reproduce with
5.0.0 and 5.0.8, where the checker was incubating. The affected code dates from
GROOVY-11261.
### Backport
Suggested target: `GROOVY_6_0_X`. The production change applies there
without changes. If a 5.0.x backport is wanted, `GROOVY_5_0_X` has the same
three dereferences in slightly different code, so the edits would need to be
made by hand.
### Not in this change
`FormatStringChecker` still fails with `BUG! ... ClassNode#getTypeClass for
Foo called before the type class is set` when a non-constant format argument
has a type declared in the same compilation. For example, `class Foo {}` with
`String.format('%s', new Foo())`, or a constant of an enum declared in the same
source passed as an argument. That failure has a separate cause
(`checkFormatStringTypes` compares runtime classes), occurs on 6.0.0 and master
alike, and is left for its own issue.
---
> 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
> Priority: Major
>
> 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)