Christopher said:
Is the following syntactically valid?
public class Foo {
public static void main(String[] args) {
for( foo: ; ; ); // Note statement label
}
}
javac is rejecting this, but IntelliJ is saying that it is valid. I
would appreciate a reference to the JLS if possible, so I can file a
bug report either with Sun or with JetBrains.
I believe it is not syntactically valid.
The remainder of this message is of interest only to extreme language
lawyers. I'm going to show the available productions, and the JLS
sections where I found them. This is based on the third editions,
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
First note that "for" is a reserved word, and as far as I can tell
appears only in for statement productions in 14.14.
The statement cannot be a EnhancedForStatement because it requires a ":"
followed by an expression. 14.14.2
Now consider it as a BasicForStatement, 14.14.1. In each production, the
portion between the "for(" and the first of the required three
semicolons is an optional ForInit. The question becomes whether "foo:"
can be a ForInit.
ForInit:
StatementExpressionList
LocalVariableDeclaration
Can "foo:" be a StatementExpressionList?
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression (14.14.1)
Since "foo:" does not contain a ",", it would have to be a single
StatementExpression.
In 14.8:
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
"foo:" does not match any of these.
Now consider the LocalVariableDeclaration case. In 14.4:
LocalVariableDeclaration:
VariableModifiers Type VariableDeclarators
In 8.4.1:
VariableModifiers:
VariableModifier
VariableModifiers VariableModifier
VariableModifier: one of
final Annotation
so "foo:" does not match VariableModifiers.
"foo" could be a type, but then ":" would have to match VariableDeclarators.
In 8.3:
VariableDeclarators:
VariableDeclarator
VariableDeclarators , VariableDeclarator
VariableDeclarator:
VariableDeclaratorId
VariableDeclaratorId = VariableInitializer
VariableDeclaratorId:
Identifier
VariableDeclaratorId [ ]
All cases of VariableDeclarators begin with an identifier, and ":" is
not a valid identifier.
Patricia