Bug 325229 - [compiler] eclipse compiler differs from javac when assert is present (FUP of bug 319510)
Summary: [compiler] eclipse compiler differs from javac when assert is present (FUP o...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M2   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-14 07:26 EDT by Satyam Kandula CLA
Modified: 2010-09-21 17:56 EDT (History)
2 users (show)

See Also:
Olivier_Thomann: review+


Attachments
patch under test (9.48 KB, patch)
2010-09-15 07:02 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Satyam Kandula CLA 2010-09-14 07:26:08 EDT
Run the following program built with javac and with eclipse compiler (don't use the assert option). With javac, it prints false but with eclipse it prints true.  
########
public class Test {
	public boolean assertTest(Object foo) {
	        assert foo instanceof String;
	        if (foo != null) {
	            return true;
	        } else {
	            return false;
	        }
	    }
	public static void main(String[] args) {
		System.out.println(new Test().assertTest(null));
	}
	
}
###########
Comment 1 Olivier Thomann CLA 2010-09-14 07:41:17 EDT
Ayushman, I think considering the side effect in the code generation, we should not make any assumption that the assert statement expression should be true for the null check.
Optimizing the code generation based on this result can lead to false runtime behavior when assertion is disabled at runtime (the default).
Comment 2 Ayushman Jain CLA 2010-09-14 09:23:23 EDT
(In reply to comment #1)
> Ayushman, I think considering the side effect in the code generation, we should
> not make any assumption that the assert statement expression should be true for
> the null check.
> Optimizing the code generation based on this result can lead to false runtime
> behavior when assertion is disabled at runtime (the default).

AFAIK, the fix for bug 303448 should've taken care of this. Perhaps some change we did recently has made this problem to resurface. I'll investigate.
Comment 3 Ayushman Jain CLA 2010-09-14 10:14:10 EDT
Ok so i think I know whats wrong here.
Bug 303448 only fixed the case for EqualExpression, which as this case shows, is clearly not sufficient. We need to extend this fix for other kinds of ASTNodes that can occur inside an assert expression. I tried doing so for instanceof expression and could easily fix the case.

I dont really want to touch the existing behaviour with asserts ( that is, treating the assert expression as always true to evaluate downstream code), since I dont know what else it might break. Olivier, what do you think?
Comment 4 Olivier Thomann CLA 2010-09-14 10:31:53 EDT
(In reply to comment #3)
> Ok so i think I know whats wrong here.
> Bug 303448 only fixed the case for EqualExpression, which as this case shows,
> is clearly not sufficient. We need to extend this fix for other kinds of
> ASTNodes that can occur inside an assert expression. I tried doing so for
> instanceof expression and could easily fix the case.
> 
> I dont really want to touch the existing behaviour with asserts ( that is,
> treating the assert expression as always true to evaluate downstream code),
> since I dont know what else it might break. Olivier, what do you think?
As long as the code generation doesn't rely on the assert result to be optimized, I think this is fine. We cannot assume the assert to always be true to optimize the code generation.
With all the cases we have with assert statement, I would be tempted to add an extra option to be able to disable the null analysis for assert statement. So we can make sure that we properly report unnecessary null checks for the users who want it and this could be disabled for the ones that don't like it.

So please provide a fix for all expressions and if everything is fine, we might contribute it for M2. Having wrong code generation is evil as problems cannot be seen before runtime. Do you think we can target M2 ?

I'd like to get Srikanth's thoughts on this.
Comment 5 Ayushman Jain CLA 2010-09-15 07:02:13 EDT
Created attachment 178912 [details]
patch under test

Here's a patch that inserts the fix for 303448 at missing places. All NullReferenceTests pass with it.

We should add the new option in a separate bug.
Comment 6 Olivier Thomann CLA 2010-09-15 08:16:51 EDT
(In reply to comment #5)
> We should add the new option in a separate bug.
Definitely. Opened bug 325342.
Comment 7 Olivier Thomann CLA 2010-09-15 10:17:52 EDT
Targetting M2 as the code generation is wrong.
Comment 8 Ayushman Jain CLA 2010-09-15 11:33:31 EDT
All tests pass, Olivier please review. TIA
Comment 9 Olivier Thomann CLA 2010-09-15 11:35:08 EDT
Patch looks good.
Comment 10 Ayushman Jain CLA 2010-09-15 12:11:16 EDT
Released in HEAD for 3.7M2.
Comment 11 Olivier Thomann CLA 2010-09-21 17:56:25 EDT
Verified using I20100921-1024