Skip to content

Commit

Permalink
Fix for #1241: ArrayExpression: multi-dimension init has array elem type
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 11, 2021
1 parent 715a51d commit 0500084
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,150 @@

public final class GroovySimpleTests extends GroovyCompilerTestSuite {

@Test
public void testArrayBasic1() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[] array = new int[0]\n" +
"assert array.length == 0\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[I");
}

@Test
public void testArrayBasic2() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[] array = [123]\n" +
"assert array.length == 1\n" +
"assert array[0] == 123\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[I");
}

@Test
public void testArrayBasic3() {
assumeTrue(isParrotParser());

//@formatter:off
String[] sources = {
"Script.groovy",
"int[] array = new int[]{123}\n" +
"assert array.length == 1\n" +
"assert array[0] == 123\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[I");
}

@Test
public void testArrayMulti1() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = new int[1][1]\n" +
"assert array.length == 1\n" +
"assert array[0][0] == 0\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testArrayMulti2() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = [ new int[1] ]\n" +
"assert array.length == 1\n" +
"assert array[0][0] == 0\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testArrayMulti3() {
assumeTrue(isParrotParser());

//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = new int[][]{ new int[]{1}, new int[]{2} }\n" +
"assert array.length == 2\n" +
"assert array[0][0] == 1\n" +
"assert array[1][0] == 2\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testArrayMulti4() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = [ [1], [2] ]\n" +
"assert array.length == 2\n" +
"assert array[0][0] == 1\n" +
"assert array[1][0] == 2\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testArrayMulti5() {
//@formatter:off
String[] sources = {
"Script.groovy",
"int[][] array = [ 1, 2 ]\n" + // runtime converts to arrays of length 1
"assert array.length == 2\n" +
"assert array[0][0] == 1\n" +
"assert array[1][0] == 2\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[I");
}

@Test
public void testArrayMulti6() {
//@formatter:off
String[] sources = {
"Script.groovy",
"String[][] array = [ '1', '23' ]\n" + // runtime converts to arrays of characters
"assert array.length == 2\n" +
"assert array[0].length == 1\n" +
"assert array[0][0] == '1'\n" +
"assert array[1].length == 2\n" +
"assert array[1][0] == '2'\n" +
"print array.class.name\n",
};
//@formatter:on

runConformTest(sources, "[[Ljava.lang.String;");
}

@Test
public void testClosuresBasic() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3582,15 +3582,13 @@ public Expression visitCreator(CreatorContext ctx) {
throw createParsingFailedException("dimension should be empty", dimWithExprList.get(0).getV3());
}

/* GRECLIPSE edit
ClassNode elementType = classNode;
for (int i = 0, n = emptyDimList.size() - 1; i < n; i += 1) {
elementType = this.createArrayType(elementType);
}
*/
arrayExpression =
new ArrayExpression(
classNode,
elementType,
this.visitArrayInitializer(ctx.arrayInitializer()));

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3389,15 +3389,13 @@ public Expression visitCreator(final CreatorContext ctx) {
throw createParsingFailedException("dimension should be empty", dimWithExprList.get(0).getV3());
}

/* GRECLIPSE edit
ClassNode elementType = classNode;
for (int i = 0, n = emptyDimList.size() - 1; i < n; i += 1) {
elementType = this.createArrayType(elementType);
}
*/
arrayExpression =
new ArrayExpression(
classNode,
elementType,
this.visitArrayInitializer(ctx.arrayInitializer()));

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void visitMethod(MethodNode node) {
@Override
public void visitArrayExpression(ArrayExpression expression) {
if (expression.getEnd() > 0) {
check(expression.getElementType(), expression.getNameStart(), expression.getNameEnd() + 1);
check(GroovyUtils.getBaseType(expression.getType()), expression.getNameStart(), expression.getNameEnd() + 1);
}
super.visitArrayExpression(expression);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -336,9 +336,10 @@ public void visitClosureExpression(ClosureExpression expression) {

@Override
public void visitArrayExpression(ArrayExpression expression) {
ClassNode baseType = GroovyUtils.getBaseType(expression.getType());
analyzeNode(expression);
analyzeType(expression.getElementType());
clear(expression.getElementType());
analyzeType(baseType);
clear(baseType);
super.visitArrayExpression(expression);
clear(expression);
}
Expand Down

0 comments on commit 0500084

Please sign in to comment.