This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 7364b3771756e30e67a141cb7d186542465f61b3
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Mar 29 14:48:17 2025 +0900
GROOVY-10240: Instance field is not allowed in `record`
(cherry picked from commit 48328e50d9236407bc22f5ec85902f3523a7deea)
---
.../apache/groovy/parser/antlr4/AstBuilder.java | 4 +++-
.../fail/RecordDeclaration_15x.groovy | 26 ++++++++++++++++++++++
.../groovy/parser/antlr4/SyntaxErrorTest.groovy | 1 +
3 files changed, 30 insertions(+), 1 deletion(-)
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 4380f7c02b..924fb647be 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1362,7 +1362,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
this.visitClassBody(ctx.classBody());
if (isRecord) {
classNode.getFields().stream().filter(f -> !isTrue(f, IS_RECORD_GENERATED) && !f.isStatic()).findFirst()
- .ifPresent(fn -> this.createParsingFailedException("Instance field is not allowed in `record`", fn));
+ .ifPresent(fn -> {
+ throw this.createParsingFailedException("Instance field is not allowed in `record`", fn);
+ });
}
classNodeStack.pop();
diff --git a/src/test-resources/fail/RecordDeclaration_15x.groovy b/src/test-resources/fail/RecordDeclaration_15x.groovy
new file mode 100644
index 0000000000..d7d02de3e3
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_15x.groovy
@@ -0,0 +1,26 @@
+/*
+ * 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 variables (non-static fields) 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 c8f30f1429..a0c2d2fedd 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -472,6 +472,7 @@ final class SyntaxErrorTest {
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_12x.groovy')
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_13x.groovy')
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_14x.groovy')
+ TestUtils.doRunAndShouldFail('fail/RecordDeclaration_15x.groovy')
}
@Test