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/28 17:46:05 UTC
[groovy] branch GROOVY-8999 updated (0f9efa6 -> f6c6418)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY-8999
in repository https://gitbox.apache.org/repos/asf/groovy.git.
discard 0f9efa6 GROOVY-8999 (pt.3): STC error for direct access to an inaccessible field
new f6c6418 GROOVY-8999 (pt.3): STC error for direct access to an inaccessible field
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (0f9efa6)
\
N -- N -- N refs/heads/GROOVY-8999 (f6c6418)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/main/java/groovy/lang/MetaClassImpl.java | 4 ++--
.../org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java | 5 ++---
2 files changed, 4 insertions(+), 5 deletions(-)
[groovy] 01/01: GROOVY-8999 (pt.3): STC error for direct access to
an inaccessible field
Posted by em...@apache.org.
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
commit f6c641864cd71f1e6c3dd4dcdc14da7fcc6c7f16
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
---
src/main/java/groovy/lang/MetaClassImpl.java | 4 +--
.../transform/stc/StaticTypeCheckingVisitor.java | 12 ++++++---
.../stc/FieldsAndPropertiesSTCTest.groovy | 29 ++++++++++++++++++++++
.../groovy/classgen/asm/sc/bugs/Groovy7300.groovy | 8 +++---
4 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 063fe49..85a321a 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -2936,7 +2936,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- throw new MissingFieldException(attribute, sender);
+ throw new MissingFieldException(attribute, !useSuper ? theClass : theClass.getSuperclass());
}
/**
@@ -2976,7 +2976,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- throw new MissingFieldException(attribute, sender);
+ throw new MissingFieldException(attribute, !useSuper ? theClass : theClass.getSuperclass());
}
/**
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..cc0d98b 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -127,7 +127,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
@@ -1475,7 +1474,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
return true;
}
- Queue<ClassNode> queue = new LinkedList<>();
+ LinkedList<ClassNode> queue = new LinkedList<>();
queue.add(receiverType);
if (isPrimitiveType(receiverType)) {
queue.add(getWrapper(receiverType));
@@ -1487,7 +1486,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
FieldNode field = current.getDeclaredField(propertyName);
if (field == null) {
if (current.getSuperClass() != null) {
- queue.add(current.getUnresolvedSuperClass());
+ queue.addFirst(current.getUnresolvedSuperClass());
}
for (ClassNode face : current.getAllInterfaces()) {
queue.add(GenericsUtils.parameterizeType(current, face));
@@ -1792,6 +1791,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'
}
}