I
Immo Landwerth
Hi there,
I am working at a project where parsing C# is a major part.
While constructing an AST I was wondering about the syntax of the
for-loop statement.
If I write
for (int i = 0; i < 10; i)
^
|
--------------------------+
the compiler aborts compilation and says:
CS0201: Only assignment, call, increment, decrement, and new object
expressions can be used as a statement.
This happens also when I do that in the loop initializer
int i = 0;
for (i; i < 3; i++)
^
|
-------+
{
}
But if I use an expression list instead of a simple expression
int i = 0;
for (1, 2, 3; i < 3; i++)
{
}
-- or --
for (int i = 0; i < 3; 1, 2, 3)
{
}
the source becomes compiled just fine. I would have expected the
compiler to complain about the expression list "1, 2, 3" with CS0201
because the initialization/incrementation of a for-loop should be a
expression or a expression list where all expressions can be used as
statements.
Is this a known/unknown issue or is there a good reason to allow any
expression list in the loop initializer/incrementer?
I am working at a project where parsing C# is a major part.
While constructing an AST I was wondering about the syntax of the
for-loop statement.
If I write
for (int i = 0; i < 10; i)
^
|
--------------------------+
the compiler aborts compilation and says:
CS0201: Only assignment, call, increment, decrement, and new object
expressions can be used as a statement.
This happens also when I do that in the loop initializer
int i = 0;
for (i; i < 3; i++)
^
|
-------+
{
}
But if I use an expression list instead of a simple expression
int i = 0;
for (1, 2, 3; i < 3; i++)
{
}
-- or --
for (int i = 0; i < 3; 1, 2, 3)
{
}
the source becomes compiled just fine. I would have expected the
compiler to complain about the expression list "1, 2, 3" with CS0201
because the initialization/incrementation of a for-loop should be a
expression or a expression list where all expressions can be used as
statements.
Is this a known/unknown issue or is there a good reason to allow any
expression list in the loop initializer/incrementer?