Bug 39739 - VariableDeclarationStatements aren't full statements
Summary: VariableDeclarationStatements aren't full statements
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.0 M5   Edit
Assignee: Jim des Rivieres CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-08 06:37 EDT by Dirk Baeumer CLA
Modified: 2003-11-24 07:06 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dirk Baeumer CLA 2003-07-08 06:37:28 EDT
I20030703++

Example:

public void foo() {
  if (true)
    int i= 10;
}

procduces a compile error since a declaration must occur inside a block. 
Should this be documented in the AST world ?
Comment 1 Philipe Mulet CLA 2003-07-08 06:53:40 EDT
I think the DOM AST tells somewhere that it accepts a superset of the grammar, 
but I agree with you that this scenario should be addressed.

Some more validation check should occur when constructing such an IF statement 
(same applies to other controls).

Comment 2 Olivier Thomann CLA 2003-07-08 09:45:15 EDT
DOM/AST must reflect the source. It doesn't mean that there is no compile
errors. The DOM/AST tree simply reflects the source code. If the source code is
incorrect, then the generated code can lead to compile errors. The construction
of a tree simply tries to garantee that it remains a tree.
It is easily possible to build a tree using methods on the
org.eclipse.jdt.dom.AST class that won't compile.
It is up to the user to know what he/she is doing.
Comment 3 Philipe Mulet CLA 2003-07-08 10:17:01 EDT
Per construction, the DOM ast still prevents obvious errors to occur. For 
instance, you cannot attach a type declaration as a statement of an IF, though 
based on your previous comment, it could have been legite.

Either it should be documented, or checked more strictly... either one is ok. 
The fact we got a PR means that there is some misunderstanding.
Comment 4 Dirk Baeumer CLA 2003-07-08 10:25:37 EDT
I am only asking for some documentation although one can argue that the 
language spec already documents it. 

More strikt checking would lead to "incomplete" AST since we wouldn't get an 
AST for the provided example. And this isn't something I would expect to 
happen.
Comment 5 Olivier Thomann CLA 2003-07-08 10:33:25 EDT
DOM/AST ensures that you add the right "type" on nodes in each collection. For 
example, you cannot add an expression as a child of a block. It is specified to 
be a statement. For the same reason, you cannot add a type declaration inside a 
block. 
So I don't see what we could add. Each type of children is documented. My 
previous comment was based on the fact that you try to put a statement in the 
then clause of an if statement. We don't check anything else than the fact that 
it is a statement. A local variable declaration statement is a statement. 
Therefore it is legite inside an if statement even if the code won't compile.
Comment 6 Olivier Thomann CLA 2003-07-08 12:00:52 EDT
Jim, could you please clarify the doc in this case? We don't need to add more
checks in the code. Simply add a note to clarify the fact that the DOM/AST can
handle a superset of the Java grammar.
Comment 7 Olivier Thomann CLA 2003-07-08 12:01:17 EDT
Add CC
Comment 8 Jim des Rivieres CLA 2003-11-14 22:04:16 EST
Added boilerplate to Ifstatement.setThenStatement
 * Special note: The Java language does not allow a local variable declaration
 * to appear as the "then" part of an if statement (they may only appear 
within a
 * block). However, the AST will allow a 
<code>VariableDeclarationStatement</code>
 * as the thenStatement of a <code>IfStatement</code>. To get something that 
will
 * compile, be sure to embed the <code>VariableDeclarationStatement</code>
 * inside a <code>Block</code>.

Also in the other places with a similar gotcha:
Ifstatement.setElseStatement
Whilestatement.setBody
DoStatement.setBody
ForStatement.setBody
LabeledStatement.setBody

Fixed in builds > N20031114. Updated release notes.
Comment 9 David Audel CLA 2003-11-24 07:06:51 EST
Verified.