Skip to content

Commit da38d0e

Browse files
srikanth-sankaraniloveeclipse
authored andcommitted
Compilation error not raised with ECJ when warnings are raised
* Fixes #4750
1 parent a3826aa commit da38d0e

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,15 @@ public void generateCode(ClassScope classScope, ClassFile classFile) {
321321
}
322322
int problemResetPC = 0;
323323
CompilationResult unitResult = null;
324+
CategorizedProblem problems[] = new CategorizedProblem[0];
324325
int problemCount = 0;
325326
if (classScope != null) {
326327
TypeDeclaration referenceContext = classScope.referenceContext;
327328
if (referenceContext != null) {
328329
unitResult = referenceContext.compilationResult();
329330
problemCount = unitResult.problemCount;
331+
if (problemCount > 0)
332+
System.arraycopy(unitResult.problems, 0, (problems = new CategorizedProblem[problemCount]), 0, problemCount);
330333
}
331334
}
332335
boolean restart = false;
@@ -347,6 +350,7 @@ public void generateCode(ClassScope classScope, ClassFile classFile) {
347350
// reset the problem count to prevent reporting the same warning twice
348351
if (unitResult != null) {
349352
unitResult.problemCount = problemCount;
353+
unitResult.problems = problems;
350354
}
351355
restart = true;
352356
} else if (e.compilationResult == CodeStream.RESTART_CODE_GEN_FOR_UNUSED_LOCALS_MODE) {
@@ -356,6 +360,7 @@ public void generateCode(ClassScope classScope, ClassFile classFile) {
356360
// reset the problem count to prevent reporting the same warning twice
357361
if (unitResult != null) {
358362
unitResult.problemCount = problemCount;
363+
unitResult.problems = problems;
359364
}
360365
restart = true;
361366
} else {
@@ -367,8 +372,7 @@ public void generateCode(ClassScope classScope, ClassFile classFile) {
367372
// produce a problem method accounting for this fatal error
368373
if (abort) {
369374
int problemsLength;
370-
CategorizedProblem[] problems =
371-
this.scope.referenceCompilationUnit().compilationResult.getAllProblems();
375+
problems = this.scope.referenceCompilationUnit().compilationResult.getAllProblems();
372376
CategorizedProblem[] problemsCopy = new CategorizedProblem[problemsLength = problems.length];
373377
System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
374378
classFile.addProblemMethod(this, this.binding, problemsCopy, problemResetPC);

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13358,4 +13358,86 @@ public void testBug550255() {
1335813358
+ "1 problem (1 warning)\n",
1335913359
true);
1336013360
}
13361+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4750
13362+
// Compilation error not raised with ECJ when warnings are raised
13363+
public void testIssue4750() {
13364+
this.runNegativeTest(
13365+
new String[] {
13366+
"X.java",
13367+
"""
13368+
public class X {
13369+
public void testIssue() throws Exception {
13370+
int N_ITERATIONS = 10;
13371+
while (N_ITERATIONS-- > 0) {
13372+
Object index = 0;
13373+
Runnable thread = new Runnable() {
13374+
@Override
13375+
public void run() {
13376+
System.out.println(index + 1);
13377+
}
13378+
};
13379+
}
13380+
}
13381+
}
13382+
""",
13383+
},
13384+
"\"" + OUTPUT_DIR + File.separator + "X.java\""
13385+
+ " -sourcepath \"" + OUTPUT_DIR + "\""
13386+
+ " -d \"" + OUTPUT_DIR + "\"",
13387+
"",
13388+
"----------\n" +
13389+
"1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 5)\n" +
13390+
" Object index = 0;\n" +
13391+
" ^^^^^\n" +
13392+
"The value of the local variable index is not used\n" +
13393+
"----------\n" +
13394+
"2. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 6)\n" +
13395+
" Runnable thread = new Runnable() {\n" +
13396+
" ^^^^^^\n" +
13397+
"The value of the local variable thread is not used\n" +
13398+
"----------\n" +
13399+
"3. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 9)\n" +
13400+
" System.out.println(index + 1);\n" +
13401+
" ^^^^^^^^^\n" +
13402+
"The operator + is undefined for the argument type(s) Object, int\n" +
13403+
"----------\n" +
13404+
"3 problems (1 error, 2 warnings)\n",
13405+
true);
13406+
}
13407+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4750
13408+
// Compilation error not raised with ECJ when warnings are raised
13409+
public void testIssue4750_nowarn() {
13410+
this.runNegativeTest(
13411+
new String[] {
13412+
"X.java",
13413+
"""
13414+
public class X {
13415+
public void testIssue() throws Exception {
13416+
int N_ITERATIONS = 10;
13417+
while (N_ITERATIONS-- > 0) {
13418+
Object index = 0;
13419+
Runnable thread = new Runnable() {
13420+
@Override
13421+
public void run() {
13422+
System.out.println(index + 1);
13423+
}
13424+
};
13425+
}
13426+
}
13427+
}
13428+
""",
13429+
},
13430+
"\"" + OUTPUT_DIR + File.separator + "X.java\""
13431+
+ " -sourcepath \"" + OUTPUT_DIR + "\""
13432+
+ " -nowarn -d \"" + OUTPUT_DIR + "\"",
13433+
"",
13434+
"----------\n" +
13435+
"1. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 9)\n" +
13436+
" System.out.println(index + 1);\n" +
13437+
" ^^^^^^^^^\n" +
13438+
"The operator + is undefined for the argument type(s) Object, int\n" +
13439+
"----------\n" +
13440+
"1 problem (1 error)\n",
13441+
true);
13442+
}
1336113443
}

0 commit comments

Comments
 (0)