You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/11/21 06:45:19 UTC

[groovy] 01/03: GROOVY-9093: SC: add compile-time error for inaccessible field or getter (port to 3_0_X)

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

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

commit c27b2c3e5169ca5afd89ffbb261757e345be31c4
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Oct 23 20:53:03 2020 -0500

    GROOVY-9093: SC: add compile-time error for inaccessible field or getter (port to 3_0_X)
---
 .../groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java   | 13 ++++++++++++-
 src/test/groovy/bugs/Groovy7165.groovy                      |  2 +-
 2 files changed, 13 insertions(+), 2 deletions(-)

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 c4adf83..4cedc74 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
@@ -453,7 +453,12 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
         if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
         if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;
 
-        MethodCallExpression call = callX(receiver, "getProperty", args(constX(property)));
+        boolean isScriptVariable = (receiverType.isScript() && receiver instanceof VariableExpression && ((VariableExpression) receiver).getAccessedVariable() == null);
+        if (!isScriptVariable && controller.getClassNode().getOuterClass() == null) { // inner class still needs dynamic property sequence
+            addPropertyAccessError(receiver, propertyName, receiverType);
+        }
+
+        MethodCallExpression call = callX(receiver, "getProperty", args(constX(propertyName)));
         call.setImplicitThis(implicitThis);
         call.setMethodTarget(GROOVYOBJECT_GETPROPERTY_METHOD);
         call.setSafe(safe);
@@ -876,4 +881,10 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
         }
         return false;
     }*/
+
+    private void addPropertyAccessError(final Expression receiver, final String propertyName, final ClassNode receiverType) {
+        String receiverName = (receiver instanceof ClassExpression ? receiver.getType() : receiverType).toString(false);
+        String message = "Access to " + receiverName + "#" + propertyName + " is forbidden";
+        controller.getSourceUnit().addError(new SyntaxException(message, receiver));
+    }
 }
diff --git a/src/test/groovy/bugs/Groovy7165.groovy b/src/test/groovy/bugs/Groovy7165.groovy
index 4ee9a12..5880057 100644
--- a/src/test/groovy/bugs/Groovy7165.groovy
+++ b/src/test/groovy/bugs/Groovy7165.groovy
@@ -74,7 +74,7 @@ final class Groovy7165 {
             new B().test()
         '''
 
-        assert err =~ /MissingPropertyException: No such property: CONST for class: B/
+        assert err =~ /Access to B#CONST is forbidden/
     }
 
     @Test