Skip to content

Commit

Permalink
VisitCollectionBuilderCollectionExpression - Ensure target of a sprea…
Browse files Browse the repository at this point in the history
…d element is rewritten. (#75201)

Fixes #75194
  • Loading branch information
AlekseyTs committed Sep 23, 2024
1 parent 54fd790 commit 6da59e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42819,5 +42819,46 @@ .locals init (MyArray<int> 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<int> GetEnumerator() => throw null;
public static CustomizedCollection Create(ReadOnlySpan<int> values) => new CustomizedCollection { Array = values.ToArray() };

public int[] Array;
}

static class Extensions
{
public static ReadOnlySpan<TResult> Select<T, TResult>(this T[] array, Func<T, TResult> 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();
}
}
}

0 comments on commit 6da59e9

Please sign in to comment.