Fit Dev
Fit Dev

Reputation: 3685

Why ref structs cannot be used as type arguments?

C# 7.2 introduced ref structs. However, given a ref struct like this:

public ref struct Foo {
  public int Bar;
}

I cannot use it as a type argument:

int i = 0;
var x = Unsafe.As<int, Foo>(ref i); // <- Error CS0306 The type 'Foo' may not be used as a type argument.

I understand that ref structs can only exist on the stack, and not the heap. But what if the generic method that would use such ref structs is guaranteed to never put them on the heap, as in the example above that uses System.Runtime.CompilerServices.Unsafe package? Why can I not use them in those cases as type parameters?

Upvotes: 16

Views: 4604

Answers (2)

Ryan
Ryan

Reputation: 2010

As of C# 13 and .NET 9, Generic parameters may now have an allows ref struct constraint. The compiler will then enforce ref struct like restrictions on the use of the type parameter within the method.

For example, there is the .NET 9 implementation of Dictionary.GetAlternateLookup():

public AlternateLookup<TAlternateKey> GetAlternateLookup<TAlternateKey>()
        where TAlternateKey : notnull, allows ref struct
{
    if (!AlternateLookup<TAlternateKey>.IsCompatibleKey(this))
    {
        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IncompatibleComparer);
    }


    return new AlternateLookup<TAlternateKey>(this);
}  

Source: https://github.com/dotnet/runtime/blob/0cecd7d4d1f8a22c186a6145c6e60b393bf9518d/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs#L649-L658

This, in turn, allows a ref struct type like ReadOnlySpan<char> to be used as an indexer, which was not previously possible:

public readonly struct AlternateLookup<TAlternateKey> where TAlternateKey : notnull, allows ref struct
{
    // ...

    /// <summary>Gets or sets the value associated with the specified alternate key.</summary>
    /// <param name="key">The alternate key of the value to get or set.</param>
    /// <value>
    /// The value associated with the specified alternate key. If the specified alternate key is not found, a get operation throws
    /// a <see cref="KeyNotFoundException"/>, and a set operation creates a new element with the specified key.
    /// </value>
    /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
    /// <exception cref="KeyNotFoundException">The property is retrieved and alternate key does not exist in the collection.</exception>
    public TValue this[TAlternateKey key]
    {
        get
        {
            ref TValue value = ref FindValue(key, out _);
            if (Unsafe.IsNullRef(ref value))
            {
                ThrowHelper.ThrowKeyNotFoundException(GetAlternateComparer(Dictionary).Create(key));
            }


            return value;
        }
        set => GetValueRefOrAddDefault(key, out _) = value;
    }

    // ...
}

Source: https://github.com/dotnet/runtime/blob/0cecd7d4d1f8a22c186a6145c6e60b393bf9518d/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs#L690-L724

Upvotes: 1

Display name
Display name

Reputation: 1542

The primary guarantee made by a ref struct is that it will never escape to the heap.

In a generic method, the compiler doesn't validate the no-heap guarantee (since almost all types can exist on the heap). The most straightforward way to prevent generic methods from leaking ref structs is to simply forbid the use of a ref struct as a type parameter, so that's what C# does.

Beginning with C# 7.2, you can use the ref modifier in the declaration of a structure type. Instances of a ref struct type are allocated on the stack and can't escape to the managed heap. To ensure that, the compiler limits the usage of ref struct types as follows:

  • A ref struct can't be the element type of an array.
  • A ref struct can't be a declared type of a field of a class or a non-ref struct.
  • A ref struct can't implement interfaces.
  • A ref struct can't be boxed to System.ValueType or System.Object.
  • A ref struct can't be a type argument.
  • A ref struct variable can't be captured by a lambda expression or a local function.
  • A ref struct variable can't be used in an async method. However, you can use ref struct variables in synchronous methods, for example, in those that return Task or Task.
  • A ref struct variable can't be used in iterators.

More details from Microsoft about ref structs

Upvotes: 7

Related Questions