Closed
Description
Related to #71195
Version Used: 4.9.0-ci (a8119d1)
Steps to Reproduce:
using System.Collections.Generic;
using System.Collections.Immutable;
static ImmutableArray<string> Test(IEnumerable<string> items)
{
return [.. items];
}
Expected Behavior:
return ImmutableArray.CreateRange(items);
OR
return items.ToImmutableArray();
Actual Behavior:
List<string> list = new List<string>();
IEnumerator<string> enumerator = items.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
string current = enumerator.Current;
list.Add(current);
}
}
finally
{
if (enumerator != null)
{
enumerator.Dispose();
}
}
return ImmutableCollectionsMarshal.AsImmutableArray(list.ToArray());
@RikkiGibson @CyrusNajmabadi not entirely sure whether the spec allows this, I think the problem is that the factory API is CreateRange
, but [CollectionBuilder]
over ImmutableArray<T>
uses Create
as the method name. Perhaps it would be possible to just hardcode these additional ImmutableArray<T>
cases? Though it'd be nice to have a more general solution 🤔