You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/03/21 07:59:04 UTC

[groovy] 02/02: GROOVY-7968: Fix class field and property with same name and both having init to throw compilation failure (closes #1500)

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

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

commit ee359f9362792ac0564b24332e67d234f00810c1
Author: Patrik Torn <pa...@vincit.fi>
AuthorDate: Wed Feb 24 13:44:45 2021 +0200

    GROOVY-7968: Fix class field and property with same name and both having init to throw compilation failure (closes #1500)
---
 src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java | 7 ++++++-
 1 file changed, 6 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 e4051c1..244b420 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1675,8 +1675,10 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         FieldNode fieldNode = classNode.getDeclaredField(fieldName);
 
         if (fieldNode != null && !classNode.hasProperty(fieldName)) {
+            if (fieldNode.hasInitialExpression() && initialValue != null) {
+                throw createParsingFailedException("A field and a property have the same name '" + fieldName + "' and both have initial values", ctx);
+            }
             classNode.getFields().remove(fieldNode);
-
             propertyNode = new PropertyNode(fieldNode, modifiers | Opcodes.ACC_PUBLIC, null, null);
             classNode.addProperty(propertyNode);
         } else {
@@ -1718,6 +1720,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         PropertyNode propertyNode = classNode.getProperty(fieldName);
 
         if (null != propertyNode && propertyNode.getField().isSynthetic()) {
+            if (propertyNode.hasInitialExpression() && initialValue != null) {
+                throw createParsingFailedException("A field and a property have the same name '" + fieldName + "' and both have initial values", ctx);
+            }
             classNode.getFields().remove(propertyNode.getField());
             fieldNode = new FieldNode(fieldName, modifiers, variableType, classNode.redirect(), initialValue);
             propertyNode.setField(fieldNode);