using System.Threading.Tasks;
interface I1
{
int this[int i] {get; set;}
}
struct S1 : I1
{
public int F1;
public int this[int i]
{
get
{
System.Console.Write(F1);
return 0;
}
set
{
System.Console.Write(F1);
}
}
}
class Program
{
static async Task Main()
{
await Test3<S1>();
}
static T GetT<T>() where T : I1 => (T)(object)new S1 { F1 = 123 };
static async Task Test3<T>() where T : I1
{
GetT<T>()[0] += await Get1Async();
}
static async Task<int> Get1Async()
{
await Task.Yield();
return 1;
}
}
Observed:
// /Program.cs(37,8): error CS8178: A reference returned by a call to 'Program.GetT<T>()' cannot be preserved across 'await' or 'yield' boundary.
// GetT<T>()[0] += await Get1Async();
Diagnostic(ErrorCode.ERR_RefReturningCallAndAwait, "GetT<T>()").WithArguments("Program.GetT<T>()").WithLocation(37, 8)
Program.GetT<T> doesn't return a reference. Async rewriter should have no problem capturing the value.