groovy git commit: Revert "GROOVY-8355: Instanceof inference does not work on field assigning(closes #706)" for the time being

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

groovy git commit: Revert "GROOVY-8355: Instanceof inference does not work on field assigning(closes #706)" for the time being

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/master b1cc8a6ae -> 8c4547965


Revert "GROOVY-8355: Instanceof inference does not work on field assigning(closes #706)" for the time being

See https://github.com/apache/groovy/pull/706


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

Branch: refs/heads/master
Commit: 8c45479655a0795267ff983ea44f100ae79941e5
Parents: b1cc8a6
Author: sunlan <[hidden email]>
Authored: Wed May 16 07:46:45 2018 +0800
Committer: sunlan <[hidden email]>
Committed: Wed May 16 07:49:44 2018 +0800

----------------------------------------------------------------------
 .../asm/sc/StaticTypesCallSiteWriter.java       |  27 +----
 .../stc/StaticTypeCheckingVisitor.java          |  94 ----------------
 src/spec/test/typing/TypeCheckingTest.groovy    |   2 -
 src/test/groovy/bugs/Groovy8355Bug.groovy       | 112 -------------------
 4 files changed, 2 insertions(+), 233 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/8c454796/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
index 61e239c..b717c42 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
@@ -670,15 +670,8 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
         rType = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
         if (receiver instanceof VariableExpression && rType == null) {
             // TODO: can STCV be made smarter to avoid this check?
-            Variable accessedVariable = ((VariableExpression)receiver).getAccessedVariable();
-            VariableExpression ve;
-            if (accessedVariable instanceof FieldNode) {
-                FieldNode fieldNode = (FieldNode) accessedVariable;
-                rType = getInferredTypeOfField(fieldNode);
-            } else {
-                ve = (VariableExpression) accessedVariable;
-                rType = ve.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
-            }
+            VariableExpression ve = (VariableExpression) ((VariableExpression)receiver).getAccessedVariable();
+            rType = ve.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
         }
         if (rType!=null && trySubscript(receiver, message, arguments, rType, aType, safe)) {
             return;
@@ -691,22 +684,6 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
                 "this error and file a bug report at https://issues.apache.org/jira/browse/GROOVY");
     }
 
