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

Spread elements on spans should use "for-loop" lowering, not GetEnumerator #76664

Open
RikkiGibson opened this issue Jan 7, 2025 · 0 comments
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature - Collection Expressions
Milestone

Comments

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Jan 7, 2025

Repro: compile and inspect IL on the following: SharpLab

using System;
using System.Collections.Generic;

static HashSet<T> FromSpanSpread<T>(ReadOnlySpan<T> inputs)
{
    return [ .. inputs];
}

static HashSet<T> FromSpanManual<T>(ReadOnlySpan<T> inputs)
{
    HashSet<T> set = new(inputs.Length);
    foreach (T e in inputs)
    {
        set.Add(e);
    }
    
    return set;
}

Expected: both use "for-loop" lowering
Actual: "foreach" is lowered as "for", while the spread element uses GetEnumerator/MoveNext.

Looks like a roslyn bug. I see some bits in foreach lowering which are handling the span case and "rewriting as for", which are missed in the spread element lowering.

else if (node.AwaitOpt is null && CanRewriteForEachAsFor(node.Syntax, nodeExpressionType, out var indexerGet, out var lengthGetter))

Originally posted by @RikkiGibson in dotnet/runtime#111093 (comment)

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 7, 2025
@jaredpar jaredpar added Feature - Collection Expressions Code Gen Quality Room for improvement in the quality of the compiler's generated code and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 8, 2025
@jaredpar jaredpar added this to the Backlog milestone Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature - Collection Expressions
Projects
None yet
Development

No branches or pull requests

2 participants