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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new d79494e669 GROOVY-11297: Fail to identify duplicate constructor 
declaration
d79494e669 is described below

commit d79494e669bdc914f8a176528b74e8683b07831a
Author: Paul King <[email protected]>
AuthorDate: Thu Jan 25 16:55:18 2024 +1000

    GROOVY-11297: Fail to identify duplicate constructor declaration
---
 src/main/java/org/codehaus/groovy/classgen/Verifier.java | 13 +++++++++++++
 src/test/gls/classes/methods/RepetitiveMethodTest.groovy | 10 ++++++++++
 2 files changed, 23 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java 
b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 986f29744c..6c496f7d62 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -268,6 +268,7 @@ public class Verifier implements GroovyClassVisitor, 
Opcodes {
         node.getObjectInitializerStatements().clear();
         node.visitContents(this);
         checkForDuplicateMethods(node);
+        checkForDuplicateConstructors(node);
         addCovariantMethods(node);
         detectNonSealedClasses(node);
         checkFinalVariables(node);
@@ -393,6 +394,18 @@ public class Verifier implements GroovyClassVisitor, 
Opcodes {
         }
     }
 
+    private static void checkForDuplicateConstructors(final ClassNode cn) {
+        Set<String> descriptors = new HashSet<>();
+        for (ConstructorNode cons : cn.getDeclaredConstructors()) {
+            String mySig = methodDescriptorWithoutReturnType(cons);
+            if (descriptors.contains(mySig)) {
+                throw new RuntimeParserException("The constructor " + 
cons.getText() +
+                    " duplicates another constructor of the same signature", 
sourceOf(cons));
+            }
+            descriptors.add(mySig);
+        }
+    }
+
     private static String scriptBodySignatureWithoutReturnType(final ClassNode 
cn) {
         for (MethodNode mn : cn.getMethods()) {
             if (mn.isScriptBody()) return 
methodDescriptorWithoutReturnType(mn);
diff --git a/src/test/gls/classes/methods/RepetitiveMethodTest.groovy 
b/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
index b724e2d90e..e9824f23b3 100644
--- a/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
+++ b/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
@@ -48,4 +48,14 @@ class RepetitiveMethodTest extends CompilableTestSupport {
             }
         '''
     }
+
+    void testRepetitiveConstructor() {
+        def message = shouldNotCompile('''
+            class A {
+                A(int x) {}
+                A(int y) {}
+            }
+        ''')
+        assert message.contains('duplicates another constructor of the same 
signature')
+    }
 }

Reply via email to