-    private ClassNode getInferredTypeOfField(FieldNode fieldNode) {
-        ClassNode rType = null;
-
-        Map nodeMetaData = fieldNode.getNodeMetaData(controller.getMethodNode());
-        if (null != nodeMetaData) {
-            rType = (ClassNode) nodeMetaData.get(StaticTypesMarker.INFERRED_TYPE);
-//            if (null == rType) {
-//                nodeMetaData = fieldNode.getNodeMetaData(controller.getClassNode());
-//                if (null != nodeMetaData) {
-//                    rType = (ClassNode) nodeMetaData.get(StaticTypesMarker.INFERRED_TYPE);
-//                }
-//            }
-        }
-        return rType;
-    }
-
     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)) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/8c454796/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 51d15f7..5f9e1cb 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -595,7 +595,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             }
         } else if (accessedVariable instanceof FieldNode) {
             FieldNode fieldNode = (FieldNode) accessedVariable;
-            FieldVariableExpression fieldVariableExpression = new FieldVariableExpression(fieldNode);
 
             ClassNode parameterizedType = GenericsUtils.findParameterizedType(fieldNode.getDeclaringClass(), typeCheckingContext.getEnclosingClassNode());
             if (null != parameterizedType) {
@@ -603,7 +602,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 ClassNode actualType = findActualTypeByPlaceholderName(originalType.getUnresolvedName(), GenericsUtils.extractPlaceholders(parameterizedType));
 
                 if (null != actualType) {
-                    fieldVariableExpression.storeInferredType(actualType);
                     storeType(vexp, actualType);
                     return;
                 }
@@ -847,13 +845,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
                 // if right expression is a ClosureExpression, store parameter type information
                 if (leftExpression instanceof VariableExpression) {
-                    VariableExpression variableExpression = (VariableExpression) leftExpression;
-
-                    final Variable accessedVariable = variableExpression.getAccessedVariable();
-                    if (accessedVariable instanceof FieldNode) {
-                        new FieldVariableExpression((FieldNode) accessedVariable).storeInferredType(resultType);
-                    }
-
                     if (rightExpression instanceof ClosureExpression) {
                         Parameter[] parameters = ((ClosureExpression) rightExpression).getParameters();
                         leftExpression.putNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS, parameters);
@@ -1760,8 +1751,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                 if (init instanceof ConstructorCallExpression) {
                     inferDiamondType((ConstructorCallExpression) init, node.getOriginType());
                 }
-
-//                new FieldVariableExpression(node).storeInferredType(getType(init));
             }
         } finally {
             currentField = null;
@@ -4539,15 +4528,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         if (cn != null) {
             return cn;
         }
-
-        if (exp instanceof FieldNode) {
-            ClassNode inferredType = new FieldVariableExpression((FieldNode) exp).getInferredType();
-
-            if (null != inferredType) {
-                return inferredType;
-            }
-        }
-
         if (exp instanceof ClassExpression) {
             ClassNode node = CLASS_Type.getPlainNodeReference();
             node.setGenericsTypes(new GenericsType[]{
@@ -5485,78 +5465,4 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             return parameter.equals(other);
         }
     }
-
-    private class FieldVariableExpression extends VariableExpression {
-        private final FieldNode fieldNode;
-
-        FieldVariableExpression(FieldNode fieldNode) {
-            super(fieldNode);
-            this.fieldNode = fieldNode;
-
-            ClassNode inferred = getInferredType();
-            if (inferred == null) {
-                storeInferredType(fieldNode.getOriginType());
-            }
-        }
-
-        @Override
-        public ListHashMap getMetaDataMap() {
-            AnnotatedNode node = getKey();
-
-            return getMetaDataMap(node);
-        }
-
-        private ListHashMap getMetaDataMap(AnnotatedNode key) {
-            if (null == key) {
-                return new ListHashMap(); // all valid cases should have a methodNode as key. return a empty map is just to avoid NPE, the map will be abandoned finally
-            }
-
-            ListHashMap metaDataMap = fieldNode.getNodeMetaData(key);
-
-            if (null == metaDataMap) {
-                metaDataMap = new ListHashMap();
-                fieldNode.putNodeMetaData(key, metaDataMap);
-            }
-
-            return metaDataMap;
-        }
-
-        @Override
-        public int hashCode() {
-            return fieldNode.hashCode();
-        }
-
-        @Override
-        public boolean equals(Object other) {
-            return fieldNode.equals(other);
-        }
-
-        @SuppressWarnings("unchecked")
-        private ClassNode getInferredType() {
-            Map<Object, Object> enclosingMetaData = getMetaDataMap();
-
-            ClassNode inferredType = (ClassNode) enclosingMetaData.get(StaticTypesMarker.INFERRED_TYPE);
-
-            return inferredType;
-//            return null == inferredType ? (ClassNode) getMetaDataMap(getDefaultKey()).get(StaticTypesMarker.INFERRED_TYPE) : inferredType;
-        }
-
-        @SuppressWarnings("unchecked")
-        private void storeInferredType(ClassNode inferredType) {
-            Map<Object, Object> enclosingMetaData = getMetaDataMap();
-
-            enclosingMetaData.put(StaticTypesMarker.INFERRED_TYPE, inferredType);
-        }
-
-        private AnnotatedNode getKey() {
-            AnnotatedNode node = typeCheckingContext.getEnclosingMethod();
-
-            return node;
-//            return null == node ? getDefaultKey() : node;
-        }
-
-//        private ClassNode getDefaultKey() {
-//            return typeCheckingContext.getEnclosingClassNode();
-//        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/8c454796/src/spec/test/typing/TypeCheckingTest.groovy
----------------------------------------------------------------------
diff --git a/src/spec/test/typing/TypeCheckingTest.groovy b/src/spec/test/typing/TypeCheckingTest.groovy
index 6574c37..fc87413 100644
--- a/src/spec/test/typing/TypeCheckingTest.groovy
+++ b/src/spec/test/typing/TypeCheckingTest.groovy
@@ -518,7 +518,6 @@ class TypeCheckingTest extends StaticTypeCheckingTestCase {
 
     }
 
-    /*
     void testTypeInferenceFieldVsLocalVariable() {
         shouldFailWithMessages '''
             // tag::typeinference_field_vs_local_variable[]
@@ -545,7 +544,6 @@ class TypeCheckingTest extends StaticTypeCheckingTestCase {
             SomeClass
         ''', 'Cannot find matching method java.lang.Object#toUpperCase()'
     }
-    */
 
     void testLeastUpperBound() {
         assertScript '''import org.codehaus.groovy.ast.ClassHelper

http://git-wip-us.apache.org/repos/asf/groovy/blob/8c454796/src/test/groovy/bugs/Groovy8355Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8355Bug.groovy b/src/test/groovy/bugs/Groovy8355Bug.groovy
deleted file mode 100644
index b512249..0000000
--- a/src/test/groovy/bugs/Groovy8355Bug.groovy
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- *  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 Groovy8355Bug extends GroovyTestCase {
-    void testGroovy8355() {
-        assertScript '''
-        import groovy.transform.CompileStatic
-        
-        @CompileStatic
-        class Foo {
-            Object str = new Object()
-            def bar() {
-                str = "str"
-                str.toUpperCase()
-            }
-        }
-        
-        assert "STR" == new Foo().bar()
-        '''
-    }
-
-    void test2() {
-        assertScript '''
-        import groovy.transform.CompileStatic
-        
-        @CompileStatic
-        class Foo {
-            Object str = new Object()
-            def bar() {
-                str = "str"
-                str.toUpperCase()
-            }
-            
-            def bar2() {
-                str = 1
-                str + 2
-            }
-        }
-        
-        Foo foo = new Foo()
-        assert "STR" == foo.bar()
-        assert 3 == foo.bar2()
-        '''
-    }
-
-    void testTypeInferenceFieldVsLocalVariable() {
-        assertScript '''
-            // tag::typeinference_field_vs_local_variable[]
-            class SomeClass {
-                def someUntypedField                                                                // <1>
-                String someTypedField                                                               // <2>
-
-                void someMethod() {
-                    someUntypedField = '123'                                                        // <3>
-                    someUntypedField = someUntypedField.toUpperCase()  // compile-time error        // <4>
-                }
-
-                void someSafeMethod() {
-                    someTypedField = '123'                                                          // <5>
-                    someTypedField = someTypedField.toUpperCase()                                   // <6>
-                }
-
-                void someMethodUsingLocalVariable() {
-                    def localVariable = '123'                                                       // <7>
-                    someUntypedField = localVariable.toUpperCase()                                  // <8>
-                }
-            }
-            // end::typeinference_field_vs_local_variable[]
-            SomeClass
-        '''
-    }
-
-    /*
-    void test3() {
-        assertScript '''
-        import groovy.transform.CompileStatic
-        
-        @CompileStatic
-        class Foo {
-            Object str = "str"
-            def bar() {
-                str.toUpperCase()
-            }
-        }
-        
-        assert "STR" == new Foo().bar()
-        '''
-    }
-    */
-}