Skip to content

Commit

Permalink
GROOVY-9385
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 6, 2020
1 parent 60209bd commit b04c8fc
Show file tree
Hide file tree
Showing 5 changed files with 674 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3691,4 +3691,73 @@ public void testCompileStatic9347a() {

runConformTest(sources, "6");
}

@Test
public void testCompileStatic9385() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"class Main {\n" +
" private int i\n" +
" int test() {\n" +
" { ->\n" +
" i += 1\n" +
" }.call()\n" +
" }\n" +
" static main(args) {\n" +
" print new Main().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "1");
}

@Test
public void testCompileStatic9385a() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"class Main {\n" +
" private int i\n" +
" int test() {\n" +
" { ->\n" +
" ++i\n" +
" }.call()\n" +
" }\n" +
" static main(args) {\n" +
" print new Main().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "1");
}

@Test
public void testCompileStatic9385b() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"class Main {\n" +
" private int i\n" +
" int test() {\n" +
" { ->\n" +
" i++\n" +
" }.call()\n" +
" }\n" +
" static main(args) {\n" +
" print new Main().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -244,15 +245,30 @@ public void visitMethod(final MethodNode node) {
* Adds special accessors and mutators for private fields so that inner classes can get/set them
*/
private static void addPrivateFieldsAccessors(ClassNode node) {
/* GRECLIPSE edit
Set<ASTNode> accessedFields = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
Set<ASTNode> mutatedFields = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION);
if (accessedFields == null && mutatedFields == null) return;
*/
Map<String, MethodNode> privateFieldAccessors = (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_ACCESSORS);
Map<String, MethodNode> privateFieldMutators = (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_MUTATORS);
if (privateFieldAccessors != null || privateFieldMutators != null) {
// already added
return;
}
// GRECLIPSE add
Set<ASTNode> accessedFields = node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
Set<ASTNode> mutatedFields = node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION);
if (accessedFields == null && mutatedFields == null) return;
// GROOVY-9385: mutation includes access in case of compound assignment or pre/post-increment/decrement
if (mutatedFields != null) {
if (accessedFields != null) {
accessedFields = new HashSet<>(accessedFields); accessedFields.addAll(mutatedFields);
} else {
accessedFields = mutatedFields;
}
}
// GRECLIPSE end
int acc = -1;
privateFieldAccessors = accessedFields != null ? new HashMap<String, MethodNode>() : null;
privateFieldMutators = mutatedFields != null ? new HashMap<String, MethodNode>() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -246,15 +247,30 @@ public void visitMethod(final MethodNode node) {
* Adds special accessors and mutators for private fields so that inner classes can get/set them
*/
private static void addPrivateFieldsAccessors(ClassNode node) {
/* GRECLIPSE edit
Set<ASTNode> accessedFields = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
Set<ASTNode> mutatedFields = (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION);
if (accessedFields == null && mutatedFields == null) return;
*/
Map<String, MethodNode> privateFieldAccessors = (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_ACCESSORS);
Map<String, MethodNode> privateFieldMutators = (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_MUTATORS);
if (privateFieldAccessors != null || privateFieldMutators != null) {
// already added
return;
}
// GRECLIPSE add
Set<ASTNode> accessedFields = node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
Set<ASTNode> mutatedFields = node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION);
if (accessedFields == null && mutatedFields == null) return;
// GROOVY-9385: mutation includes access in case of compound assignment or pre/post-increment/decrement
if (mutatedFields != null) {
if (accessedFields != null) {
accessedFields = new HashSet<>(accessedFields); accessedFields.addAll(mutatedFields);
} else {
accessedFields = mutatedFields;
}
}
// GRECLIPSE end
int acc = -1;
privateFieldAccessors = accessedFields != null ? new HashMap<String, MethodNode>() : null;
privateFieldMutators = mutatedFields != null ? new HashMap<String, MethodNode>() : null;
Expand Down
1 change: 1 addition & 0 deletions base/org.codehaus.groovy30/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<file-match-pattern match-pattern="groovy/transform/FieldASTTransformation.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/LazyASTTransformation.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/LogASTTransformation.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/sc/StaticCompilationVisitor.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/sc/transformers/BinaryExpressionTransformer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/sc/transformers/MethodCallExpressionTransformer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/transform/stc/AbstractExtensionMethodCache.java" include-pattern="false" />
Expand Down
Loading

0 comments on commit b04c8fc

Please sign in to comment.