You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/06/27 14:27:38 UTC

[groovy] branch GROOVY-8999 updated: GROOVY-8999 (pt.3): STC error for direct access to an inaccessible field

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

emilles pushed a commit to branch GROOVY-8999
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-8999 by this push:
     new 0f9efa6  GROOVY-8999 (pt.3): STC error for direct access to an inaccessible field
0f9efa6 is described below

commit 0f9efa6d77bb118e7da49030a14383870629070c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Jun 27 09:04:27 2020 -0500

    GROOVY-8999 (pt.3): STC error for direct access to an inaccessible field
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  7 ++++++
 .../stc/FieldsAndPropertiesSTCTest.groovy          | 29 ++++++++++++++++++++++
 .../groovy/classgen/asm/sc/bugs/Groovy7300.groovy  |  8 +++---
 3 files changed, 39 insertions(+), 5 deletions(-)

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 90558c6..193f26a 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1792,6 +1792,13 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     private boolean storeField(final FieldNode field, final PropertyExpression expressionToStoreOn, final ClassNode receiver, final ClassCodeVisitorSupport visitor, final String delegationData, final boolean lhsOfAssignment) {
         if (visitor != null) visitor.visitField(field);
         checkOrMarkPrivateAccess(expressionToStoreOn, field, lhsOfAssignment);
+
+        if (expressionToStoreOn instanceof AttributeExpression) { // TODO: expand to include PropertyExpression
+            if (!hasAccessToField(isSuperExpression(expressionToStoreOn.getObjectExpression()) ? typeCheckingContext.getEnclosingClassNode() : receiver, field)) {
+                addStaticTypeError("The field " + field.getDeclaringClass().getNameWithoutPackage() + "." + field.getName() + " is not accessible", expressionToStoreOn.getProperty());
+            }
+        }
+
         storeWithResolve(field.getOriginType(), receiver, field.getDeclaringClass(), field.isStatic(), expressionToStoreOn);
         if (delegationData != null) {
             expressionToStoreOn.putNodeMetaData(IMPLICIT_RECEIVER, delegationData);
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 4225cd9..5db8505 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -18,6 +18,8 @@
  */
 package groovy.transform.stc
 
+import groovy.test.NotYetImplemented
+
 /**
  * Unit tests for static type checking : fields and properties.
  */
@@ -115,6 +117,20 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
         ''', 'No such property: x for class: A'
     }
 
+    @NotYetImplemented
+    void testShouldComplainAboutMissingProperty3() {
+        shouldFailWithMessages '''
+            class A {
+                private x
+            }
+            class B extends A {
+                void test() {
+                    this.x
+                }
+            }
+        ''', 'The field A.x is not accessible'
+    }
+
     void testShouldComplainAboutMissingAttribute() {
         shouldFailWithMessages '''
             Object o = new Object()
@@ -151,6 +167,19 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
         ''', 'No such attribute: x for class: A'
     }
 
+    void testShouldComplainAboutMissingAttribute5() {
+        shouldFailWithMessages '''
+            class A {
+                private x
+            }
+            class B extends A {
+                void test() {
+                    this.@x
+                }
+            }
+        ''', 'The field A.x is not accessible'
+    }
+
     void testPropertyWithInheritance() {
         assertScript '''
             class A {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
index 2770ba3..58908dd 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
@@ -66,7 +66,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
     }
 
     void testUseSuperToBypassOverride2a() {
-        def err = shouldFail '''
+        shouldFailWithMessages '''
             abstract class A {
                 private x = 1
                 def getX() { 2 }
@@ -75,9 +75,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
                 @Override
                 def getX() { super.@x }
             }
-            assert new B().getX() == 1
-        '''
-
-        assert err =~ /No such field: x for class: A/ // TODO: Replace run-time error with compile-time error.
+            assert false
+        ''', 'The field A.x is not accessible'
     }
 }