Skip to content

[API Proposal]: Employ allows ref struct in libraries #102671

Closed

Description

@stephentoub

Background and motivation

C# 13 and .NET 9 now allow generic parameters to be annotated as where T : allows ref struct, indicating that the T may be a ref struct (expanding the allowed types that may be used as the T, as opposed to normal constraints which require that the T be the specified kind). For this to be broadly useful, we need to annotate appropriate library surface area. Any T annotated as such is then subject to all the rules of a ref struct, and it’s a breaking change to remove the allows ref struct anti-constraint, so we can only use it in places where we don’t now or won’t in the future need to store instances of the T to the heap. Developers implementing interfaces on ref structs also need to be aware that it’s a breaking change for DIMs to be added to an interface implemented on one without also overriding that DIM at the same time, since the lack of an override would result in boxing the ref struct to the interface type.

API Proposal

namespace System
{
    public delegate void Action<in T>(T obj)
+       where T : allows ref struct;

    public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2)
+       where T1 : allows ref struct
+       where T2 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct
+       where T15 : allows ref struct;

    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct
+       where T15 : allows ref struct
+       where T16 : allows ref struct;

    public static class Activator
    {
        public static T CreateInstance<T>()
+           where T : allows ref struct
    }

    public delegate int Comparison<in T>(T x, T y)
+       where T : allows ref struct;

    public delegate TOutput Converter<in TInput, out TOutput>(TInput input)
+       where TInput : allows ref struct
+       where TOutput : allows ref struct;

    public delegate TResult Func<out TResult>()
+       where TResult : allows ref struct;

    public delegate TResult Func<in T, out TResult>(T arg)
+       where T : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct
+       where T15 : allows ref struct
+       where TResult : allows ref struct;

    public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
+       where T1 : allows ref struct
+       where T2 : allows ref struct
+       where T3 : allows ref struct
+       where T4 : allows ref struct
+       where T5 : allows ref struct
+       where T6 : allows ref struct
+       where T7 : allows ref struct
+       where T8 : allows ref struct
+       where T9 : allows ref struct
+       where T10 : allows ref struct
+       where T11 : allows ref struct
+       where T12 : allows ref struct
+       where T13 : allows ref struct
+       where T14 : allows ref struct
+       where T15 : allows ref struct
+       where T16 : allows ref struct
+       where TResult : allows ref struct;

    public delegate bool Predicate<in T>(T obj)
+       where T : allows ref struct;

    public sealed class String
    {
        public static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
+           where TState : allows ref struct
    }
}

namespace System.Buffers
{
    public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg)
+       where TArg : allows ref struct;

    public delegate void ReadOnlySpanAction<T, in TArg>(ReadOnlySpan<T> span, TArg arg)
+       where TArg : allows ref struct;
}

namespace System.Collections.Concurrent
{
    public class ConcurrentDictionary<TKey, TValue>
    {
        public TValue AddOrUpdate<TArg>(TKey key, Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)
+           where TArg : allows ref struct

        public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
+           where TArg : allows ref struct
    }
}

namespace System.Collections.Generic
{
   public interface IEnumerable<out T> : IEnumerable
+       where T : allows ref struct

    public interface IEnumerator<out T> : IDisposable, IEnumerator
+       where T : allows ref struct
}

namespace System.Collections.Immutable
{
    public static class ImmutableArray
    {
        public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(ImmutableArray<TSource> items, System.Func<TSource, TArg, TResult> selector, TArg arg)
+#if NET9_0_OR_GREATER
+           where TArg : allows ref struct
+#endif

        public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(ImmutableArray<TSource> items, int start, int length, Func<TSource, TArg, TResult> selector, TArg arg)
+#if NET9_0_OR_GREATER
+           where TArg : allows ref struct
+#endif
    }

