Skip to content

Commit 42f4fc6

Browse files
committed
Fix for #999: set line and column of this expression for error reporting
1 parent ed2387f commit 42f4fc6

File tree

12 files changed

+1523
-66
lines changed

12 files changed

+1523
-66
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/ErrorRecoveryTests.java

+60
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertTrue;
2424

25+
import java.util.List;
26+
2527
import org.codehaus.groovy.ast.ClassHelper;
2628
import org.codehaus.groovy.ast.ClassNode;
2729
import org.codehaus.groovy.ast.MethodNode;
@@ -1468,6 +1470,46 @@ public void testParsingRecovery_GRE494_2() {
14681470
"}\n");
14691471
}
14701472

1473+
@Test
1474+
public void testParsingRecovery_GRE495() {
1475+
//@formatter:off
1476+
String[] sources = {
1477+
"A.groovy",
1478+
"class Bar {}\n" +
1479+
"class Foo extends Bar { }\n" +
1480+
"class BBB extends Fo",
1481+
};
1482+
//@formatter:on
1483+
1484+
runNegativeTest(sources,
1485+
"----------\n" +
1486+
"1. ERROR in A.groovy (at line 3)\n" +
1487+
"\tclass BBB extends Fo\n" +
1488+
"\t ^^\n" +
1489+
"Groovy:unable to resolve class Fo\n" +
1490+
"----------\n" +
1491+
"2. ERROR in A.groovy (at line 3)\n" +
1492+
"\tclass BBB extends Fo\n" +
1493+
"\t ^\n" +
1494+
"Groovy:Malformed class declaration\n" +
1495+
"----------\n");
1496+
1497+
// missing end curly, but that shouldn't cause us to discard what we successfully parsed
1498+
ModuleNode mn = getModuleNode("A.groovy");
1499+
assertNotNull(mn);
1500+
List<ClassNode> l = mn.getClasses();
1501+
for (int i = 0; i < l.size(); i++) {
1502+
System.out.println(l.get(i));
1503+
}
1504+
assertFalse(mn.encounteredUnrecoverableError());
1505+
ClassNode cn = mn.getClasses().get(2);
1506+
assertNotNull(cn);
1507+
assertEquals("Foo", cn.getName());
1508+
cn = mn.getClasses().get(1);
1509+
assertNotNull(cn);
1510+
assertEquals("BBB", cn.getName());
1511+
}
1512+
14711513
@Test // variations: 'import' 'import static' 'import ' 'import static ' 'import com.' 'import static com.'
14721514
public void testParsingRecovery_Imports1() {
14731515
//@formatter:off
@@ -1831,6 +1873,24 @@ public void testParsingRecovery_SafeDereferencing() {
18311873
"}\n");
18321874
}
18331875

