Skip to content

Commit

Permalink
Properly iterate through an inline array used as a spread element. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs authored Sep 18, 2024
1 parent a182892 commit 2c8a815
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 72 deletions.
11 changes: 10 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5209,11 +5209,20 @@ static BoundNode bindSpreadElement(SpreadElementSyntax syntax, BindingDiagnostic
Debug.Assert(conversion.IsValid);
diagnostics.Add(syntax.Expression, useSiteInfo);
var convertedExpression = @this.ConvertForEachCollection(expressionPlaceholder, conversion, collectionType, diagnostics);

BoundExpression? lengthOrCount;
if (!@this.TryBindLengthOrCount(syntax.Expression, expressionPlaceholder, out lengthOrCount, diagnostics))

if (enumeratorInfo is { InlineArraySpanType: not WellKnownType.Unknown })
{
_ = expression.Type.HasInlineArrayAttribute(out int length);
Debug.Assert(length > 0);
lengthOrCount = new BoundLiteral(expression.Syntax, ConstantValue.Create(length), @this.GetSpecialType(SpecialType.System_Int32, diagnostics, expression.Syntax)) { WasCompilerGenerated = true };
}
else if (!@this.TryBindLengthOrCount(syntax.Expression, expressionPlaceholder, out lengthOrCount, diagnostics))
{
lengthOrCount = null;
}

return new BoundCollectionExpressionSpreadElement(
syntax,
expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,24 @@ private BoundExpression MakeCollectionExpressionSpreadElement(
rewrittenBody);
}
}
else if (enumeratorInfo is { InlineArraySpanType: not WellKnownType.Unknown })
{
statement = RewriteForEachStatementAsFor(
node,
getPreamble: GetInlineArrayForEachStatementPreambleDelegate(),
getItem: GetInlineArrayForEachStatementGetItemDelegate(),
getLength: GetInlineArrayForEachStatementGetLengthDelegate(),
arg: null,
convertedExpression.Operand,
enumeratorInfo,
elementPlaceholder: null,
elementConversion: null,
iterationVariables,
deconstructionOpt: null,
breakLabel,
continueLabel,
rewrittenBody);
}
else
{
statement = RewriteForEachEnumerator(
Expand Down
Loading

0 comments on commit 2c8a815

Please sign in to comment.