Closed
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When trying to initialize a ReadOnlySpan of an enum, I'm hitting a runtime issue, "cannot initialize array of non-primitive type".
Expected Behavior
I should be able to initialize a ReadOnlySpan of an enum, based on this issue: dotnet/roslyn#61414
Steps To Reproduce
Here's a minimalistic solution with an example of the same code working in a console app, and crashing in a Blazor WASM app: https://github.com/codemonkey85/BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives
The relevant code is:
ReadOnlySpan<MyEnum> myEnums = new[]
{
MyEnum.A,
MyEnum.B,
MyEnum.C
};
Console.WriteLine(string.Join(", ", myEnums.ToArray()));
enum MyEnum
{
A,
B,
C
}
Exceptions (if any)
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Cannot initialize array of non-primitive type (Parameter 'array')
System.ArgumentException: Cannot initialize array of non-primitive type (Parameter 'array')
at System.Runtime.CompilerServices.RuntimeHelpers.GetSpanDataFrom(RuntimeFieldHandle fldHandle, RuntimeTypeHandle targetTypeHandle, Int32& count)
at System.Runtime.CompilerServices.RuntimeHelpers.CreateSpan[MyEnum](RuntimeFieldHandle fldHandle)
at DoesntWorkInBlazorWasm.wwwroot.Pages.Index.OnInitialized() in C:\git\BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives\BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives\wwwroot\Pages\Index.razor:line 22
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
.NET Version
7.0.5
Anything else?
Please note, I do have .NET 8 preview 4 installed, but I am using .NET 7.0.5 (SDK 7.0.302) for this project.
PowerShell 7.3.4
PS C:\git\BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives> dotnet --version
8.0.100-preview.4.23260.5
PS C:\git\BlazorWasmIssueWithArrayInitializationWithRelevantPrimitives> dotnet --info
.NET SDK:
Version: 8.0.100-preview.4.23260.5
Commit: 2268e7b15c
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100-preview.4.23260.5\
.NET workloads installed:
There are no installed workloads to display.
Host:
Version: 8.0.0-preview.4.23259.5
Architecture: x64
Commit: 84a3d0e37e
.NET SDKs installed:
7.0.302 [C:\Program Files\dotnet\sdk]
7.0.400-preview.23225.8 [C:\Program Files\dotnet\sdk]
8.0.100-preview.4.23260.5 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0-preview.4.23260.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0-preview.4.23259.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0-preview.4.23260.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download