Repository navigation
Description
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
dotnet-policy-service commentedon May 24, 2024
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
stephentoub commentedon May 24, 2024
cc: @jaredpar, @AaronRobinsonMSFT, @AlekseyTs
AaronRobinsonMSFT commentedon May 24, 2024
@stephentoub We should add
Activator.CreateInstance<T>()as well. I'm working on rewriting it - #102636.stephentoub commentedon May 24, 2024
Thanks, it's there, I just left out the "Activator" part by accident. Will fix.
Joe4evr commentedon May 24, 2024
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 structat their declaration site? The anti-constraint (or lack thereof) is really only relevant at use-sites where a delegate instance is actually created/consumed.GrabYourPitchforks commentedon May 24, 2024
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 onTask<>'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 toMakeGenericType. 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.
huoyaoyuan commentedon May 25, 2024
I remember it was discussed and explicitly denied, to avoid breaking change.
tannergooding commentedon May 25, 2024
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 : structorwhere T : IEquatable<T>constraints. For example, given somestruct MyStruct<T> where T : structand somestruct MyOtherStruct<T> where T : struct, it would've been valid to dotypeof(MyStruct<T>).MakeGenericType(myOtherStruct.GetType().GetGenericArguments()[0]). However, if you relax it to bestruct MyOtherStruct<T> { }(dropping thewhere T : structconstraint), this code can now fail given aMyOtherStruct<string> myOtherStructwhich is now valid.jkotas commentedon May 25, 2024
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, butMarshal.PtrToStructure<T>orMarshal.DestroyStructure<T>to be left without the treatment. These APIs are typically used together.stephentoub commentedon May 28, 2024
Sure... I'm not 100% sold on any of the additions in CompilerServices/InteropServices, per:
and just included them for completeness. I'll remove the Marshal ones at least.
terrajobst commentedon May 28, 2024
Video
1 remaining item
ladeak commentedon May 28, 2024
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)
stephentoub commentedon May 28, 2024
default(Span<byte>)KalleOlaviNiemitalo commentedon May 29, 2024
Why does this have
where TResult : allows ref structon Func<out TResult> and Func<in T, out TResult> but not on Func<in T1, in T2, out TResult> and higher arities?stephentoub commentedon May 29, 2024
Copy/paste error.
KalleOlaviNiemitalo commentedon May 29, 2024
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.
jkotas commentedon May 29, 2024
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 ofIsReferenceOrContainsReferencesmay not produce consistent results.tannergooding commentedon May 29, 2024
For what we typically use
IsReferenceOrContainsReferencesfor, I would expect it to returntrue. We don't want to start doing SIMD copies or indexing for such structs, as an example.stephentoub commentedon May 29, 2024
+1
Yes, I just added some tests, and it is not behaving as I'd expect. All of these are failing on coreclr:
T-Gro commentedon May 30, 2024
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" ?stephentoub commentedon Jun 3, 2024
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 beingallows ref structeven though you can't "correctly" implement the non-genericIEnumerator.Current.I expect such a tool could be written.