Skip to content

Commit 559ca2c

Browse files
committed
GROOVY-7996
1 parent 146a2e3 commit 559ca2c

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,46 @@ public void testCompileStatic7996d() {
15811581
runConformTest(sources, "true");
15821582
}
15831583

1584+
@Test
1585+
public void testCompileStatic7996e() {
1586+
//@formatter:off
1587+
String[] sources = {
1588+
"Main.groovy",
1589+
"import groovy.transform.*\n" +
1590+
"import org.codehaus.groovy.ast.DynamicVariable\n" +
1591+
"import org.codehaus.groovy.ast.expr.VariableExpression\n" +
1592+
"import static org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE\n" +
1593+
"import static org.codehaus.groovy.transform.stc.StaticTypesMarker.INFERRED_TYPE\n" +
1594+
"import static org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys.PROPERTY_OWNER\n" +
1595+
1596+
"class JSON {\n" +
1597+
" def get(String name) {\n" +
1598+
" }\n" +
1599+
"}\n" +
1600+
"class POGO {\n" +
1601+
" Number getAnswer() {\n" +
1602+
" }\n" +
1603+
" @CompileStatic\n" +
1604+
" void usage() {\n" +
1605+
" new JSON().with {\n" +
1606+
" @ASTTest(phase=CLASS_GENERATION, value={\n" +
1607+
" def vexp = node.rightExpression\n" +
1608+
" assert vexp instanceof VariableExpression\n" +
1609+
" assert vexp.accessedVariable instanceof DynamicVariable\n" +
1610+
" assert vexp.getNodeMetaData(INFERRED_TYPE) == OBJECT_TYPE\n" +
1611+
" assert vexp.getNodeMetaData(PROPERTY_OWNER).name == 'JSON'\n" +
1612+
" })\n" +
1613+
" def result = answer\n" + // "answer" accessed from JSON; "getAnswer()" invoked from POGO
1614+
" }\n" +
1615+
" }\n" +
1616+
"}\n" +
1617+
"new POGO().usage()\n",
1618+
};
1619+
//@formatter:on
1620+
1621+
runConformTest(sources, "");
1622+
}
1623+
15841624
@Test
15851625
public void testCompileStatic8051() {
15861626
//@formatter:off

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,10 @@ private boolean tryVariableExpressionAsProperty(final VariableExpression vexp, f
774774
storeType(vexp, type != null ? type: pexp.getType());
775775

776776
String receiver = vexp.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
777-
// GROOVY-7701: correct false assumption made by VariableScopeVisitor
778-
if (receiver != null && !receiver.endsWith("owner") && !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
777+
Boolean dynamic = pexp.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION);
778+
// GROOVY-7701, GROOVY-7996: correct false assumption made by VariableScopeVisitor
779+
if (((receiver != null && !receiver.endsWith("owner")) || Boolean.TRUE.equals(dynamic))
780+
&& !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
779781
vexp.setAccessedVariable(new DynamicVariable(dynName, false));
780782
}
781783
return true;
@@ -1694,10 +1696,11 @@ protected boolean existsProperty(final PropertyExpression pexp, final boolean re
16941696
}
16951697
if (mopMethod == null) mopMethod = receiverType.getMethod("propertyMissing", new Parameter[]{new Parameter(STRING_TYPE, "propertyName")});
16961698

1697-
if (mopMethod != null && !mopMethod.isSynthetic()) {
1699+
if (mopMethod != null && !mopMethod.isStatic() && !mopMethod.isSynthetic()) {
16981700
pexp.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
16991701
pexp.removeNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
17001702
pexp.removeNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
1703+
visitor.visitMethod(mopMethod);
17011704
return true;
17021705
}
17031706
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,10 @@ private boolean tryVariableExpressionAsProperty(final VariableExpression vexp, f
685685
storeType(vexp, Optional.ofNullable(type).orElseGet(pexp::getType));
686686

687687
String receiver = vexp.getNodeMetaData(IMPLICIT_RECEIVER);
688-
// GROOVY-7701: correct false assumption made by VariableScopeVisitor
689-
if (receiver != null && !receiver.endsWith("owner") && !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
688+
Boolean dynamic = pexp.getNodeMetaData(DYNAMIC_RESOLUTION);
689+
// GROOVY-7701, GROOVY-7996: correct false assumption made by VariableScopeVisitor
690+
if (((receiver != null && !receiver.endsWith("owner")) || Boolean.TRUE.equals(dynamic))
691+
&& !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
690692
vexp.setAccessedVariable(new DynamicVariable(dynName, false));
691693
}
692694
return true;
@@ -1661,10 +1663,11 @@ protected boolean existsProperty(final PropertyExpression pexp, final boolean re
16611663
}
16621664
if (mopMethod == null) mopMethod = receiverType.getMethod("propertyMissing", new Parameter[]{new Parameter(STRING_TYPE, "propertyName")});
16631665

1664-
if (mopMethod != null && !mopMethod.isSynthetic()) {
1666+
if (mopMethod != null && !mopMethod.isStatic() && !mopMethod.isSynthetic()) {
16651667
pexp.putNodeMetaData(DYNAMIC_RESOLUTION, Boolean.TRUE);
16661668
pexp.removeNodeMetaData(DECLARATION_INFERRED_TYPE);
16671669
pexp.removeNodeMetaData(INFERRED_TYPE);
1670+
visitor.visitMethod(mopMethod);
16681671
return true;
16691672
}
16701673
}

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,10 @@ private boolean tryVariableExpressionAsProperty(final VariableExpression vexp, f
685685
storeType(vexp, Optional.ofNullable(type).orElseGet(pexp::getType));
686686

687687
String receiver = vexp.getNodeMetaData(IMPLICIT_RECEIVER);
688-
// GROOVY-7701: correct false assumption made by VariableScopeVisitor
689-
if (receiver != null && !receiver.endsWith("owner") && !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
688+
Boolean dynamic = pexp.getNodeMetaData(DYNAMIC_RESOLUTION);
689+
// GROOVY-7701, GROOVY-7996: correct false assumption made by VariableScopeVisitor
690+
if (((receiver != null && !receiver.endsWith("owner")) || Boolean.TRUE.equals(dynamic))
691+
&& !(vexp.getAccessedVariable() instanceof DynamicVariable)) {
690692
vexp.setAccessedVariable(new DynamicVariable(dynName, false));
691693
}
692694
return true;
@@ -1653,10 +1655,11 @@ protected boolean existsProperty(final PropertyExpression pexp, final boolean re
16531655
}
16541656
if (mopMethod == null) mopMethod = receiverType.getMethod("propertyMissing", new Parameter[]{new Parameter(STRING_TYPE, "propertyName")});
16551657

1656-
if (mopMethod != null && !mopMethod.isSynthetic()) {
1658+
if (mopMethod != null && !mopMethod.isStatic() && !mopMethod.isSynthetic()) {
16571659
pexp.putNodeMetaData(DYNAMIC_RESOLUTION, Boolean.TRUE);
16581660
pexp.removeNodeMetaData(DECLARATION_INFERRED_TYPE);
16591661
pexp.removeNodeMetaData(INFERRED_TYPE);
1662+
visitor.visitMethod(mopMethod);
16601663
return true;
16611664
}
16621665
}

0 commit comments

Comments
 (0)