Community
Participate
Working Groups
The following code should complain that i may have not been initialized after the if statement. The recent fixes around null check analysis improvement broke this as it considers that i is definitely assigned after the if statement. public class X { void foo() { Object o = null; int i; if (o == null) // redundant check i = 0; System.out.println(i); } } This should be a negative test. Right now we compile without reporting any error or we report a redundant null check if the null warning is enabled. We should also make sure that the try/catch/finally statement is properly handled.
Created attachment 158761 [details] proposed fix + regression test The problem is: with our new analysis, we are sure that the comparison o==null is redundant and that the if statement will always be executed, as a result of which we concluded that i will always be initialised. But we dont want to be concluding the initializations so aggressively. Attaching a patch which takes care of this, although it changes the null analysis code a bit. Now we do set the unreachable bit, but we dont set optimised constants. Two bits in ASTNode, Bit8 and 9, take care of code gen according to whether unreachable bit is set or not, and also report dead code warnings at appropriate places. A new method FlowInfo#mergedOptimisedBranchesIfElse() is introduced in addition to the old FlowInfo#mergedOptimisedBranches(). This new method takes care of the initializations inside if else constructs where one branch is sure to be executed.
Created attachment 158805 [details] Proposed fix Same patch with minor changes.
Released for 3.6M6. Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest. Regression tests will be released tomorrow so that the nightly build doesn't fail.
Released tests: org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#test0569_try_catch org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#test0570_try_catch org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#test0571_try_catch_finally org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#test0572_if_statement
Verified for 3.6M6 using I20100307-2000.