You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/09/21 08:22:35 UTC

[groovy] 01/01: Prohibits instance fields declared in `record`

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

sunlan pushed a commit to branch GROOVY-10240
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit abd3f8e8a8938d9001892e09e6b23e09b3c793f6
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Sep 21 16:22:16 2021 +0800

    Prohibits instance fields declared in `record`
---
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 15 +++++++++--
 .../core/RecordDeclaration_07x.groovy              | 30 ++++++++++++++++++++++
 .../fail/RecordDeclaration_06x.groovy              | 23 +++++++++++++++++
 .../fail/RecordDeclaration_07x.groovy              | 23 +++++++++++++++++
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  1 +
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy    |  2 ++
 6 files changed, 92 insertions(+), 2 deletions(-)

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 7da7598..52c8498 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1617,6 +1617,14 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         classNodeStack.push(classNode);
         ctx.classBody().putNodeMetaData(CLASS_DECLARATION_CLASS_NODE, classNode);
         this.visitClassBody(ctx.classBody());
+        if (isRecord) {
+            Optional<FieldNode> fieldNodeOptional =
+                    classNode.getFields().stream()
+                            .filter(f -> !isTrue(f, IS_RECORD_GENERATED) && !f.isStatic()).findFirst();
+            if (fieldNodeOptional.isPresent()) {
+                createParsingFailedException("Instance field is not allowed in `record`", fieldNodeOptional.get());
+            }
+        }
         classNodeStack.pop();
 
         if (!(asBoolean(ctx.CLASS()) || asBoolean(ctx.TRAIT()))) {
@@ -1642,7 +1650,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
             Parameter parameter = parameters[i];
             ModifierManager parameterModifierManager = parameter.getNodeMetaData(PARAMETER_MODIFIER_MANAGER);
             FormalParameterContext parameterCtx = parameter.getNodeMetaData(PARAMETER_CONTEXT);
-            declareProperty(parameterCtx, parameterModifierManager, parameter.getType(), classNode, i, parameter, parameter.getName(), parameter.getModifiers(), parameter.getInitialExpression());
+            PropertyNode propertyNode = declareProperty(parameterCtx, parameterModifierManager, parameter.getType(), classNode, i, parameter, parameter.getName(), parameter.getModifiers(), parameter.getInitialExpression());
+            propertyNode.getField().putNodeMetaData(IS_RECORD_GENERATED, true);
         }
     }
 
@@ -2268,7 +2277,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         }
     }
 
-    private void declareProperty(final GroovyParserRuleContext ctx, final ModifierManager modifierManager, final ClassNode variableType, final ClassNode classNode, final int i, final ASTNode startNode, final String fieldName, final int modifiers, final Expression initialValue) {
+    private PropertyNode declareProperty(final GroovyParserRuleContext ctx, final ModifierManager modifierManager, final ClassNode variableType, final ClassNode classNode, final int i, final ASTNode startNode, final String fieldName, final int modifiers, final Expression initialValue) {
         PropertyNode propertyNode;
         FieldNode fieldNode = classNode.getDeclaredField(fieldName);
 
@@ -2315,6 +2324,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         } else {
             configureAST(propertyNode, startNode, initialValue);
         }
+        return propertyNode;
     }
 
     private void declareField(final VariableDeclarationContext ctx, final ModifierManager modifierManager, final ClassNode variableType, final ClassNode classNode, final int i, final VariableExpression variableExpression, final String fieldName, final int modifiers, final Expression initialValue) {
@@ -5152,6 +5162,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
     private static final String IS_YIELD_STATEMENT = "_IS_YIELD_STATEMENT";
     private static final String PARAMETER_MODIFIER_MANAGER = "_PARAMETER_MODIFIER_MANAGER";
     private static final String PARAMETER_CONTEXT = "_PARAMETER_CONTEXT";
+    private static final String IS_RECORD_GENERATED = "_IS_RECORD_GENERATED";
     private static final String RECORD_TYPE_NAME = "groovy.transform.RecordType";
     private static final String RECORD_CLASS_NAME = "java.lang.Record";
 }
diff --git a/src/test-resources/core/RecordDeclaration_07x.groovy b/src/test-resources/core/RecordDeclaration_07x.groovy
new file mode 100644
index 0000000..c1ed9cc
--- /dev/null
+++ b/src/test-resources/core/RecordDeclaration_07x.groovy
@@ -0,0 +1,30 @@
+/*
+ *  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
+
+record Fruit(String name, double price) {
+    public static final String CONST_1 = 'C1'
+
+    String eat() {
+        return "$name, $price, $CONST_1"
+    }
+}
+
+assert 'C1' == Fruit.CONST_1
+assert 'apple, 11.6, C1' == new Fruit('apple', 11.6).eat()
diff --git a/src/test-resources/fail/RecordDeclaration_06x.groovy b/src/test-resources/fail/RecordDeclaration_06x.groovy
new file mode 100644
index 0000000..80de1ad
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_06x.groovy
@@ -0,0 +1,23 @@
+/*
+ *  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
+
+record Fruit(String name) {
+    private String price
+}
diff --git a/src/test-resources/fail/RecordDeclaration_07x.groovy b/src/test-resources/fail/RecordDeclaration_07x.groovy
new file mode 100644
index 0000000..cbb8062
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_07x.groovy
@@ -0,0 +1,23 @@
+/*
+ *  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
+
+record Fruit(String name) {
+    String price
+}
diff --git a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 5b989d1..5b4881a 100644
--- a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -364,6 +364,7 @@ final class GroovyParserTest extends GroovyTestCase {
         doRunAndTestAntlr4('core/RecordDeclaration_04x.groovy')
         doRunAndTestAntlr4('core/RecordDeclaration_05x.groovy')
         doRunAndTestAntlr4('core/RecordDeclaration_06x.groovy')
+        doRunAndTestAntlr4('core/RecordDeclaration_07x.groovy')
     }
 
     void "test groovy core - AnnotationDeclaration"() {
diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index c460ca2..f720202 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -433,6 +433,8 @@ final class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_03x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_04x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_05x.groovy')
+        TestUtils.doRunAndShouldFail('fail/RecordDeclaration_06x.groovy')
+        TestUtils.doRunAndShouldFail('fail/RecordDeclaration_07x.groovy')
     }
 
     void 'test groovy core - Array'() {