diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs index f24cc72535321..641913d90b11d 100644 --- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs +++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_CollectionExpression.cs @@ -472,7 +472,7 @@ private BoundExpression VisitCollectionBuilderCollectionExpression(BoundCollecti // and that span cannot be captured in a returned ref struct // we can directly use `anotherReadOnlySpan` as collection builder argument and skip the copying assignment. BoundExpression span = CanOptimizeSingleSpreadAsCollectionBuilderArgument(node, out var spreadExpression) - ? spreadExpression + ? VisitExpression(spreadExpression) : VisitArrayOrSpanCollectionExpression(node, CollectionExpressionTypeKind.ReadOnlySpan, spanType, elementType); var invocation = new BoundCall( diff --git a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs index c2810818113b4..9780b076011c7 100644 --- a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs +++ b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs @@ -42819,5 +42819,46 @@ .locals init (MyArray V_0, } "); } + + [Fact] + [WorkItem("https://github.com/dotnet/roslyn/issues/75194")] + public void LinqSpread() + { + string source = """ + using System; + using System.Runtime.CompilerServices; + using System.Collections.Generic; + + CustomizedCollection c = [.. from element in returnArray() select element / 9]; + + foreach (var i in c.Array) + { + System.Console.Write(i); + } + + + static int[] returnArray() => null; + + [CollectionBuilder(typeof(CustomizedCollection), nameof(Create))] + struct CustomizedCollection + { + public void Add(int variable) => throw null; + public IEnumerator GetEnumerator() => throw null; + public static CustomizedCollection Create(ReadOnlySpan values) => new CustomizedCollection { Array = values.ToArray() }; + + public int[] Array; + } + + static class Extensions + { + public static ReadOnlySpan Select(this T[] array, Func selector) + => (TResult[])[(TResult)(object)1, (TResult)(object)2, (TResult)(object)3]; + } + """; + + var comp = CreateCompilation(source, targetFramework: TargetFramework.Net80); + + CompileAndVerify(comp, expectedOutput: IncludeExpectedOutput("123"), verify: Verification.Skipped).VerifyDiagnostics(); + } } }