Summary: | [compiler] Misleading compiler error on single line if statement | ||
---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | Will Horn <will.horn> |
Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
Status: | NEW --- | QA Contact: | |
Severity: | enhancement | ||
Priority: | P3 | CC: | jarthana, Olivier_Thomann, sknuijver, stephan.herrmann |
Version: | 3.3 | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | Windows XP | ||
Whiteboard: |
Description
Will Horn
2007-06-05 14:36:32 EDT
I consider this as an enhancement. Not a bug since the code is invalid and we reject it. class Test { public void test() { String string; if (true) string = new String(); } } does compile without issue. Is String string = new String(); shorthand for two statements? *** Bug 485275 has been marked as a duplicate of this bug. *** (In reply to Sknu ijver from comment #3) > *** Bug 485275 has been marked as a duplicate of this bug. *** public class X { public void test() { if (true) String string = new String(); } } With that code, javac reports: X.java:4: error: variable declaration not allowed here String string = new String(); ^ 1 error This is not different from your example. Why do you believe this is valid java? Could you please point me to where you found out this is valid? Thanks. As far as I know, variable declaration statements are not valid statements as an if then clause. Stephan, anything to add? class Test { public void test() { String string; if (true) string = new String(); } } is completely different. string = new String(); is an assignment which is part of an expression statement which is perfectly fine as an if then clause. This should be closed as invalid. The only way to get a better message would be to modify the grammar to consume variable declaration statement as part of an if then clause and reject it then. Stephan, Jay, I let you take care of this. Thx. Thanks Olivier, Yep, see http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.2 A LocalVariableDeclarationStatement is a BlockStatement and hence can only occur in a Block. The then branch of an if-then statement (14.9.1) is a Statement. Productions for Statement include Assignment (via StatementWithoutTrailingSubstatement -> ExpressionStatement -> StatementExpression -> Assignment), but no direct option for LocalVariableDeclarationStatement. Local vars are legal in a then statement only if enclosed in a Block. Besides: if you were allowed to create a local variable in a then statement, the scope of the variable would be only its own declaring statement => no code location would be able to read the variable. Not very useful :) Grammar modification is also the first I could think of for reporting a more useful error message. If that works without creating LALR(1) conflicts, I'd agree to that, but if further tweaking of the grammar is required, I don't see this worth the effort, honestly. That was my point. This bug here initially got a short reply from Olivier Thomann, where he said the code was syntactically wrong but there could be an enhancement. I had just filed a similar bug when I realized that this was a compound / block statement. I just added a comment in addition to Olivier's response. |