Bug 80211 - Bug in Java Parser
Summary: Bug in Java Parser
Status: CLOSED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.1 M4   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-12-04 16:39 EST by Marcin Brodziak CLA
Modified: 2004-12-05 05:57 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marcin Brodziak CLA 2004-12-04 16:39:23 EST
Consider that code: 
String a,b;
do{
   a="Hello ";
}while(a!=null);
		
if(a!=null)
{
   b="World!";
}
System.out.println(a+b);

obviously a!=null and the first loop terminates. Because conditional statement
in if-clause is true, b and c are set to "Hello " and "World!" respectively.
Parser recognizes, that variable b may not have been initialized. Of course this
code is rubbish and noone will ever write something like that, but shouldn't
parser allow a programmer to compile a correct piece of code?
Comment 1 Olivier Thomann CLA 2004-12-04 16:47:38 EST
the if condition is not a constant, therefore it is possible that the then
clause is not executed. So b might not be initialized.

This is perfectly fine. You can notice that javac and jikes are reporting the
same problem.
Ok to close?
Comment 2 Philipe Mulet CLA 2004-12-04 19:14:54 EST
Closing. The compiler is implementing the definite assignment rules strictly.
Rather blame the language specification.
Comment 3 Marcin Brodziak CLA 2004-12-04 19:50:31 EST
I suppose you're right. Eclipse works in the same way as the compiler, so we
cannot say, that this is as a bug. Although I still think, that it's silly. 
Comment 4 Philipe Mulet CLA 2004-12-05 05:54:56 EST
Conservative flow analysis is quite restrictive indeed.
Also note that the null check 'if (a!=null)' is useless, and removing it will
make your problem go away.

FYI, we are working in adding an optional warning to our compiler to diagnose
such unnecessary null checks.
Comment 5 Philipe Mulet CLA 2004-12-05 05:57:03 EST
You'll get something like:

----------
1. WARNING in d:\src\X.java (at line 9)
	if(a!=null)
	   ^
The variable a cannot be null; it was either set to a non-null value or assumed
to be non-null when last used
----------