    public static class ImmutableInterlocked
    {
        public static TValue GetOrAdd<TKey, TValue, TArg>(ref ImmutableDictionary<TKey, TValue> location, TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TKey : notnull
+#if NET9_0_OR_GREATER
+           where TArg : allows ref struct
+#endif

        public static bool Update<T, TArg>(ref T location, System.Func<T, TArg, T> transformer, TArg transformerArgument) where T : class?
+#if NET9_0_OR_GREATER
+           where TArg : allows ref struct
+#endif

        public static bool Update<T, TArg>(ref ImmutableArray<T> location, Func<ImmutableArray<T>, TArg, ImmutableArray<T>> transformer, TArg transformerArgument)
+#if NET9_0_OR_GREATER
+           where TArg : allows ref struct
+#endif
    }
}

namespace System.Runtime.CompilerServices
{
    public static unsafe class Unsafe
    {
        public static void* AsPointer<T>(ref T value)
+           where T : allows ref struct

        public static int SizeOf<T>()
+           where T : allows ref struct

        public static ref TTo As<TFrom, TTo>(ref TFrom source)
+           where TFrom : allows ref struct
+           where TTo : allows ref struct

        public static ref T Add<T>(ref T source, int elementOffset)
+           where T : allows ref struct

        public static ref T Add<T>(ref T source, IntPtr elementOffset)
+           where T : allows ref struct

        public static void* Add<T>(void* source, int elementOffset)
+           where T : allows ref struct

        public static ref T Add<T>(ref T source, nuint elementOffset)
+           where T : allows ref struct

        public static ref T AddByteOffset<T>(ref T source, nuint byteOffset)
+           where T : allows ref struct

        public static bool AreSame<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
+           where T : allows ref struct

        public static TTo BitCast<TFrom, TTo>(TFrom source)
+           where TFrom : allows ref struct
+           where TTo : allows ref struct

        public static void Copy<T>(void* destination, ref readonly T source)
+           where T : allows ref struct

        public static void Copy<T>(ref T destination, void* source)
+           where T : allows ref struct

        public static bool IsAddressGreaterThan<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
+           where T : allows ref struct

        public static bool IsAddressLessThan<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
+           where T : allows ref struct

        public static T ReadUnaligned<T>(void* source)
+           where T : allows ref struct

        public static T ReadUnaligned<T>(ref readonly byte source)
+           where T : allows ref struct

        public static void WriteUnaligned<T>(void* destination, T value)
+           where T : allows ref struct

        public static void WriteUnaligned<T>(ref byte destination, T value)
+           where T : allows ref struct

        public static ref T AddByteOffset<T>(ref T source, IntPtr byteOffset)
+           where T : allows ref struct

        public static T Read<T>(void* source)
+           where T : allows ref struct

        public static void Write<T>(void* destination, T value)
+           where T : allows ref struct

        public static ref T AsRef<T>(void* source)
+           where T : allows ref struct

        public static ref T AsRef<T>(scoped ref readonly T source)
+           where T : allows ref struct

        public static IntPtr ByteOffset<T>([AllowNull] ref readonly T origin, [AllowNull] ref readonly T target)
+           where T : allows ref struct

        public static ref T NullRef<T>()
+           where T : allows ref struct

        public static bool IsNullRef<T>(ref readonly T source)
+           where T : allows ref struct

        public static void SkipInit<T>(out T value)
+           where T : allows ref struct

        public static ref T Subtract<T>(ref T source, int elementOffset)
+           where T : allows ref struct

        public static void* Subtract<T>(void* source, int elementOffset)
+           where T : allows ref struct

        public static ref T Subtract<T>(ref T source, IntPtr elementOffset)
+           where T : allows ref struct

        public static ref T Subtract<T>(ref T source, nuint elementOffset)
+           where T : allows ref struct

        public static ref T SubtractByteOffset<T>(ref T source, IntPtr byteOffset)
+           where T : allows ref struct

