Skip to content

Commit

Permalink
Use where T : allows ref struct in runtime (#102795)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored and pull[bot] committed Aug 22, 2024
1 parent 8da2c0d commit 4240277
Show file tree
Hide file tree
Showing 32 changed files with 1,114 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public static object GetUninitializedObject(
/// <returns>true if given type is reference type or value type that contains references</returns>
[Intrinsic]
public static bool IsReferenceOrContainsReferences<T>()
// where T : allows ref struct // TODO https://github.com/dotnet/runtime/issues/102847
{
// The body of this function will be replaced by the EE with unsafe code!!!
// See getILIntrinsicImplementationForRuntimeHelpers for how this happens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public ConcurrentDictionary(int concurrencyLevel, int capacity, System.Collectio
public System.Collections.Generic.ICollection<TValue> Values { get { throw null; } }
public TValue AddOrUpdate(TKey key, System.Func<TKey, TValue> addValueFactory, System.Func<TKey, TValue, TValue> updateValueFactory) { throw null; }
public TValue AddOrUpdate(TKey key, TValue addValue, System.Func<TKey, TValue, TValue> updateValueFactory) { throw null; }
public TValue AddOrUpdate<TArg>(TKey key, System.Func<TKey, TArg, TValue> addValueFactory, System.Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument) { throw null; }
public TValue AddOrUpdate<TArg>(TKey key, System.Func<TKey, TArg, TValue> addValueFactory, System.Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument) where TArg : allows ref struct { throw null; }
public void Clear() { }
public bool ContainsKey(TKey key) { throw null; }
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, TValue>> GetEnumerator() { throw null; }
public TValue GetOrAdd(TKey key, System.Func<TKey, TValue> valueFactory) { throw null; }
public TValue GetOrAdd(TKey key, TValue value) { throw null; }
public TValue GetOrAdd<TArg>(TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) { throw null; }
public TValue GetOrAdd<TArg>(TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TArg : allows ref struct { throw null; }
void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Add(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Contains(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { throw null; }
void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.CopyTo(System.Collections.Generic.KeyValuePair<TKey, TValue>[] array, int index) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
/// key is already in the dictionary, or the new value for the key as returned by valueFactory
/// if the key was not in the dictionary.</returns>
public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
where TArg : allows ref struct
{
if (key is null)
{
Expand Down Expand Up @@ -1279,6 +1280,7 @@ public TValue GetOrAdd(TKey key, TValue value)
/// absent) or the result of updateValueFactory (if the key was present).</returns>
public TValue AddOrUpdate<TArg>(
TKey key, Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)
where TArg : allows ref struct
{
if (key is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,16 @@ public static partial class ImmutableArray
public static System.Collections.Immutable.ImmutableArray<T> CreateRange<T>(System.Collections.Generic.IEnumerable<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, System.Func<TSource, TResult> selector) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, int start, int length, System.Func<TSource, TResult> selector) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, System.Func<TSource, TArg, TResult> selector, TArg arg) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, int start, int length, System.Func<TSource, TArg, TResult> selector, TArg arg) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, System.Func<TSource, TArg, TResult> selector, TArg arg)
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{ throw null; }
public static System.Collections.Immutable.ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(System.Collections.Immutable.ImmutableArray<TSource> items, int start, int length, System.Func<TSource, TArg, TResult> selector, TArg arg)
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{ throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(System.Collections.Immutable.ImmutableArray<T> items, int start, int length) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(T item) { throw null; }
Expand Down Expand Up @@ -672,7 +680,11 @@ public static partial class ImmutableInterlocked
public static void Enqueue<T>(ref System.Collections.Immutable.ImmutableQueue<T> location, T value) { }
public static TValue GetOrAdd<TKey, TValue>(ref System.Collections.Immutable.ImmutableDictionary<TKey, TValue> location, TKey key, System.Func<TKey, TValue> valueFactory) where TKey : notnull { throw null; }
public static TValue GetOrAdd<TKey, TValue>(ref System.Collections.Immutable.ImmutableDictionary<TKey, TValue> location, TKey key, TValue value) where TKey : notnull { throw null; }
public static TValue GetOrAdd<TKey, TValue, TArg>(ref System.Collections.Immutable.ImmutableDictionary<TKey, TValue> location, TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TKey : notnull { throw null; }
public static TValue GetOrAdd<TKey, TValue, TArg>(ref System.Collections.Immutable.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
{ throw null; }
public static System.Collections.Immutable.ImmutableArray<T> InterlockedCompareExchange<T>(ref System.Collections.Immutable.ImmutableArray<T> location, System.Collections.Immutable.ImmutableArray<T> value, System.Collections.Immutable.ImmutableArray<T> comparand) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> InterlockedExchange<T>(ref System.Collections.Immutable.ImmutableArray<T> location, System.Collections.Immutable.ImmutableArray<T> value) { throw null; }
public static bool InterlockedInitialize<T>(ref System.Collections.Immutable.ImmutableArray<T> location, System.Collections.Immutable.ImmutableArray<T> value) { throw null; }
Expand All @@ -683,9 +695,17 @@ public static void Push<T>(ref System.Collections.Immutable.ImmutableStack<T> lo
public static bool TryRemove<TKey, TValue>(ref System.Collections.Immutable.ImmutableDictionary<TKey, TValue> location, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) where TKey : notnull { throw null; }
public static bool TryUpdate<TKey, TValue>(ref System.Collections.Immutable.ImmutableDictionary<TKey, TValue> location, TKey key, TValue newValue, TValue comparisonValue) where TKey : notnull { throw null; }
public static bool Update<T>(ref T location, System.Func<T, T> transformer) where T : class? { throw null; }
public static bool Update<T, TArg>(ref T location, System.Func<T, TArg, T> transformer, TArg transformerArgument) where T : class? { throw null; }
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
{ throw null; }
public static bool Update<T>(ref System.Collections.Immutable.ImmutableArray<T> location, Func<System.Collections.Immutable.ImmutableArray<T>, System.Collections.Immutable.ImmutableArray<T>> transformer) { throw null; }
public static bool Update<T, TArg>(ref System.Collections.Immutable.ImmutableArray<T> location, Func<System.Collections.Immutable.ImmutableArray<T>, TArg, System.Collections.Immutable.ImmutableArray<T>> transformer, TArg transformerArgument) { throw null; }
public static bool Update<T, TArg>(ref System.Collections.Immutable.ImmutableArray<T> location, Func<System.Collections.Immutable.ImmutableArray<T>, TArg, System.Collections.Immutable.ImmutableArray<T>> transformer, TArg transformerArgument)
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{ throw null; }
}
public static partial class ImmutableList
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ public static ImmutableArray<TResult> CreateRange<TSource, TResult>(ImmutableArr
/// the source array.
/// </remarks>
public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(ImmutableArray<TSource> items, Func<TSource, TArg, TResult> selector, TArg arg)
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{
Requires.NotNull(selector, nameof(selector));

Expand Down Expand Up @@ -370,6 +373,9 @@ public static ImmutableArray<TResult> CreateRange<TSource, TArg, TResult>(Immuta
/// included in the resulting array.
/// </remarks>
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
{
int itemsLength = items.Length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public static bool Update<T>(ref T location, Func<T, T> transformer) where T : c
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
public static bool Update<T, TArg>(ref T location, Func<T, TArg, T> transformer, TArg transformerArgument) where T : class?
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{
Requires.NotNull(transformer, nameof(transformer));

Expand Down Expand Up @@ -162,6 +165,9 @@ public static bool Update<T>(ref ImmutableArray<T> location, Func<ImmutableArray
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
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
{
Requires.NotNull(transformer, nameof(transformer));

Expand Down Expand Up @@ -241,7 +247,11 @@ public static bool InterlockedInitialize<T>(ref ImmutableArray<T> location, Immu
/// <param name="valueFactory">The function to execute to obtain the value to insert into the dictionary if the key is not found.</param>
/// <param name="factoryArgument">The argument to pass to the value factory.</param>
/// <returns>The value obtained from the dictionary or <paramref name="valueFactory"/> if it was not present.</returns>
public static TValue GetOrAdd<TKey, TValue, TArg>(ref ImmutableDictionary<TKey, TValue> location, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TKey : notnull
public static TValue GetOrAdd<TKey, TValue, TArg>(ref ImmutableDictionary<TKey, TValue> location, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
where TKey : notnull
#if NET9_0_OR_GREATER
where TArg : allows ref struct
#endif
{
Requires.NotNull(valueFactory, nameof(valueFactory));

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ private static TResult Sum<T, TResult>(ReadOnlySpan<T> span)

if (typeof(T) == typeof(long))
{
return (TResult)(object)SumSignedIntegersVectorized(MemoryMarshal.Cast<T, long>(span));
return (TResult)(object)SumSignedIntegersVectorized(Unsafe.BitCast<ReadOnlySpan<T>, ReadOnlySpan<long>>(span));
}
if (typeof(T) == typeof(int))
{
return (TResult)(object)SumSignedIntegersVectorized(MemoryMarshal.Cast<T, int>(span));
return (TResult)(object)SumSignedIntegersVectorized(Unsafe.BitCast<ReadOnlySpan<T>, ReadOnlySpan<int>>(span));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ private static void ValidateInputOutputSpanNonOverlapping<T>(ReadOnlySpan<T> inp
private static unsafe Span<TTo> Rename<TFrom, TTo>(Span<TFrom> span)
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
#if NET9_0_OR_GREATER
return Unsafe.BitCast<Span<TFrom>, Span<TTo>>(span);
#else
return *(Span<TTo>*)(&span);
#endif
}

/// <summary>Creates a span of <typeparamref name="TTo"/> from a <typeparamref name="TFrom"/> when they're the same type.</summary>
Expand All @@ -48,7 +52,11 @@ private static unsafe Span<TTo> Rename<TFrom, TTo>(Span<TFrom> span)
private static unsafe ReadOnlySpan<TTo> Rename<TFrom, TTo>(ReadOnlySpan<TFrom> span)
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
#if NET9_0_OR_GREATER
return Unsafe.BitCast<ReadOnlySpan<TFrom>, ReadOnlySpan<TTo>>(span);
#else
return *(ReadOnlySpan<TTo>*)(&span);
#endif
}

/// <summary>Mask used to handle alignment elements before vectorized handling of the input.</summary>
Expand Down
Loading

0 comments on commit 4240277

Please sign in to comment.