Skip to content

Commit

Permalink
GROOVY-11369: STC: map entry before access method
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 7, 2024
1 parent 8df6a8b commit b505625
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,9 @@ protected boolean existsProperty(final PropertyExpression pexp, final boolean re
getter = allowStaticAccessToMember(getter, staticOnly);
if (getter == null) getter = findGetter(current, isserName, pexp.isImplicitThis());
getter = allowStaticAccessToMember(getter, staticOnly);
if (getter != null && !isThisExpression(objectExpression) && !isSuperExpression(objectExpression) && isOrImplements(objectExpressionType, MAP_TYPE)) {
getter = null; // GROOVY-11369: map entry comes before access method
}
List<MethodNode> setters = findSetters(current, setterName, false);
setters = allowStaticAccessToMember(setters, staticOnly);

Expand Down Expand Up @@ -1587,7 +1590,7 @@ protected boolean existsProperty(final PropertyExpression pexp, final boolean re
}
pexp.removeNodeMetaData(READONLY_PROPERTY);
return true;
} else if (getter != null && field == null) {
} else if (getter != null && (field == null || field.isFinal())) {
pexp.putNodeMetaData(READONLY_PROPERTY, Boolean.TRUE); // GROOVY-9127
}
}
Expand Down
44 changes: 29 additions & 15 deletions src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-5700, GROOVY-8788
// GROOVY-8788
void testMapPropertyAccess2() {
assertScript '''
def map = [key: 123]
Expand All @@ -626,31 +626,45 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {

void testMapPropertyAccess3() {
assertScript '''
Map map = [a:1, b:2]
assert map['a'] == 1
String bee = 'b'
assert map[bee] == 2
String key = 'foo'
Map map = [(key): 123]
@ASTTest(phase=INSTRUCTION_SELECTION, value={
assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
})
def val = map[key]
assert val == 123
'''
}

// GROOVY-5797
void testMapPropertyAccess4() {
assertScript '''
class C {
public static final Map TABLE = [key:'value']
def test(Map foo) {
def map = [baz: 1]
map[ foo.bar ]
}
String name = 'key'
assert C.TABLE[name] == 'value'
assert test(bar:'baz') == 1
'''
}

// GROOVY-5797
// GROOVY-11369
void testMapPropertyAccess5() {
assertScript '''
def test(Map foo) {
def map = [baz: 1]
map[ foo.bar ]
}
assert test(bar:'baz') == 1
def map = [:]
assert map.entry == null
assert map.empty == null
assert map.class == null
assert map.metaClass == null // TODO
map.entry = null
map.empty = null // not read-only property
map.class = null // not read-only property
map.metaClass = null
assert map.containsKey('entry')
assert map.containsKey('empty')
assert map.containsKey('class')
assert !map.containsKey('metaClass')
'''
}

Expand Down

0 comments on commit b505625

Please sign in to comment.