1876+
@Test
1877+
public void testParsingRecovery_ScriptWithError() {
1878+
//@formatter:off
1879+
String[] sources = {
1880+
"Foo.groovy",
1881+
"print Coolio!",
1882+
};
1883+
//@formatter:on
1884+
1885+
runNegativeTest(sources,
1886+
"----------\n" +
1887+
"1. ERROR in Foo.groovy (at line 1)\n" +
1888+
"\tprint Coolio!\n" +
1889+
"\t ^\n" +
1890+
"Groovy:expecting EOF, found \'!\'\n" +
1891+
"----------\n");
1892+
}
1893+
18341894
@Test
18351895
public void testUnrecoverableErrors_GRE755_1() {
18361896
//@formatter:off

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java

+31-62
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertNotNull;
2322
import static org.junit.Assert.assertTrue;
2423
import static org.junit.Assert.fail;
2524
import static org.junit.Assume.assumeTrue;
2625

2726
import java.io.File;
2827
import java.util.Iterator;
29-
import java.util.List;
3028
import java.util.Map;
3129

32-
import org.codehaus.groovy.ast.ClassNode;
33-
import org.codehaus.groovy.ast.ModuleNode;
3430
import org.codehaus.jdt.groovy.internal.compiler.ast.EventListener;
3531
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyClassScope;
3632
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration;
@@ -3426,6 +3422,37 @@ public void testCallGroovyObjectMethods_invokeMethod() {
34263422
runConformTest(sources, "success");
34273423
}
34283424

3425+
@Test
3426+
public void testSuperCallWithPrivateMethod() {
3427+
//@formatter:off
3428+
String[] sources = {
3429+
"AandC.groovy",
3430+
"abstract class A {\n" +
3431+
" private x() {\n" +
3432+
" }\n" +
3433+
"}\n" +
3434+
"@groovy.transform.CompileStatic\n" +
3435+
"class C extends A {\n" +
3436+
" private y() {\n" +
3437+
" x()\n" +
3438+
" }\n" +
3439+
"}\n",
3440+
};
3441+
//@formatter:on
3442+
3443+
runNegativeTest(sources,
3444+
"----------\n" +
3445+
"1. ERROR in AandC.groovy (at line 8)\n" +
3446+
"\tx()\n" +
3447+
(!isAtLeastGroovy(30)
3448+
? "\t^\n" +
3449+
"Groovy:Cannot call private method A#x from class C\n"
3450+
: "\t^^^\n" +
3451+
"Groovy:[Static type checking] - Cannot find matching method C#x(). Please check if the declared type is correct and if the method exists.\n"
3452+
) +
3453+
"----------\n");
3454+
}
3455+
34293456
@Test
34303457
public void testSuperCallWithStaticMethod() {
34313458
//@formatter:off
@@ -4424,24 +4451,6 @@ public void testScriptCallJava() {
44244451
runConformTest(sources, "abc");
44254452
}
44264453

4427-
@Test
4428-
public void testScriptWithError() {
4429-
//@formatter:off
4430-
String[] sources = {
4431-
"Foo.groovy",
4432-
"print Coolio!",
4433-
};
4434-
//@formatter:on
4435-
4436-
runNegativeTest(sources,
4437-
"----------\n" +
4438-
"1. ERROR in Foo.groovy (at line 1)\n" +
4439-
"\tprint Coolio!\n" +
4440-
"\t ^\n" +
4441-
"Groovy:expecting EOF, found \'!\'\n" +
4442-
"----------\n");
4443-
}
4444-
44454454
@Test
44464455
public void testConfigScriptWithError() {
44474456
Map<String, String> options = getCompilerOptions();
@@ -5023,46 +5032,6 @@ public void testSecondaryTypeTagging() {
50235032
assertTrue((tds[3].bits & ASTNode.IsSecondaryType) != 0);
50245033
}
50255034

5026-
@Test
5027-
public void testParsingIncompleteClassDeclaration_495() {
5028-
//@formatter:off
5029-
String[] sources = {
5030-
"A.groovy",
5031-
"class Bar {}\n" +
5032-
"class Foo extends Bar { }\n" +
5033-
"class BBB extends Fo",
5034-
};
5035-
//@formatter:on
5036-
5037-
runNegativeTest(sources,
5038-
"----------\n" +
5039-
"1. ERROR in A.groovy (at line 3)\n" +
5040-
"\tclass BBB extends Fo\n" +
5041-
"\t ^^\n" +
5042-
"Groovy:unable to resolve class Fo\n" +
5043-
"----------\n" +
5044-
"2. ERROR in A.groovy (at line 3)\n" +
5045-
"\tclass BBB extends Fo\n" +
5046-
"\t ^\n" +
5047-
"Groovy:Malformed class declaration\n" +
5048-
"----------\n");
5049-
5050-
// missing end curly, but that shouldn't cause us to discard what we successfully parsed
5051-
ModuleNode mn = getModuleNode("A.groovy");
5052-
assertNotNull(mn);
5053-
List<ClassNode> l = mn.getClasses();
5054-
for (int i = 0; i < l.size(); i++) {
5055-
System.out.println(l.get(i));
5056-
}
5057-
assertFalse(mn.encounteredUnrecoverableError());
5058-
ClassNode cn = mn.getClasses().get(2);
5059-
assertNotNull(cn);
5060-
assertEquals("Foo", cn.getName());
5061-
cn = mn.getClasses().get(1);
5062-
assertNotNull(cn);
5063-
assertEquals("BBB", cn.getName());
5064-
}
5065-
50665035
@Test
50675036
public void testMultiCatch0() {
50685037
assumeTrue(!isAtLeastJava(JDK7));

base/org.codehaus.groovy24/.checkstyle

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<file-match-pattern match-pattern="groovy/transform/FieldASTTransformation.java" include-pattern="false" />
7575
<file-match-pattern match-pattern="groovy/transform/LazyASTTransformation.java" include-pattern="false" />
7676
<file-match-pattern match-pattern="groovy/transform/LogASTTransformation.java" include-pattern="false" />
77+
<file-match-pattern match-pattern="groovy/transform/NewifyASTTransformation.java" include-pattern="false" />
7778
<file-match-pattern match-pattern="groovy/transform/sc/StaticCompilationVisitor.java" include-pattern="false" />
7879
<file-match-pattern match-pattern="groovy/transform/sc/transformers/MethodCallExpressionTransformer.java" include-pattern="false" />
7980
<file-match-pattern match-pattern="groovy/transform/stc/StaticTypeCheckingSupport.java" include-pattern="false" />

base/org.codehaus.groovy24/src/org/codehaus/groovy/antlr/AntlrParserPlugin.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,13 @@ protected Expression methodCallExpression(AST methodCallNode) {
30293029
selector = objectNode.getNextSibling();
30303030
} else {
30313031
implicitThis = true;
3032+
/* GRECLIPSE edit
30323033
objectExpression = VariableExpression.THIS_EXPRESSION;
3034+
*/
3035+
objectExpression = new VariableExpression("this");
3036+
objectExpression.setLineNumber(node.getLine());
3037+
objectExpression.setColumnNumber(node.getColumn());
3038+
// GRECLIPSE end
30333039
selector = node;
30343040
}
30353041

@@ -3046,8 +3052,7 @@ protected Expression methodCallExpression(AST methodCallNode) {
30463052
objectExpression = VariableExpression.SUPER_EXPRESSION;
30473053
}
30483054
} else if (isPrimitiveTypeLiteral(selector)) {
3049-
throw new ASTRuntimeException(selector, "Primitive type literal: " + selector.getText()
3050-
+ " cannot be used as a method name");
3055+
throw new ASTRuntimeException(selector, "Primitive type literal: " + selector.getText() + " cannot be used as a method name");
30513056
} else if (isType(SELECT_SLOT, selector)) {
30523057
Expression field = expression(selector.getFirstChild(), true);
30533058
AttributeExpression attributeExpression = new AttributeExpression(objectExpression, field, node.getType() != DOT);
@@ -3057,8 +3062,7 @@ protected Expression methodCallExpression(AST methodCallNode) {
30573062
setTypeArgumentsOnMethodCallExpression(expression, typeArgumentList);
30583063
configureAST(expression, methodCallNode);
30593064
return expression;
3060-
} else if (!implicitThis || isType(DYNAMIC_MEMBER, selector) || isType(IDENT, selector) ||
3061-
isType(STRING_CONSTRUCTOR, selector) || isType(STRING_LITERAL, selector)) {
3065+
} else if (!implicitThis || isType(DYNAMIC_MEMBER, selector) || isType(IDENT, selector) || isType(STRING_CONSTRUCTOR, selector) || isType(STRING_LITERAL, selector)) {
30623066
name = expression(selector, true);
30633067
} else {
30643068
implicitThis = false;

0 commit comments

Comments
 (0)