        public static ref T SubtractByteOffset<T>(ref T source, nuint byteOffset)
+           where T : allows ref struct
    }
}

namespace System.Runtime.InteropServices
{
    public static class RuntimeHelpers
    {
        public static bool IsReferenceOrContainsReferences<T>()
+           where T : allows ref struct
    }
}

API Usage

e.g.

ReadOnlySpan<char> slice = ...;
string result = string.Create(slice.Length * 2, slice, (dest, slice) =>
{
    slice.CopyTo(dest);
    slice.CopyTo(dest.Slice(slice.Length);
});

Alternative Designs

n/a

Risks

No response

Activity

  1. added
    api-ready-for-review
    on May 24, 2024
  2. added this to the 9.0.0 milestone on May 24, 2024
  3. dotnet-policy-service commented on May 24, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  4. stephentoub commented on May 24, 2024

    @stephentoub
    MemberAuthor
  5. AaronRobinsonMSFT commented on May 24, 2024

    @AaronRobinsonMSFT
    Member

    @stephentoub We should add Activator.CreateInstance<T>() as well. I'm working on rewriting it - #102636.

  6. stephentoub commented on May 24, 2024

    @stephentoub
    MemberAuthor

    Thanks, it's there, I just left out the "Activator" part by accident. Will fix.

  7. added
    blocking
    on May 24, 2024
  8. Joe4evr commented on May 24, 2024

    @Joe4evr
    Contributor

    Is there anything stopping the compiler from heuristically treating type parameters on delegate types (provided they're an input and not otherwise constrained) implicitly as allows ref struct at their declaration site? The anti-constraint (or lack thereof) is really only relevant at use-sites where a delegate instance is actually created/consumed.

  9. GrabYourPitchforks commented on May 24, 2024

    @GrabYourPitchforks
    Member
    var taskType = typeof(Task<>).MakeGenericType(typeof(Action<>).GetGenericArguments()[0]);
    Console.WriteLine(taskType);

    That code compiles and executes successfully, even though it doesn't do anything. It's legal since the generic constraints on Action<>'s TArg are no more permissive than the generic constraints on Task<>'s TArg.

    But if Action<>'s TArg ever becomes more permissive (e.g., because it now allows ref structs), the reflection APIs will throw an exception in the call to MakeGenericType. It's a really weird edge case, and I don't know why somebody would do this with the unbound TArg type instead of a real bound type. But... I guess technically it's a break?

    Edit: I'm not arguing that this is undesirable. Just that there might be some bizarre unintended consequences and we should be sure we're ok with them before proceeding.

  10. huoyaoyuan commented on May 25, 2024

    @huoyaoyuan
    Member

    Is there anything stopping the compiler from heuristically treating type parameters on delegate types

    I remember it was discussed and explicitly denied, to avoid breaking change.

  11. tannergooding commented on May 25, 2024

    @tannergooding
    Member

    Just that there might be some bizarre unintended consequences and we should be sure we're ok with them before proceeding.

    I expect this is one of those cases where metadata versions over time, where we've always viewed generic constraints becoming more permissive as acceptable, and where we've typically viewed using reflection to be dangerous and prone to breaks. So we should say that this is one of the weird edge cases that ideally no one is doing but which is overall best for the ecosystem to allow to break.

    It's also worth noting you could run into the same consideration any of the places where we removed where T : struct or where T : IEquatable<T> constraints. For example, given some struct MyStruct<T> where T : struct and some struct MyOtherStruct<T> where T : struct, it would've been valid to do typeof(MyStruct<T>).MakeGenericType(myOtherStruct.GetType().GetGenericArguments()[0]). However, if you relax it to be struct MyOtherStruct<T> { } (dropping the where T : struct constraint), this code can now fail given a MyOtherStruct<string> myOtherStruct which is now valid.

  12. jkotas commented on May 25, 2024

    @jkotas
    Member

    Marshal.SizeOf

    Should we omit the legacy interop APIs from this proposal? From user point of view, it is inconsistent for Marshal.SizeOf<T> to get the treatment, but Marshal.PtrToStructure<T> or Marshal.DestroyStructure<T> to be left without the treatment. These APIs are typically used together.

  13. stephentoub commented on May 28, 2024

    @stephentoub
    MemberAuthor

    Marshal.SizeOf

    Should we omit the legacy interop APIs from this proposal? From user point of view, it is inconsistent for Marshal.SizeOf<T> to get the treatment, but Marshal.PtrToStructure<T> or Marshal.DestroyStructure<T> to be left without the treatment. These APIs are typically used together.

    Sure... I'm not 100% sold on any of the additions in CompilerServices/InteropServices, per:

    // OPEN ISSUE:
    // I'm not sure how useful these CompilerServices / InteropServices allowances are in practice.

    and just included them for completeness. I'll remove the Marshal ones at least.

  14. terrajobst commented on May 28, 2024

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System
    {
        public delegate void Action<in T>(T obj)
            where T : allows ref struct;
    
        public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2)
            where T1 : allows ref struct
            where T2 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct
            where T15 : allows ref struct;
    
        public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct
            where T15 : allows ref struct
            where T16 : allows ref struct;
    
        public static class Activator
        {
            public static T CreateInstance<T>()
                where T : allows ref struct
        }
    
        public delegate int Comparison<in T>(T x, T y)
            where T : allows ref struct;
    
        public delegate TOutput Converter<in TInput, out TOutput>(TInput input)
            where TInput : allows ref struct
            where TOutput : allows ref struct;
    
        public delegate TResult Func<out TResult>()
            where TResult : allows ref struct;
    
        public delegate TResult Func<in T, out TResult>(T arg)
            where T : allows ref struct
            where TResult : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2)
            where T1 : allows ref struct
            where T2 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct
            where T15 : allows ref struct;
    
        public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
            where T1 : allows ref struct
            where T2 : allows ref struct
            where T3 : allows ref struct
            where T4 : allows ref struct
            where T5 : allows ref struct
            where T6 : allows ref struct
            where T7 : allows ref struct
            where T8 : allows ref struct
            where T9 : allows ref struct
            where T10 : allows ref struct
            where T11 : allows ref struct
            where T12 : allows ref struct
            where T13 : allows ref struct
            where T14 : allows ref struct
            where T15 : allows ref struct
            where T16 : allows ref struct;
    
        public delegate bool Predicate<in T>(T obj)
            where T : allows ref struct;
    
        public sealed class String
        {
            public static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
                where TState : allows ref struct
        }
    }
    
