You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/06/10 13:39:00 UTC

(groovy) branch master updated: GROOVY-12080: RegexChecker crashes on inline pattern (~/.../) as .matcher() receiver

This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new d5899d5eaa GROOVY-12080: RegexChecker crashes on inline pattern (~/.../) as .matcher() receiver
d5899d5eaa is described below

commit d5899d5eaaf5058814b4fcae148321ee7c4f8922
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Jun 10 22:46:16 2026 +1000

    GROOVY-12080: RegexChecker crashes on inline pattern (~/.../) as .matcher() receiver
---
 .../groovy/groovy/typecheckers/RegexChecker.groovy |  8 ++++----
 .../groovy/typecheckers/RegexCheckerTest.groovy    | 23 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/subprojects/groovy-typecheckers/src/main/groovy/groovy/typecheckers/RegexChecker.groovy b/subprojects/groovy-typecheckers/src/main/groovy/groovy/typecheckers/RegexChecker.groovy
index 69b8896dce..f105b6af0c 100644
--- a/subprojects/groovy-typecheckers/src/main/groovy/groovy/typecheckers/RegexChecker.groovy
+++ b/subprojects/groovy-typecheckers/src/main/groovy/groovy/typecheckers/RegexChecker.groovy
@@ -149,8 +149,8 @@ class RegexChecker extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
                     if (call.objectExpression instanceof ClassExpression) {
                         checkPatternMethod(call, call.objectExpression.type)
                     } else if (isPattern(call.receiver) && call.methodAsString == 'matcher') {
-                        def var = findTargetVariable(call.receiver)
-                        def groupCount = var?.getNodeMetaData(REGEX_GROUP_COUNT)
+                        def source = call.receiver instanceof VariableExpression ? findTargetVariable(call.receiver) : call.receiver
+                        def groupCount = source?.getNodeMetaData(REGEX_GROUP_COUNT)
                         if (groupCount != null) {
                             call.putNodeMetaData(REGEX_GROUP_COUNT, groupCount)
                         }
@@ -233,8 +233,8 @@ class RegexChecker extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
                 @Override
                 void visitMethodCallExpression(MethodCallExpression call) {
                     if (isPattern(call.receiver) && call.methodAsString == 'matcher') {
-                        def var = findTargetVariable(call.receiver)
-                        def groupCount = var?.getNodeMetaData(REGEX_GROUP_COUNT)
+                        def source = call.receiver instanceof VariableExpression ? findTargetVariable(call.receiver) : call.receiver
+                        def groupCount = source?.getNodeMetaData(REGEX_GROUP_COUNT)
                         if (groupCount != null) {
                             call.putNodeMetaData(REGEX_GROUP_COUNT, groupCount)
                         }
diff --git a/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy b/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
index cb1db912d5..a28a8dda07 100644
--- a/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
+++ b/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
@@ -271,6 +271,29 @@ final class RegexCheckerTest {
         '''
     }
 
+    @Test
+    void testInlinePatternAsMatcherReceiver() {
+        // GROOVY-12080: pattern operator result used directly as a matcher() receiver
+        // used to crash the compiler ("No signature of method: findTargetVariable")
+        assertScript shell, '''
+            Matcher m = (~/(...)(...)/).matcher('foobar')
+            assert m.find()
+            assert m.group(1) == 'foo'
+            assert m.group(2) == 'bar'
+        '''
+    }
+
+    @Test
+    void testBadRegexGroupCountForInlinePattern() {
+        // GROOVY-12080: group count is now inferred for inline patterns too
+        def err = shouldFail shell, '''
+            Matcher m = (~/(...)(...)/).matcher('foobar')
+            assert m.find()
+            assert m.group(3)
+        '''
+        assert err.message =~ /Invalid group count 3 for regex with 2 groups/
+    }
+
     @Test
     void testGoodRegexGroupCount() {
         assertScript shell, '''