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

Collection expressions: invalid code generated for inline array spread element #70708

Closed
cston opened this issue Nov 7, 2023 · 4 comments · Fixed by #75151
Closed

Collection expressions: invalid code generated for inline array spread element #70708

cston opened this issue Nov 7, 2023 · 4 comments · Fixed by #75151
Assignees
Labels
Milestone

Comments

@cston
Copy link
Member

cston commented Nov 7, 2023

Version Used:

Steps to Reproduce:

Compile and run:

using System.Runtime.CompilerServices;

[InlineArray(3)]
struct MyArray<T>
{
    T _e0;
}

class Program
{
    static void Main()
    {
        MyArray<int> x = new();
        x[0] = 1;
        x[1] = 2;
        x[2] = 3;
        int[] y = [..x];
    }
}

Actual Behavior:
Exception thrown at runtime iterating spread.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 7, 2023
@cston cston self-assigned this Nov 7, 2023
@jaredpar jaredpar added this to the 17.9 milestone Nov 8, 2023
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 8, 2023
@jaredpar
Copy link
Member

jaredpar commented Dec 5, 2023

Moving out as this is a fairly niche scenario. Will reconsider based on customer feedback.

@jaredpar jaredpar modified the milestones: 17.9, Backlog Dec 5, 2023
@ZingBallyhoo
Copy link

I ran into this today when trying to build a IReadOnlyCollection from an inline array + some extra data.

Is this scenario understood to be broken in the way it is? If so, this really should be prevented from compiling or be fixed... currently trying to use this seemingly valid pattern is a guaranteed AVE/NRE at runtime. The contents of the inline array are accidentally being reinterpreted as a Span (first 8 bytes being the ref, next 4 being the length) which causes an immediate explosion.

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGADAAQY4B0ASgK4B2ALgJYC2MlAwhKwA7MAbGFADKwgG7MwMAM4BuQoQDaASXoDm9GAEEoUAIYBPABRYAlAF1CGAMykZjKLTCNS1GLygQ1GrboOGhADehKRhpJ7M4vqMMKS0AhD0AOakrAD6wPpgANaayQoEAL6KRHbYpJykIQTh5FhY1aF14dFQpPp6RqQAvKRaAO5uHl4+mjpdJmaFLeGdAUrEFr2kODOzHZNKOMt9WOuzzbMJSclKyzKeMPoAJitKlJTzRhYHLdz0MhBClADytIxKAB1KDMWIAGXGxkusFu0yOpBKxSAA===
(see the Span<ulong>.Enumerator enumerator = ((Span<ulong>*)(&buffer))->GetEnumerator();)

The workaround is to cast the inline array to a span directly before using the spread operator e.g [..(ReadOnlySpan<ulong>)m_array]

@aromaa
Copy link

aromaa commented Jun 21, 2024

Also ran into this myself today. This seems to sometimes cause the CLR to crash and sometimes it just silently fails without the content being added to the list.

@huoyaoyuan
Copy link
Member

One more report at dotnet/runtime#107018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants