groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8260: Static compilation requires casting inside instanceof check (closes #603)
Date Sun, 24 Sep 2017 09:45:43 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 12f3f818b -> 65fcb6a7c


GROOVY-8260: Static compilation requires casting inside instanceof check (closes #603)


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

Branch: refs/heads/master
Commit: 65fcb6a7c8b2444ec6d7e848dea94f13484b5ab4
Parents: 12f3f81
Author: paulk <paulk@asert.com.au>
Authored: Mon Sep 18 13:37:59 2017 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Sun Sep 24 19:45:06 2017 +1000

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingVisitor.java          | 10 +++-
 src/test/groovy/bugs/Groovy8260Bug.groovy       | 57 ++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/65fcb6a7/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 186efdd..cbb2798 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4021,12 +4021,18 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
             }
             if (variable instanceof Parameter) {
                 Parameter parameter = (Parameter) variable;
-                ClassNode type = typeCheckingContext.controlStructureVariables.get(parameter);
+                ClassNode type = null;
+                // check if param part of control structure - but not if inside instanceof
+                List<ClassNode> temporaryTypesForExpression = getTemporaryTypesForExpression(vexp);
+                if (temporaryTypesForExpression == null || temporaryTypesForExpression.isEmpty())
{
+                    type = typeCheckingContext.controlStructureVariables.get(parameter);
+                }
+                // now check for closure override
                 TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
                 ClassNode[] closureParamTypes = (ClassNode[]) (enclosingClosure != null ?
enclosingClosure.getClosureExpression().getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS)
: null);
                 if (type == null && enclosingClosure != null && "it".equals(variable.getName())
&& closureParamTypes != null) {
                     final Parameter[] parameters = enclosingClosure.getClosureExpression().getParameters();
-                    if (parameters.length == 0 && getTemporaryTypesForExpression(vexp)
== null && closureParamTypes.length != 0) {
+                    if (parameters.length == 0 && temporaryTypesForExpression ==
null && closureParamTypes.length != 0) {
                         type = closureParamTypes[0];
                     }
                 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/65fcb6a7/src/test/groovy/bugs/Groovy8260Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8260Bug.groovy b/src/test/groovy/bugs/Groovy8260Bug.groovy
new file mode 100644
index 0000000..e69d68b
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8260Bug.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
+
+class Groovy8260Bug extends GroovyTestCase {
+
+    void testNoCastForInstanceofInsideLoop() {
+        assertScript '''
+            import groovy.transform.CompileStatic
+
+            interface FooI {
+                def intfMethod()
+            }
+
+            class Foo implements FooI {
+                def intfMethod() { 'Foo Interface method' }
+                def implMethod() { 'Foo Implementation method' }
+            }
+
+            @CompileStatic
+            def method(FooI propIn, List result) {
+                if (propIn instanceof Foo) {
+                    result << propIn.implMethod()
+                } else {
+                    result << propIn?.intfMethod()
+                }
+                for (FooI propLoop : [null, new Foo()]) {
+                    result << propLoop?.intfMethod()
+                    if (propLoop instanceof Foo) {
+                        result << propLoop.implMethod()
+                    }
+                }
+            }
+
+            def result = []
+            method(null, result)
+            assert result == [null, null, 'Foo Interface method', 'Foo Implementation method']
+        '''
+    }
+
+}


Mime
View raw message