Skip to content

CS9361 not emitted for stackalloc nested in ternary/parens/cast under [SkipLocalsInit] #84034

@EgorBo

Description

@EgorBo

unsafe-v2

CS9361 (stackalloc without initializer inside SkipLocalsInit requires unsafe) only fires when the stackalloc is the direct RHS of the local initializer. Wrapping it in parentheses, a conditional, or a cast silences the diagnostic, even though the underlying behavior is identical.

Repro

[module: System.Runtime.CompilerServices.SkipLocalsInit]

public class C
{
    public void Direct()         { System.Span<char> s = stackalloc char[128]; }                        // CS9361 ✅
    public void Parens()         { System.Span<char> s = (stackalloc char[128]); }                      // no diagnostic ❌
    public void Ternary(bool b)  { System.Span<char> s = b ? stackalloc char[128] : default; }          // no diagnostic ❌
    public void Cast()           { System.Span<char> s = (System.Span<char>)stackalloc char[128]; }     // no diagnostic ❌
    public void Call()           { Consume(stackalloc char[128]); }    // no diagnostic ❌
    void Consume(System.Span<char> span){}
}

Expected

CS9361 should fire on every StackAllocArrayCreationExpressionSyntax whose result is consumed as Span<T> / ReadOnlySpan<T> under [SkipLocalsInit], regardless of intermediate conversions.

Version

Reproduced on Microsoft.CodeAnalysis.CSharp 5.8.0-1.26266.103 (dotnet/runtime live build).

Note

This issue was drafted with the help of GitHub Copilot.

Metadata

Metadata

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions