codeconsole opened a new pull request, #3218:
URL: https://github.com/apache/groovy/pull/3218
RegexChecker's afterVisitMethod visitor dereferenced the result of
findConstExp(..., Integer) without a null check in two places: the
Matcher.group(...) check and the matcher[...][...] subscript check. Any
argument that is not an Integer constant, which is every group(String) call on
a named group as well as group(int) or a subscript with a variable index, made
the extension throw a NullPointerException that the compiler surfaced as
"General error during canonicalization: Cannot get property 'value' on null
object". Code that is correct at runtime could not be compiled with the
extension enabled.
This change makes both checks skip arguments that are not constants, so a
variable index or name is left to the runtime as before the checker was
involved, and adds the obvious check for a constant group name: when the regex
is known, the checker records its named-capturing group names next to the group
count it already records, and group(String) with a name the regex does not
define is reported as a static type error ("Invalid group name 'third' for
regex with named groups first, second", or "... for regex with no named
groups"), mirroring the existing "Invalid group count" diagnostic. The names
are propagated along the same paths as the group count (declaration,
Pattern.matcher() on a tracked pattern), through one small helper that replaces
four copies of the same copy-if-present code.
Group names are collected by scanning the (already compiled) regex text for
named-group openers, tolerating whitespace inside the angle brackets as the JDK
does in COMMENTS mode. The scan can only over-approximate (openers in escaped
or quoted contexts are picked up too), so it can never report a name the regex
really defines; Pattern.namedGroups() was not used because it needs JDK 20 and
the build targets 17.
Testing: three regression tests in RegexCheckerTest cover the =~,
Pattern-variable and Pattern.compile(...).matcher(...) forms with existing,
missing and non-constant group names and indices, plus the original reproducer.
They fail on master before the change (two with the compiler failure, one
because no diagnostic is produced) and pass with it; the rest of the class and
:groovy-typecheckers:test, lintGate and rat pass. The ten standalone scripts
used to reproduce the problem were also rerun against a distribution built from
this branch: the eight that failed now either compile and print the expected
value or produce the new diagnostic.
Suggested backport: GROOVY_6_0_X (6.0.0 is affected).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]