    namespace System.Buffers
    {
        public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg)
            where TArg : allows ref struct;
    
        public delegate void ReadOnlySpanAction<T, in TArg>(ReadOnlySpan<T> span, TArg arg)
            where TArg : allows ref struct;
    }
    
    namespace System.Collections.Concurrent
    {
        public class ConcurrentDictionary<TKey, TValue>
        {
            public TValue AddOrUpdate<TArg>(TKey key, Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)
                where TArg : allows ref struct
    
            public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
                where TArg : allows ref struct
        }
    }
    
    namespace System.Collections.Generic
    {
       public interface IEnumerable<out T> : IEnumerable
            where T : allows ref struct
    
        public interface IEnumerator<out T> : IDisposable, IEnumerator
            where T : allows ref struct
    }
    
    namespace System.Collections.Immutable
    {
        public static class ImmutableArray
        {
            public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(ImmutableArray<TSource> items, System.Func<TSource, TArg, TResult> selector, TArg arg)
     #if NET9_0_OR_GREATER
                where TArg : allows ref struct
     #endif
    
            public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(ImmutableArray<TSource> items, int start, int length, Func<TSource, TArg, TResult> selector, TArg arg)
     #if NET9_0_OR_GREATER
                where TArg : allows ref struct
     #endif
        }
    
        public static class ImmutableInterlocked
        {
            public static TValue GetOrAdd<TKey, TValue, TArg>(ref ImmutableDictionary<TKey, TValue> location, TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TKey : notnull
     #if NET9_0_OR_GREATER
                where TArg : allows ref struct
     #endif
    
            public static bool Update<T, TArg>(ref T location, System.Func<T, TArg, T> transformer, TArg transformerArgument) where T : class?
     #if NET9_0_OR_GREATER
                where TArg : allows ref struct
     #endif
    
            public static bool Update<T, TArg>(ref ImmutableArray<T> location, Func<ImmutableArray<T>, TArg, ImmutableArray<T>> transformer, TArg transformerArgument)
     #if NET9_0_OR_GREATER
                where TArg : allows ref struct
     #endif
        }
    }
    
    namespace System.Runtime.CompilerServices
    {
        public static unsafe class Unsafe
        {
            public static void* AsPointer<T>(ref T value)
                where T : allows ref struct
    
            public static int SizeOf<T>()
                where T : allows ref struct
    
            public static ref TTo As<TFrom, TTo>(ref TFrom source)
                where TFrom : allows ref struct
                where TTo : allows ref struct
    
            public static ref T Add<T>(ref T source, int elementOffset)
                where T : allows ref struct
    
            public static ref T Add<T>(ref T source, IntPtr elementOffset)
                where T : allows ref struct
    
            public static void* Add<T>(void* source, int elementOffset)
                where T : allows ref struct
    
            public static ref T Add<T>(ref T source, nuint elementOffset)
                where T : allows ref struct
    
            public static ref T AddByteOffset<T>(ref T source, nuint byteOffset)
                where T : allows ref struct
    
            public static bool AreSame<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
                where T : allows ref struct
    
            public static TTo BitCast<TFrom, TTo>(TFrom source)
                where TFrom : allows ref struct
                where TTo : allows ref struct
    
            public static void Copy<T>(void* destination, ref readonly T source)
                where T : allows ref struct
    
            public static void Copy<T>(ref T destination, void* source)
                where T : allows ref struct
    
            public static bool IsAddressGreaterThan<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
                where T : allows ref struct
    
            public static bool IsAddressLessThan<T>([AllowNull] ref readonly T left, [AllowNull] ref readonly T right)
                where T : allows ref struct
    
            public static T ReadUnaligned<T>(void* source)
                where T : allows ref struct
    
            public static T ReadUnaligned<T>(ref readonly byte source)
                where T : allows ref struct
    
            public static void WriteUnaligned<T>(void* destination, T value)
                where T : allows ref struct
    
            public static void WriteUnaligned<T>(ref byte destination, T value)
                where T : allows ref struct
    
            public static ref T AddByteOffset<T>(ref T source, IntPtr byteOffset)
                where T : allows ref struct
    
            public static T Read<T>(void* source)
                where T : allows ref struct
    
            public static void Write<T>(void* destination, T value)
                where T : allows ref struct
    
            public static ref T AsRef<T>(void* source)
                where T : allows ref struct
    
            public static ref T AsRef<T>(scoped ref readonly T source)
                where T : allows ref struct
    
            public static IntPtr ByteOffset<T>([AllowNull] ref readonly T origin, [AllowNull] ref readonly T target)
                where T : allows ref struct
    
            public static ref T NullRef<T>()
                where T : allows ref struct
    
            public static bool IsNullRef<T>(ref readonly T source)
                where T : allows ref struct
    
            public static void SkipInit<T>(out T value)
                where T : allows ref struct
    
            public static ref T Subtract<T>(ref T source, int elementOffset)
                where T : allows ref struct
    
            public static void* Subtract<T>(void* source, int elementOffset)
                where T : allows ref struct
    
            public static ref T Subtract<T>(ref T source, IntPtr elementOffset)
                where T : allows ref struct
    
            public static ref T Subtract<T>(ref T source, nuint elementOffset)
                where T : allows ref struct
    
            public static ref T SubtractByteOffset<T>(ref T source, IntPtr byteOffset)
                where T : allows ref struct
    
            public static ref T SubtractByteOffset<T>(ref T source, nuint byteOffset)
                where T : allows ref struct
        }
    }
    
    namespace System.Runtime.InteropServices
    {
        public static class RuntimeHelpers
        {
            public static bool IsReferenceOrContainsReferences<T>()
                where T : allows ref struct
        }
    }
  15. 1 remaining item

  16. added
    api-approved
    on May 28, 2024
  17. ladeak commented on May 28, 2024

    @ladeak
    Contributor
    public static class Activator
    {
        public static T CreateInstance<T>()
            where T : allows ref struct
    }

    So if someone invokes it as:

    Activator.CreateInstance<Span<byte>>().

    What would it return as the underlying type? An array? (or how would it decide)

  18. stephentoub commented on May 28, 2024

    @stephentoub
    MemberAuthor

    So if someone invokes it as:
    Activator.CreateInstance<Span<byte>>().
    What would it return as the underlying type? An array? (or how would it decide)

    default(Span<byte>)

  19. self-assigned this
    on May 29, 2024
  20. KalleOlaviNiemitalo commented on May 29, 2024

    @KalleOlaviNiemitalo

    Why does this have where TResult : allows ref struct on Func<out TResult> and Func<in T, out TResult> but not on Func<in T1, in T2, out TResult> and higher arities?

  21. stephentoub commented on May 29, 2024

    @stephentoub
    MemberAuthor

    Why does this have where TResult : allows ref struct on Func and Func<in T, out TResult> but not on Func<in T1, in T2, out TResult> and higher arities?

    Copy/paste error.

  22. KalleOlaviNiemitalo commented on May 29, 2024

    @KalleOlaviNiemitalo

    I presume IsReferenceOrContainsReferences will only check for actual references and not for ref-like types. So if you have

    ref struct R { int i; }

    then RuntimeHelpers.IsReferenceOrContainsReferences<R>() returns false, because R is not a reference type and does not contain references, even though it is a ref struct type.

  23. jkotas commented on May 29, 2024

    @jkotas
    Member

    IsReferenceOrContainsReferences

    A more interesting case are ref-structs that contain byrefs. For example, should IsReferenceOrContainsReferences<Span<int>>() return true or false? I suspect that the existing implementations of IsReferenceOrContainsReferences may not produce consistent results.

  24. tannergooding commented on May 29, 2024

    @tannergooding
    Member

    For example, should IsReferenceOrContainsReferences<Span>() return true or false

    For what we typically use IsReferenceOrContainsReferences for, I would expect it to return true. We don't want to start doing SIMD copies or indexing for such structs, as an example.

  25. stephentoub commented on May 29, 2024

    @stephentoub
    MemberAuthor

    For what we typically use IsReferenceOrContainsReferences for, I would expect it to return true. We don't want to start doing SIMD copies or indexing for such structs, as an example.

    +1

    I suspect that the existing implementations of IsReferenceOrContainsReferences may not produce consistent results.

    Yes, I just added some tests, and it is not behaving as I'd expect. All of these are failing on coreclr:

                Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<Span<char>>());
                Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<ReadOnlySpan<char>>());
                Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithRef>());
                Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<RefStructWithNestedRef>());
  26. added
    in-pr
    on May 30, 2024
  27. T-Gro commented on May 30, 2024

    @T-Gro
    Member

    Was this proposal human-written or compiler generated?
    Could Roslyn be locally tweaked to show which APIs could potentially be allows ref struct'd, and the PR would then be a division of that into "we want those" vs "we don't want to because" ?

  28. stephentoub commented on Jun 3, 2024

    @stephentoub
    MemberAuthor

    Was this proposal human-written or compiler generated?

    Hand-written. The set of what we want to support is not a subset of what's supported, e.g. we can mark the T in IEnumerator<T> as being allows ref struct even though you can't "correctly" implement the non-generic IEnumerator.Current.

    Could Roslyn be locally tweaked to show which APIs could potentially be allows ref struct'd

    I expect such a tool could be written.

  29. locked and limited conversation to collaborators on Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Runtimeblockingin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions