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

InlineArray fails at runtime when declared as a ref struct. #71058

Closed
burkenyo opened this issue Dec 1, 2023 · 3 comments · Fixed by #71103
Closed

InlineArray fails at runtime when declared as a ref struct. #71058

burkenyo opened this issue Dec 1, 2023 · 3 comments · Fixed by #71103
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Feature - Inline Arrays
Milestone

Comments

@burkenyo
Copy link

burkenyo commented Dec 1, 2023

It appears [InlineArray] structs cannot be ref structs because they need to be passed as generic arguments to helper methods in the runtime. I think this should be a compile-time error.

Version Used:
.NET 8 SDK 8.0.100
Compiler version: '4.9.0-2.23571.2 (d1b2e28)'. Language version: 12.0.

Steps to Reproduce:
run this:

using System;
using System.Runtime.CompilerServices;

TenChars x = new();

Console.WriteLine(((Span<Char>) x).Length);

RefTenChars y = new();

Console.WriteLine(((Span<Char>) y).Length);

[InlineArray(10)]
struct TenChars
{
    char _;
}

[InlineArray(10)]
ref struct RefTenChars
{
    char _;
}

The program fails when trying to convert variable y to Span<char>.

Expected Behavior:

An error is emitted at compile-time.

Actual Behavior:

An exception is thrown at runtime.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 1, 2023
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 4, 2023
@jaredpar jaredpar added this to the 17.9 milestone Dec 4, 2023
@AlekseyTs
Copy link
Contributor

Runtime probably doesn't have this limitation, therefore, declaring the type is probably fine. But the type cannot be indexed or converted in C#, and the fact should be reported when an attempt is made to do that.

AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue Dec 5, 2023
@AlekseyTs AlekseyTs added the 4 - In Review A fix for the issue is submitted for review. label Dec 5, 2023
@burkenyo
Copy link
Author

burkenyo commented Dec 6, 2023

Runtime probably doesn't have this limitation, therefore, declaring the type is probably fine. But the type cannot be indexed or converted in C#, and the fact should be reported when an attempt is made to do that.

I would offer that one of the selling points of Inline Arrays is the ability to treat them as “span-ables”, so declaring something that can’t do that seems less useful.

Nevertheless, the new analyzer being introduced would definitely addresses this for me by letting me know at compile-time that it might not work as expected at run-time.

@burkenyo
Copy link
Author

burkenyo commented Dec 6, 2023

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug Feature - Inline Arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants