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 25171eb839 GROOVY-10240: Cannot assign a value to final variable
25171eb839 is described below
commit 25171eb839ad37fc4b1d8a6c3006f02ce35fd354
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Mar 29 16:51:58 2025 +0900
GROOVY-10240: Cannot assign a value to final variable
---
.../apache/groovy/parser/antlr4/AstBuilder.java | 2 +-
.../fail/RecordDeclaration_18x.groovy | 26 ++++++++++++++++++++++
.../groovy/parser/antlr4/SyntaxErrorTest.groovy | 1 +
3 files changed, 28 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 24a40bc8a7..46cb515314 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1681,7 +1681,7 @@ public class AstBuilder extends
GroovyParserBaseVisitor<Object> {
String receiverText =
expression.getObjectExpression().getText();
String propertyName = expression.getPropertyAsString();
if (THIS_STR.equals(receiverText) &&
Arrays.stream(header).anyMatch(p -> p.getName().equals(propertyName))) {
- createParsingFailedException("Cannot assign a value to
final variable '" + propertyName + "'", expression.getProperty());
+ throw createParsingFailedException("Cannot assign a value
to final variable '" + propertyName + "'", expression.getProperty());
}
super.visitPropertyExpression(expression);
}
diff --git a/src/test-resources/fail/RecordDeclaration_18x.groovy
b/src/test-resources/fail/RecordDeclaration_18x.groovy
new file mode 100644
index 0000000000..860f0effc8
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_18x.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 fail
+
+record Person(String name, int age) {
+ Person {
+ // Cannot assign a value to final variable
+ this.age = 40
+ }
+}
diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 52654b8b38..da50aeac44 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -498,6 +498,7 @@ final class SyntaxErrorTest {
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_15x.groovy')
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_16x.groovy')
TestUtils.doRunAndShouldFail('fail/RecordDeclaration_17x.groovy')
+ TestUtils.doRunAndShouldFail('fail/RecordDeclaration_18x.groovy')
}
@Test