groovy git commit: GROOVY-8549: Compile Static causes getAt to fail (closes #798)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: GROOVY-8549: Compile Static causes getAt to fail (closes #798)

paulk
Repository: groovy
Updated Branches:
  refs/heads/master 74c5aa66a -> d0707a2db


GROOVY-8549: Compile Static causes getAt to fail (closes #798)


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

Branch: refs/heads/master
Commit: d0707a2db325f3e7a21372e556bad5edb805bcb2
Parents: 74c5aa6
Author: Paul King <[hidden email]>
Authored: Sun Sep 16 21:26:51 2018 +1000
Committer: Paul King <[hidden email]>
Committed: Thu Oct 11 13:16:44 2018 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    | 14 +++++++
 src/test/groovy/bugs/Groovy8549Bug.groovy       | 40 ++++++++++++++++++++
 2 files changed, 54 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/d0707a2d/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 32ea174..f2a6b26 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -7818,6 +7818,13 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Support subscript operator for list access.
+     */
+    public static <T> T getAt(List<T> self, Number idx) {
+        return getAt(self, idx.intValue());
+    }
+
+    /**
      * Support the subscript operator for an Iterator. The iterator
      * will be partially exhausted up until the idx entry after returning
      * if a +ve or 0 idx is used, or fully exhausted if a -ve idx is used
@@ -7915,6 +7922,13 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Support subscript operator for list modification.
+     */
+    public static <T> void putAt(List<T> self, Number idx, T value) {
+        putAt(self, idx.intValue(), value);
+    }
+
+    /**
      * A helper method to allow lists to work with subscript operators.
      * <pre class="groovyTestCase">def list = ["a", true]
      * list[1..<1] = 5

http://git-wip-us.apache.org/repos/asf/groovy/blob/d0707a2d/src/test/groovy/bugs/Groovy8549Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8549Bug.groovy b/src/test/groovy/bugs/Groovy8549Bug.groovy
new file mode 100644
index 0000000..fdcae61
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8549Bug.groovy
@@ -0,0 +1,40 @@
+/*
+ *  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 gls.CompilableTestSupport
+
+class Groovy8549Bug extends CompilableTestSupport {
+    void testNumberWithVarSlotLoader() {
+        assertScript """
+            import groovy.transform.CompileStatic
+
+            @CompileStatic
+            def method() {
+                def list = [0, 1, 2, 3]
+                for (idx in 1..2) {
+                    list[idx-1]++
+                }
+                list
+            }
+
+            assert method() == [1, 2, 2, 3]
+        """
+    }
+}