Skip to content

Commit 6da59e9

Browse files
authored
VisitCollectionBuilderCollectionExpression - Ensure target of a spread element is rewritten. (#75201)
Fixes #75194
1 parent 54fd790 commit 6da59e9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ private BoundExpression VisitCollectionBuilderCollectionExpression(BoundCollecti
472472
// and that span cannot be captured in a returned ref struct
473473
// we can directly use `anotherReadOnlySpan` as collection builder argument and skip the copying assignment.
474474
BoundExpression span = CanOptimizeSingleSpreadAsCollectionBuilderArgument(node, out var spreadExpression)
475-
? spreadExpression
475+
? VisitExpression(spreadExpression)
476476
: VisitArrayOrSpanCollectionExpression(node, CollectionExpressionTypeKind.ReadOnlySpan, spanType, elementType);
477477

478478
var invocation = new BoundCall(

src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs

+41
Original file line numberDiff line numberDiff line change
@@ -42819,5 +42819,46 @@ .locals init (MyArray<int> V_0,
4281942819
}
4282042820
");
4282142821
}
42822+
42823+
[Fact]
42824+
[WorkItem("https://github.com/dotnet/roslyn/issues/75194")]
42825+
public void LinqSpread()
42826+
{
42827+
string source = """
42828+
using System;
42829+
using System.Runtime.CompilerServices;
42830+
using System.Collections.Generic;
42831+
42832+
CustomizedCollection c = [.. from element in returnArray() select element / 9];
42833+
42834+
foreach (var i in c.Array)
42835+
{
42836+
System.Console.Write(i);
42837+
}
42838+
42839+
42840+
static int[] returnArray() => null;
42841+
42842+
[CollectionBuilder(typeof(CustomizedCollection), nameof(Create))]
42843+
struct CustomizedCollection
42844+
{
42845+
public void Add(int variable) => throw null;
42846+
public IEnumerator<int> GetEnumerator() => throw null;
42847+
public static CustomizedCollection Create(ReadOnlySpan<int> values) => new CustomizedCollection { Array = values.ToArray() };
42848+
42849+
public int[] Array;
42850+
}
42851+
42852+
static class Extensions
42853+
{
42854+
public static ReadOnlySpan<TResult> Select<T, TResult>(this T[] array, Func<T, TResult> selector)
42855+
=> (TResult[])[(TResult)(object)1, (TResult)(object)2, (TResult)(object)3];
42856+
}
42857+
""";
42858+
42859+
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net80);
42860+
42861+
CompileAndVerify(comp, expectedOutput: IncludeExpectedOutput("123"), verify: Verification.Skipped).VerifyDiagnostics();
42862+
}
4282242863
}
4282342864
}

0 commit comments

Comments
 (0)