Skip to content

Commit

Permalink
[23] ECJ Crashes with CCE #2782 (#2785)
Browse files Browse the repository at this point in the history
+ do expect ExplicitConstructorCall even in regular method

Fixes #2782
  • Loading branch information
stephan-herrmann authored Aug 2, 2024
1 parent 51dff48 commit 7c62f67
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ public void resolve(BlockScope scope) {
return;
}
boolean hasError = false;
ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
if (methodDeclaration == null || !methodDeclaration.isConstructor()) {
hasError = true;
} else {
// is it the first constructor call?
ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
ExplicitConstructorCall constructorCall = constructorDeclaration.constructorCall;
if (constructorCall == null) {
constructorCall = constructorDeclaration.getLateConstructorCall(); // JEP 482
Expand All @@ -339,7 +339,7 @@ public void resolve(BlockScope scope) {
if (hasError) {
if (!(methodDeclaration instanceof CompactConstructorDeclaration)) {// already flagged for CCD
if (JavaFeature.FLEXIBLE_CONSTRUCTOR_BODIES.isSupported(scope.compilerOptions())) {
boolean isTopLevel = Arrays.stream(constructorDeclaration.statements).anyMatch(this::equals);
boolean isTopLevel = Arrays.stream(methodDeclaration.statements).anyMatch(this::equals);
if (isTopLevel)
scope.problemReporter().duplicateExplicitConstructorCall(this);
else // otherwise it's illegally nested in some control structure:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IProblemRequestor;
Expand Down Expand Up @@ -243,4 +244,46 @@ public static void main(String argv[]) {
deleteProject(p);
}
}
public void testGH2782() throws CoreException {
IJavaProject p = createJava21Project("p");
try {
createFolder("p/src/test");
createFile("p/src/test/StringVar2.java",
"""
package test;
public class StringVar2 {
public void test () {
"foo".
if (true);
}
}
""");
p.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
IMarker[] markers = p.getProject().findMarkers(null, true, IResource.DEPTH_INFINITE);
sortMarkers(markers);
assertMarkers("markers in p",
"""
Constructor call must be the first statement in a constructor
Syntax error on token "if", super expected""",
markers);

ICompilationUnit unit = getCompilationUnit("p/src/test/StringVar2.java");
this.problemRequestor.initialize(unit.getSource().toCharArray());
this.workingCopy = getCompilationUnit("p/src/test/StringVar2.java").getWorkingCopy(this.wcOwner, null);
assertProblems("Expecting no problems",
"""
----------
1. ERROR in /p/src/test/StringVar2.java (at line 5)
if (true);
^^
Syntax error on token "if", super expected
----------
""",
this.problemRequestor);
this.workingCopy.discardWorkingCopy();
} finally {
deleteProject(p);
}
}

}

0 comments on commit 7c62f67

Please sign in to comment.