You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There seems to be an undesired behavior when using struct refs inside of a foreach loop.
When modifying byref fields of a struct within a foreach loop increment and similar operations do not work as expected.
I am not sure what exactly is causing this issue, but I also tested this with local variables and there it works as expected. I assume there is sth strange going on during the foreach.
Description
Copied from the dotnet repo as suggested
dotnet/runtime#102665
There seems to be an undesired behavior when using struct refs inside of a foreach loop.
When modifying byref fields of a struct within a foreach loop increment and similar operations do not work as expected.
I am not sure what exactly is causing this issue, but I also tested this with local variables and there it works as expected. I assume there is sth strange going on during the foreach.
Reproduction Steps
Consider this example project:
RefTest.zip
Code:
Output:
Expected behavior
The PostIncrement loop and the ExpandedIncrement loop should do the same.
Actual behavior
PostIncrement does not work as expected, but the ExpandedIncrement does
Regression?
Can't test this right now.
Known Workarounds
The ExpandedIncrement version works as expected but this could definetly cause errors down the line when someone misuses this slightly
Configuration
Version: .Net 9.0, .Net 8.0
OS: Windows
Architecture: AnyCpu
I don't have other devices to test this code unfortunately
Other information
I think it might be related to the foreach loop. Also the IL code of both operations is different. The PostIncrement version uses:
While the ExpandedIncrement version uses this:
The lowered version of this code also works correctly btw:
And this version works as well
The text was updated successfully, but these errors were encountered: