Description
Background and motivation
C# allows an implicit conversion of InlineArray to Span. To perform this implicit conversion, Roslyn emits calls of several unsafe methods today (Unsafe.As
, MemoryMarshal.CreateSpan
).
In hindsight, we have realized that allowing Roslyn to produce unverifiable IL for safe C# is very slippery slope. Instead, we would like safe C# constructs to always produce IL that is verifiable using verification rules written in the same spirit as the existing ECMA-335 verification rules.
The proposed API enables Roslyn to perform the InlineArray to Span conversion without use of unsafe or unverifiable IL.
API Proposal
namespace System.Runtime.CompilerServices;
public class RuntimeHelpers
{
public static Span<TElement> InlineArrayAsSpan<TInlineArray, TElement>(ref TInlineArray inlineArray) where TInlineArray: allows ref struct;
public static ReadOnlySpan<TElement> InlineArrayAsReadOnlySpan<TInlineArray, TElement>(ref readonly TInlineArray inlineArray) where TInlineArray: allows ref struct;
}
API Usage
This API is not meant to be used directly by user code. It called by Roslyn generated code to perform InlineArray to Span conversion.
Alternative Designs
No response
Risks
The API is intrinsic with non-trivial expansion logic.