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

sunlan 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 32272b6e8a GROOVY-10240: Instance initializer is not allowed in 
`record`
32272b6e8a is described below

commit 32272b6e8a2b3e7797f081c2e106b93a198ac861
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Mar 29 15:31:08 2025 +0900

    GROOVY-10240: Instance initializer is not allowed in `record`
---
 .../apache/groovy/parser/antlr4/AstBuilder.java    |  5 ++++
 .../fail/RecordDeclaration_16x.groovy              | 28 ++++++++++++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy    |  1 +
 3 files changed, 34 insertions(+)

diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java 
b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index f9cfaebb01..01d848479e 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1327,6 +1327,11 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> {
                     .ifPresent(fn -> {
                         throw this.createParsingFailedException("Instance 
field is not allowed in `record`", fn);
                     });
+
+            final List<Statement> objectInitializerStatements = 
classNode.getObjectInitializerStatements();
+            if (asBoolean(objectInitializerStatements)) {
+                throw this.createParsingFailedException("Instance initializer 
is not allowed in `record`", objectInitializerStatements.get(0));
+            }
         }
         this.classNodeStack.pop();
 
diff --git a/src/test-resources/fail/RecordDeclaration_16x.groovy 
b/src/test-resources/fail/RecordDeclaration_16x.groovy
new file mode 100644
index 0000000000..e6479144c5
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_16x.groovy
@@ -0,0 +1,28 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package core
+
+import java.util.function.BiFunction
+
+// cannot declare instance initializers in a record class.
+record Rectangle(double length, double width) {
+    {
+        BiFunction<Double, Double, Double> diagonal = (x, y) -> Math.sqrt(x*x 
+ y*y)
+    }
+}
diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy 
b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 73bd19fa48..0f0c7e7565 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -491,6 +491,7 @@ final class SyntaxErrorTest {
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_13x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_14x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_15x.groovy')
+        TestUtils.doRunAndShouldFail('fail/RecordDeclaration_16x.groovy')
     }
 
     @Test

Reply via email to