Skip to content

can stackalloc be allowed in ref struct constructors? #35658

Closed

Description

Version Used:

c# 7.3

Steps to Reproduce:

  public ref struct Matrix<T> where T: unmanaged
   {
       private readonly int w;
       private readonly int h;
       private readonly Span<T> span;

       public Matrix(int w, int h)
       {
           this.w = w;
           this.h = h;
           this.span = stackalloc T[w*h];
       }

results in error
CS8353: A result of a stackalloc expression of type 'Span<T>' cannot be used in this context because it may be exposed outside of the containing method

I think I understand the error message and why it is necessary to prevent it.
But in this specific case the this.span is a field on a ref struct itself, so the stack will be maintained and the matrix itself cannot "be exposed outside the context".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions