groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Refine safe index for static compilation
Date Fri, 11 Nov 2016 17:31:19 GMT
Repository: groovy
Updated Branches:
  refs/heads/parrot 532a0a24b -> ca21c32c9


Refine safe index for static compilation


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ca21c32c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ca21c32c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ca21c32c

Branch: refs/heads/parrot
Commit: ca21c32c9609c425397d41a2560ac67648ce9412
Parents: 532a0a2
Author: Daniel Sun <sunlan@apache.org>
Authored: Sat Nov 12 01:30:43 2016 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Sat Nov 12 01:30:43 2016 +0800

----------------------------------------------------------------------
 .../asm/sc/StaticTypesCallSiteWriter.java       | 13 +++++++++---
 .../parser/antlr4/GroovyParserTest.groovy       |  1 +
 .../test/resources/core/SafeIndex_02x.groovy    | 21 ++++++++++++++++++++
 3 files changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ca21c32c/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java b/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
index 3a13c6b..a221c66 100644
--- a/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
+++ b/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
@@ -16,6 +16,7 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
+
 package org.codehaus.groovy.classgen.asm.sc;
 
 import org.codehaus.groovy.GroovyBugError;
@@ -626,12 +627,12 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements
Opcodes
         ClassNode classNode = controller.getClassNode();
         ClassNode rType = typeChooser.resolveType(receiver, classNode);
         ClassNode aType = typeChooser.resolveType(arguments, classNode);
-        if (trySubscript(receiver, message, arguments, rType, aType)) {
+        if (trySubscript(receiver, message, arguments, rType, aType, safe)) {
             return;
         }
         // new try with flow type instead of declaration type
         rType = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
-        if (rType!=null && trySubscript(receiver, message, arguments, rType, aType))
{
+        if (rType!=null && trySubscript(receiver, message, arguments, rType, aType,
safe)) {
             return;
         }
         // todo: more cases
@@ -642,7 +643,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements
Opcodes
                 "a bug report at https://issues.apache.org/jira/browse/GROOVY");
     }
 
-    private boolean trySubscript(final Expression receiver, final String message, final Expression
arguments, ClassNode rType, final ClassNode aType) {
+    private boolean trySubscript(final Expression receiver, final String message, final Expression
arguments, ClassNode rType, final ClassNode aType, boolean safe) {
         if (getWrapper(rType).isDerivedFrom(Number_TYPE)
                 && getWrapper(aType).isDerivedFrom(Number_TYPE)) {
             if ("plus".equals(message) || "minus".equals(message) || "multiply".equals(message)
|| "div".equals(message)) {
@@ -690,6 +691,8 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements
Opcodes
                             "getAt",
                             arguments
                     );
+
+                    call.setSafe(safe);
                     call.setSourcePosition(arguments);
                     call.setImplicitThis(false);
                     call.setMethodTarget(getAtNode);
@@ -716,6 +719,8 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements
Opcodes
                             message,
                             arguments
                     );
+
+                    call.setSafe(safe);
                     call.setSourcePosition(arguments);
                     call.setImplicitThis(false);
                     call.setMethodTarget(methodNode);
@@ -729,6 +734,8 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements
Opcodes
                             "get",
                             arguments
                     );
+
+                    call.setSafe(safe);
                     call.setMethodTarget(MAP_GET_METHOD);
                     call.setSourcePosition(arguments);
                     call.setImplicitThis(false);

http://git-wip-us.apache.org/repos/asf/groovy/blob/ca21c32c/subprojects/groovy-antlr4-grammar/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-antlr4-grammar/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
b/subprojects/groovy-antlr4-grammar/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 27ab7e0..01f0767 100644
--- a/subprojects/groovy-antlr4-grammar/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/groovy-antlr4-grammar/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -222,6 +222,7 @@ class GroovyParserTest extends GroovyTestCase {
 
     void "test groovy core - SafeIndex"() {
         doRunAndTest('core/SafeIndex_01x.groovy');
+        doRunAndTest('core/SafeIndex_02x.groovy');
     }
 
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/ca21c32c/subprojects/groovy-antlr4-grammar/src/test/resources/core/SafeIndex_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-antlr4-grammar/src/test/resources/core/SafeIndex_02x.groovy
b/subprojects/groovy-antlr4-grammar/src/test/resources/core/SafeIndex_02x.groovy
new file mode 100644
index 0000000..b753cf0
--- /dev/null
+++ b/subprojects/groovy-antlr4-grammar/src/test/resources/core/SafeIndex_02x.groovy
@@ -0,0 +1,21 @@
+import groovy.transform.*
+
+class SomeContainer {
+    public Object getAt(int i) {
+        return "123";
+    }
+}
+
+@CompileStatic
+def cs() {
+    List list = null;
+    list?[1];
+
+    Map map = null;
+    map?[1];
+
+    SomeContainer sc = null;
+    sc?[1];
+}
+
+cs()


Mime
View raw message