Version Used:
.net 5.0.202
Steps to Reproduce:
- new console app
- code:
#pragma warning disable CS0436
using System;
namespace System {
public readonly ref struct Index {
public Index(int value, bool fromEnd = false) {
}
}
}
namespace ConsoleApp1 {
interface I<T> {
ref readonly T this[Index index] { get; }
}
class Program {
static ref readonly T f<T>(I<T> i) {
return ref i[new Index(1, true)];
}
static ref readonly T h<T>(I<T> i) {
return ref i[^1];
}
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
- dotnet build
Expected Behavior:
Build succeeded
Actual Behavior:
Build FAILED.
Program.cs(18,26): error CS8751: Internal error in the C# compiler.
Program.cs(18,24): error CS8347: Cannot use a result of 'I.this[Index]' in this context because it may expose variables referenced by parameter 'index' outside of their declaration scope
It is ok that uses public readonly struct Index instead of public readonly ref struct Index
Program.zip
Version Used:
.net 5.0.202
Steps to Reproduce:
Expected Behavior:
Build succeeded
Actual Behavior:
Build FAILED.
Program.cs(18,26): error CS8751: Internal error in the C# compiler.
Program.cs(18,24): error CS8347: Cannot use a result of 'I.this[Index]' in this context because it may expose variables referenced by parameter 'index' outside of their declaration scope
It is ok that uses
public readonly struct Indexinstead ofpublic readonly ref struct IndexProgram.zip