Skip to content

Commit

Permalink
GROOVY-11387: STC: class field within closure
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 10, 2024
1 parent 937cc54 commit da99cd9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,17 @@ public void visitVariableExpression(final VariableExpression vexp) {
} else if (accessedVariable instanceof FieldNode) {
FieldNode accessedField = (FieldNode) accessedVariable;
ClassNode temporaryType = getInferredTypeFromTempInfo(vexp, null); // GROOVY-9454
if (enclosingClosure != null) {
tryVariableExpressionAsProperty(vexp, name);
} else if (getOutermost(accessedField.getDeclaringClass()) == getOutermost(typeCheckingContext.getEnclosingClassNode())
|| !tryVariableExpressionAsProperty(vexp, name)) { // GROOVY-10981: check for property before super class field
checkOrMarkPrivateAccess(vexp, accessedField, typeCheckingContext.isTargetOfEnclosingAssignment(vexp));
if (temporaryType == null) storeType(vexp, getType(vexp));
}
if (temporaryType != null && !temporaryType.equals(OBJECT_TYPE)) {
vexp.putNodeMetaData(INFERRED_TYPE, temporaryType);
boolean declaredInScope = (getOutermost(accessedField.getDeclaringClass()) == getOutermost(typeCheckingContext.getEnclosingClassNode())
&& (enclosingClosure == null || accessedField.isStatic())); // GROOVY-9655, GROOVY-9683, GROOVY-9695, GROOVY-9768, GROOVY-11387
if (declaredInScope || tryVariableExpressionAsProperty(vexp, name)) { // GROOVY-10981: check for property before super class field
if (temporaryType == null) {
storeType(vexp, getType(vexp));
} else if (!temporaryType.equals(OBJECT_TYPE)) {
vexp.putNodeMetaData(INFERRED_TYPE, temporaryType);
}
if (declaredInScope) {
checkOrMarkPrivateAccess(vexp, accessedField, typeCheckingContext.isTargetOfEnclosingAssignment(vexp));
}
}
} else if (accessedVariable instanceof PropertyNode) {
// we must be careful, because the property node may be of a wrong type:
Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,21 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
def accept = HttpHeaders.ACCEPT
assert accept == 'Accept'
'''
assertScript '''
class MapSub extends HashMap<String,String> {
private static final short ANSWER = 42
def m() {
{->
@ASTTest(phase=INSTRUCTION_SELECTION, value={
assert node.getNodeMetaData(INFERRED_TYPE) == short_TYPE
})
def answer = ANSWER
}()
}
}
Object answer = new MapSub().m()
assert answer == 42
'''
}

void testTypeCheckerDoesNotThinkPropertyIsReadOnly() {
Expand Down

0 comments on commit da99cd9

Please sign in to comment.