Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisitCollectionBuilderCollectionExpression - Ensure target of a spread element is rewritten. #75201

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}