Skip to content

for loop with multiple uninitialized variables no longer compiles #77160

@jonsagara

Description

@jonsagara

Version Used: .NET SDK 9.0.200

Steps to Reproduce:

  1. Clone https://github.com/jonsagara/ForLoopCompileTest
  2. Open the solution
  3. Build -> Rebuild Solution

In a nutshell, a for loop with two or more uninitialized variables breaks compilation:

for (int offset = 0, c1, c2; offset < length;)
{
    // snipped
}

But a single uninitialized loop variable compiles:

for (int offset = 0, c1; offset < length;)
{
    // snipped
}

Expected Behavior:

This loop is extracted from a file that has been part of our solution since 2016. The for loop itself has never changed. I expect it to still compile.

Actual Behavior:

You get compilation errors. From the solution listed above, you get compiler errors like these:

; expected
The name 'c1' does not exist in the current context
; expected
The name 'c2' does not exist in the current context
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Syntax error, ',' expected
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Syntax error, ',' expected
Invalid expression term ')'
; expected
The name 'c1' does not exist in the current context
; expected
The name 'c2' does not exist in the current context
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
The name 'c3' does not exist in the current context
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Syntax error, ',' expected
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Syntax error, ',' expected
Invalid expression term ')'

Environment

  • Visual Studio 2022 17.13
  • Visual Studio 2022 17.14.0 Preview 1.0
  • .NET SDK 9.0.200
  • Windows 11 Version 24H2 (OS Build 26100.3194)

JetBrains Rider

The solution also fails to compile in JetBrains Rider 2024.3.5.

Previous reports

I reported this to VS Feedback when I first encountered it.

Metadata

Metadata

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions