-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some edge case in foreach transform #1869
Comments
Sorry, but could you please describe the problems in more detail? Maybe it's easier to understand if you split the problems into 3 different pieces of code and show the input, expected output and current output. Thank you very much! |
Case 1Inputstatic void Method1()
{
//shouldn't create another new temp var for foreach
var array = new int[5];
foreach(int value in array)//it will use an new temp var copy from `array` if there is other usage of `array`
{
//array = null;//<--it will use the orig var instead of temp var
//Dup usage of `value` to avoid inline
Console.WriteLine(value);
Console.WriteLine(value);
}
//roslyn will strip the usage of temp var in release mode if `array` is not used in other place
Console.WriteLine(array);
} ExpectedFor Currentprivate static void Method1()
{
int[] array = new int[5];
int[] array2 = array;
foreach (int value in array2)
{
//array = null;
Console.WriteLine(value);
Console.WriteLine(value);
}
Console.WriteLine(array);
} Case 2Inputstatic void Method2()
{
var array = new int[5];
for (int i = 0; i < array.Length; i++)
{
var value = array[i];
//if the loop var is writen(or access by ref) during loop, it shouldn't be an foreach
//(modify `array` in really foreach will copy and use an new temp var as loop var, see Method1)
array = null;
Console.WriteLine(value);
Console.WriteLine(value);
}
}
static void Method2(ref int[] array)
{
for (int i = 0; i < array.Length; i++)
{
var cur = array[i];
Console.WriteLine(cur);
Console.WriteLine(cur);
}
} ExpectedFor Currentprivate static void Method2()
{
int[] array = new int[5];
foreach (int cur in array)
{
array = null;
Console.WriteLine(cur);
Console.WriteLine(cur);
}
}
private static void Method2(ref int[] array)
{
foreach (int value in array)
{
Console.WriteLine(value);
Console.WriteLine(value);
}
} Case 3Input(Debug only)//Debug only
static void Method3()
{
var array = new List<int>();
using(List<int>.Enumerator enumerator = array.GetEnumerator())
{
while (enumerator.MoveNext())
{
int value = enumerator.Current;
Console.WriteLine(value);
Console.WriteLine(value);
value++;//<--shouldn'd reuse the same var name as the name of loop var here (DEBUG only)
Console.WriteLine(value);
Console.WriteLine(value);
}
}
} ExpectedOnly happen for roslyn+debug, and not for array(only for normal enumerator). CurrentSee the var name conflict for
|
Tested in master
Compile the below code with roslyn and Debug
See the 3
//<--
in the below code.Also for Span, with the same mode as ForeachArray does.
ForeachArrayA:
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQAwAIpwCwG5nJQDMmM6AwugN7Lp2YlTboBiA9gE4CmAhgMYALAIIcOPAJ4AKAJS16NJPSXoAlgDsALgG0Auuh6iJ6ALzo1XAO6rNWuKlQ78i5XQD0rrAE5JBseOlOLnRyLgBmnLyC6JLqGuh8AK4c1vqG/iHKCkHK7r5GpmoJADZFgdn0XpKJHAEZ2ZXVtc4uAL519OEcMZqqJuiouL0APKl+AHQAMlxqAOYaAoNgYCqyzZntLgBuBvFJZKZ54loqjhvKh32FJWXlmHDe1TBNt26uj30D7kMAtN9CRdweMBxOhQioAB5cYApAC2PAAzhouMk2KF0ABJCYAZQADuIzkoGntnkE2mslLk0pdiqUzpVDiS6GSWkA=
The text was updated successfully, but these errors were encountered: