Skip to content

[ReadOnly]Span<T> fields and properties in a ref struct can have a stackalloc as a default value #60568

@DaZombieKiller

Description

@DaZombieKiller

The following code compiles successfully:

using System;

ref struct Example
{
    public Span<byte> Field = stackalloc byte[512];
    public Span<byte> Property { get; } = stackalloc byte[512];
    public Example() {}
}

(SharpLab)

Neither the field nor the property are valid to access, as they are referencing stack memory from the constructor. Since this construct is wholly unsafe, it should probably be disallowed or at least warned.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions