Skip to content

stackalloc should be disallowed for structs with ref fields #63104

@jkotas

Description

@jkotas

Version Used: Microsoft.Net.Compilers.Toolset 4.4.0-2.22379.18 (current daily build)

Steps to Reproduce:

  1. Build
using System;
using System.Reflection;
using System.Runtime.InteropServices;

unsafe
{
    StructWithRefField* p = stackalloc StructWithRefField[10];

    MyClass o = new();    
    // This has GC hole. The stackallocated array of StructWithRefField is not reported to GC
    p[0].RefField = ref o.ByteField;
}

ref struct StructWithRefField
{
    public ref byte RefField;
}

class MyClass
{
    public byte ByteField;
}

sharplab is crashing on this repro.

Expected Behavior:

Build error. stackalloc should be disallowed for structs with ref fields.

Actual Behavior:

Builds successfully, the program has GC hole at runtime.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions