Skip to content

Error in replace-stackalloc-expression-with-collection-expression fix #81846

@bernd5

Description

@bernd5

Steps to Reproduce:

For the following code:

using System;

scoped Span<int> a = default;

int val = 3;
if (val == 3)
{
    int xxx = 123;
    Span<int> s = stackalloc int[3] { xxx, 1, 1 }; //<-- Here the IDE says: use a collection expression
    //Span<int> s = [xxx, 1, 1];
    a = s;
}
foreach (var item in a)
{
    Console.WriteLine(item);
}

the IDE issues a hint to remove the stackalloc expression and replace it with a collection expression.

Expected Behavior:
It should be okay to use a collection expression in that context (especially because the lifetime of InlineArray-Span-instances are extended to the method scope). The suggested fix should work.

Actual Behavior:
The fix does not work. CS8352 is triggered at a = s

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions