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 2019/04/23 04:55:01 UTC
[groovy] branch GROOVY_2_5_X updated (b88184f -> 888411c)
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a change to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.
from b88184f update notices - some still had 2018
new 336ec2d GROOVY-9077: STC: inferred type of property that resolves to method not always stored
new 888411c GROOVY-5961: Variable scope not checked properly for AIC in static method
The 2 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:
.../groovy/classgen/InnerClassVisitorHelper.java | 2 +-
.../groovy/classgen/VariableScopeVisitor.java | 9 +++-
.../transform/stc/StaticTypeCheckingVisitor.java | 3 +-
src/test/groovy/bugs/VariableScopingBug.groovy | 42 +++++++++++++------
.../transform/stc/TypeInferenceSTCTest.groovy | 48 ++++++++++++++++++----
5 files changed, 79 insertions(+), 25 deletions(-)
[groovy] 01/02: GROOVY-9077: STC: inferred type of property that
resolves to method not always stored
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 336ec2d88b301c27d1dc8adaf71010ab7b2d2b9a
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Apr 22 23:31:24 2019 +1000
GROOVY-9077: STC: inferred type of property that resolves to method not always stored
---
.../transform/stc/StaticTypeCheckingVisitor.java | 3 +-
.../transform/stc/TypeInferenceSTCTest.groovy | 48 ++++++++++++++++++----
2 files changed, 42 insertions(+), 9 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 bc67c5e..749cbef 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1524,6 +1524,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (getter != null) {
ClassNode cn = inferReturnTypeGenerics(current, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
storeInferredTypeForPropertyExpression(pexp, cn);
+ storeTargetMethod(pexp, getter);
pexp.removeNodeMetaData(StaticTypesMarker.READONLY_PROPERTY);
String delegationData = receiver.getData();
if (delegationData != null)
@@ -1588,7 +1589,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
ClassNode cn = inferReturnTypeGenerics(dgmReceiver, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
storeInferredTypeForPropertyExpression(pexp, cn);
-
+ storeTargetMethod(pexp, getter);
return true;
}
}
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index e9acc12..f570378 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -769,14 +769,46 @@ Thing.run()
// GROOVY-6574
void testShouldInferPrimitiveBoolean() {
assertScript '''
-def foo(Boolean o) {
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
- assert node.getNodeMetaData(INFERRED_TYPE) == boolean_TYPE
- })
- boolean b = o
- println b
-}
+ def foo(Boolean o) {
+ @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ assert node.getNodeMetaData(INFERRED_TYPE) == boolean_TYPE
+ })
+ boolean b = o
+ println b
+ }
+ '''
+ }
+
+ // GROOVY-9077
+ void testInferredTypeForPropertyThatResolvesToMethod() {
+ assertScript '''
+ import groovy.transform.*
+ import static org.codehaus.groovy.transform.stc.StaticTypesMarker.DIRECT_METHOD_CALL_TARGET
+
+ @CompileStatic
+ void meth() {
+ def items = [1, 2] as LinkedList
+
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
+ node = node.rightExpression
+ assert node.class.name.contains('PropertyExpression')
+ def target = node.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
+ assert target != null
+ assert target.declaringClass.name == 'java.util.LinkedList'
+ })
+ def one = items.first
+
+ @ASTTest(phase=CLASS_GENERATION, value={
+ node = node.rightExpression
+ assert node.class.name.contains('MethodCallExpression')
+ def target = node.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
+ assert target != null
+ assert target.declaringClass.name == 'java.util.LinkedList'
+ })
+ def alsoOne = items.peek()
+ }
+
+ meth()
'''
}
}
-
[groovy] 02/02: GROOVY-5961: Variable scope not checked properly
for AIC in static method
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 888411c7256a8a862416d2c4fe2de4448edf1091
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Apr 23 14:51:30 2019 +1000
GROOVY-5961: Variable scope not checked properly for AIC in static method
---
.../groovy/classgen/InnerClassVisitorHelper.java | 2 +-
.../groovy/classgen/VariableScopeVisitor.java | 9 +++--
src/test/groovy/bugs/VariableScopingBug.groovy | 42 +++++++++++++++-------
3 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java b/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
index 7ed0629..0971146 100644
--- a/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
+++ b/src/main/java/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
@@ -103,7 +103,7 @@ public abstract class InnerClassVisitorHelper extends ClassCodeVisitorSupport {
protected static boolean isStatic(InnerClassNode node) {
VariableScope scope = node.getVariableScope();
- if (scope != null) return scope.isInStaticContext();
+ if (scope != null) return scope.getParent().isInStaticContext();
return (node.getModifiers() & Opcodes.ACC_STATIC) != 0;
}
diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 18a94d5..66f314d 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -160,7 +160,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
// if we are in a class and no variable is declared until
// now, then we can break the loop, because we are allowed
// to declare a variable of the same name as a class member
- if (scope.getClassScope() != null) break;
+ if (scope.getClassScope() != null && !isAnonymous(scope.getClassScope())) break;
if (scope.getDeclaredVariable(var.getName()) != null) {
// variable already declared
@@ -204,6 +204,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
Variable ret = findClassMember(cn.getSuperClass(), name);
if (ret != null) return ret;
+ if (isAnonymous(cn)) return null;
return findClassMember(cn.getOuterClass(), name);
}
@@ -255,7 +256,9 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
if (!(staticScope && !staticMember))
var = member;
}
- break;
+ // GROOVY-5961
+ if (!isAnonymous(classScope))
+ break;
}
scope = scope.getParent();
}
@@ -557,6 +560,8 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
pushState();
InnerClassNode innerClass = (InnerClassNode) call.getType();
innerClass.setVariableScope(currentScope);
+ currentScope.setClassScope(innerClass);
+ currentScope.setInStaticContext(false);
for (MethodNode method : innerClass.getMethods()) {
Parameter[] parameters = method.getParameters();
if (parameters.length == 0) parameters = null; // null means no implicit "it"
diff --git a/src/test/groovy/bugs/VariableScopingBug.groovy b/src/test/groovy/bugs/VariableScopingBug.groovy
index b8d1d95..7586b1c 100644
--- a/src/test/groovy/bugs/VariableScopingBug.groovy
+++ b/src/test/groovy/bugs/VariableScopingBug.groovy
@@ -18,14 +18,10 @@
*/
package groovy.bugs
-/**
- */
class VariableScopingBug extends TestSupport {
- void testBug() {
- // undeclared variable x
-
- shouldFail {
+ void testUndeclaredVariable() {
+ shouldFail(MissingPropertyException) {
def shell = new GroovyShell()
shell.evaluate("""
class SomeTest {
@@ -35,17 +31,18 @@ class VariableScopingBug extends TestSupport {
}
for (t in 0..3) {
- for (y in x) {
+ for (y in x) { // previous x no longer be in scope
println x
}
}
}
- }
- new SomeTest().run()""")
- }
+ }
+ new SomeTest().run()
+ """)
+ }
}
- void testVariableReuse() {
+ void testVariableReuseAllowedInDifferentScopes() {
def shell = new GroovyShell()
shell.evaluate("""
for (z in 0..2) {
@@ -55,6 +52,25 @@ class VariableScopingBug extends TestSupport {
for (t in 0..3) {
def x = 123
println x
- }""")
+ }
+ """)
+ }
+
+ // GROOVY-5961
+ void testVariableInAicInsideStaticMethod() {
+ def shell = new GroovyShell()
+ shell.evaluate("""
+ static foo() {
+ new LinkedList([1, 2]) {
+ int count
+ Object get(int i) { super.get(count++) }
+ }
+ }
+
+ def l = foo()
+ assert l.count == 0
+ assert l[0] == 1
+ assert l.count == 1
+ """)
}
-}
\ No newline at end of file
+}