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
I tried running roslyn-linq-rewrite (built from source) on a big solution and found some issues. There's an issue with expressions capturing the foreach loop variable when it is a struct type (eg. when iterating over an IDictionary<K,V>, the loop variable is of type KeyValuePair<K,V> which is a struct)
I made a minimal code snippet reproducing the issue:
public void Bugged(IDictionary<string, string> dict, IList<string> list) {
foreach (KeyValuePair<string, string> kv in dict) {
if (list.Any(item => item == kv.Value)) {
list.Remove(kv.Value);
}
}
}
This gets rewritten to
public void Bugged(IDictionary<string, string> dict, IList<string> list)
{
foreach (KeyValuePair<string, string> kv in dict)
{
if (Bugged_ProceduralLinq1(list, ref kv))
{
list.Remove(kv.Value);
}
}
}
bool Bugged_ProceduralLinq1(System.Collections.Generic.IList<string> _linqitems, ref System.Collections.Generic.KeyValuePair<string, string> kv)
{
if (_linqitems == null)
throw new System.ArgumentNullException();
foreach (var _linqitem in _linqitems)
{
if (_linqitem == kv.Value)
{
return true;
}
}
return false;
}
Which produces a compilation error (CS1567)
The text was updated successfully, but these errors were encountered:
The problem is, that a foreach variable is "readonly". The subroutine is not allowed to change the struct.
So we must submit the reference via readonly ref which is called "in" now in CSharp 7.
I tried running roslyn-linq-rewrite (built from source) on a big solution and found some issues. There's an issue with expressions capturing the foreach loop variable when it is a struct type (eg. when iterating over an IDictionary<K,V>, the loop variable is of type KeyValuePair<K,V> which is a struct)
I made a minimal code snippet reproducing the issue:
This gets rewritten to
Which produces a compilation error (CS1567)
The text was updated successfully, but these errors were encountered: