Calls to extension properties for `readonly struct` treat setters as mutating the foreach loop variable
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
**Version Used**: 10.0.102
**Steps to Reproduce**:
1. Attempt to compile the following listing:
```cs
using System;
using System.Collections.Generic;
public class Api
{
public int GetSomeProper(int index)
=> throw new NotImplementedException();
public void SetSomeProper(int index, int value)
=> throw new NotImplementedException();
}
public readonly struct Blah(Api api, int index)
{
public readonly Api Api = api;
public readonly int Index = index;
}
public static class BlahExtensions
{
extension(in Blah blah) // Please note that `in` param doesn't make a difference
{
public int SomeProp
{
get => blah.Api.GetSomeProper(blah.Index);
set => blah.Api.SetSomeProper(blah.Index, value);
}
}
}
public static class Program
{
public static void Main()
{
var api = new Api();
var lst = new List();
lst.Add(new Blah(api, 1));
lst.Add(new Blah(api, 2));
foreach (var b in lst)
{
b.SomeProp = 123; // doesn't compile - cannot modify the loop variable inside foreach
BlahExtensions.set_SomeProp(b, 123); // Compiles
}
}
}
```
2. Receive error message:
```
Program.cs(42,13): Error CS1654 : Cannot modify members of 'b' because it is a 'foreach iteration variable'
```
**Diagnostic Id**: CS1654
**Expected Behavior**: Listing compiles
**Actual Behavior**: Doesn't compile, reporting the CS1654 compile time error.
Contributor guide
Assessment
This issue has not been assessed yet.