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 2020/06/30 04:43:54 UTC

[groovy] branch GROOVY_3_0_X updated (f8280e0 -> 87da0a5)

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

sunlan pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from f8280e0  Tweak DFA cache threshold to support almost no limit(when threshold is set less than 0)
     new b949074  GROOVY-9608: add test case
     new 87da0a5  Tweak `methodDeclaration` rule

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/antlr/GroovyParser.g4                          | 12 ++---
 .../{Groovy3519Bug.groovy => Groovy9608.groovy}    | 55 ++++++++++------------
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 10 +++-
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy    | 17 +++++++
 .../resources/core/AnnotationDeclaration_01.groovy |  4 ++
 5 files changed, 60 insertions(+), 38 deletions(-)
 copy src/test/groovy/bugs/{Groovy3519Bug.groovy => Groovy9608.groovy} (55%)


[groovy] 01/02: GROOVY-9608: add test case

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b9490749125d91926f6a3b874a5fe506e8cea751
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jun 28 12:34:58 2020 -0500

    GROOVY-9608: add test case
    
    (cherry picked from commit 0b886e910bbb0e75aae527fae2cab0ec4f096bf2)
---
 src/test/groovy/bugs/Groovy9608.groovy | 57 ++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/src/test/groovy/bugs/Groovy9608.groovy b/src/test/groovy/bugs/Groovy9608.groovy
new file mode 100644
index 0000000..1cf5209
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9608.groovy
@@ -0,0 +1,57 @@
+/*
+ *  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 groovy.bugs
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy9608 {
+
+    @Test
+    void testGetFieldOnSuper() {
+        assertScript '''
+            import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
+
+            class A {
+              public x = 'A'
+            }
+            class B extends A {
+              public x = 'B'
+            }
+
+            def a = new A()
+            def ax = a.metaClass.getAttribute(A, a, 'x', false)
+            assert ax == 'A'
+
+            def b = new B()
+            def bx = b.metaClass.getAttribute(B, b, 'x', false)
+            assert bx == 'B'
+
+            try {
+                def bSuperX = b.metaClass.getAttribute(A, b, 'x', true)
+                assert bSuperX == 'A'
+                bSuperX = ScriptBytecodeAdapter.getFieldOnSuper(A, b, 'x')
+                assert bSuperX == 'A'
+            } catch (MissingFieldException e) {
+                // TODO: Why can't these read public/protected field from super?
+            }
+        '''
+    }
+}


[groovy] 02/02: Tweak `methodDeclaration` rule

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 87da0a5598156965e53bbad80aa28a5534d56c3f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Jun 30 11:30:52 2020 +0800

    Tweak `methodDeclaration` rule
    
    (cherry picked from commit 076bce0eb516c2fe7524b223bd4697232536dcca)
---
 src/antlr/GroovyParser.g4                               | 12 ++++++------
 .../org/apache/groovy/parser/antlr4/AstBuilder.java     | 10 +++++++++-
 .../apache/groovy/parser/antlr4/SyntaxErrorTest.groovy  | 17 +++++++++++++++++
 .../test/resources/core/AnnotationDeclaration_01.groovy |  4 ++++
 4 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 52d7e4a..9feca64 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -267,14 +267,14 @@ memberDeclaration[int t]
  *  ct  9: script, other see the comment of classDeclaration
  */
 methodDeclaration[int t, int ct]
-    :   modifiersOpt
-        (   { 3 == $ct }?
-            returnType[$ct] methodName LPAREN rparen (DEFAULT nls elementValue)?
+    :   modifiersOpt typeParameters? returnType[$ct]?
+        methodName formalParameters
+        (
+            DEFAULT nls elementValue
         |
-            typeParameters? returnType[$ct]?
-            methodName formalParameters (nls THROWS nls qualifiedClassNameList)?
+            (nls THROWS nls qualifiedClassNameList)?
             (nls methodBody)?
-        )
+        )?
     ;
 
 methodName
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index e3f2f29..6b69578 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1582,7 +1582,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         }
 
         boolean isAbstractMethod = methodNode.isAbstract();
-        boolean hasMethodBody = asBoolean(methodNode.getCode());
+        boolean hasMethodBody =
+                asBoolean(methodNode.getCode())
+                        && !(methodNode.getCode() instanceof ExpressionStatement);
 
         if (9 == ctx.ct) { // script
             if (isAbstractMethod || !hasMethodBody) { // method should not be declared abstract in the script
@@ -1595,6 +1597,12 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
                 }
             }
 
+            if (3 == ctx.ct) { // annotation
+                if (hasMethodBody) {
+                    throw createParsingFailedException("Annotation type element should not have body", ctx);
+                }
+            }
+
             if (!isAbstractMethod && !hasMethodBody) { // non-abstract method without body in the non-script(e.g. class, enum, trait) is not allowed!
                 throw createParsingFailedException("You defined a method[" + methodNode.getName() + "] without a body. Try adding a method body, or declare it abstract", methodNode);
             }
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 54d87f2..4bbe12e 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -272,6 +272,23 @@ final class SyntaxErrorTest extends GroovyTestCase {
             |'''.stripMargin()
     }
 
+    void 'test groovy core - AnnotationDeclaration 5'() {
+        def err = expectParseError '''\
+            |@interface A {
+            |    String a() {
+            |    }
+            |}'''.stripMargin()
+        println err
+        assert err == '''\
+            |startup failed:
+            |test.groovy: 2: Annotation type element should not have body @ line 2, column 5.
+            |       String a() {
+            |       ^
+            |
+            |1 error
+            |'''.stripMargin()
+    }
+
     void 'test groovy core - MethodDeclaration'() {
         TestUtils.shouldFail('fail/MethodDeclaration_01.groovy')
         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy')
diff --git a/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy b/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
index 88f3a3d..8a08591 100644
--- a/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
+++ b/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
@@ -54,4 +54,8 @@ import java.lang.annotation.Target
 
 @interface b {
     String name()
+}
+
+@interface C {
+    String name() default ''
 }
\ No newline at end of file