Skip to content

CS8751 when using ref struct System.Index #52724

@bysdxt

Description

@bysdxt

Version Used:
.net 5.0.202
Steps to Reproduce:

  1. new console app
  2. 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!");
        }
    }
}
  1. 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

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions