Repository navigation
Closed
Labels
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
Activity
stephentoub commentedon May 29, 2023
@lambdageek, could you take a look?
lambdageek commentedon May 29, 2023
Works with the net8 runtime, fails with the net7 runtime
Tomorrow I'll try to find the PR that fixed it...
codemonkey85 commentedon May 31, 2023
@lambdageek Thanks for looking into this. Is there a possibility this can be fixed in an update to .NET 7?
lambdageek commentedon Jun 1, 2023
Still haven't been able to track down why it is working in net8 and not in net7. The roslyn change dotnet/roslyn#61414 is complicating things...
lambdageek commentedon Jun 1, 2023
Oh, I see. it's due to #81695 and #82093
Which both call
mini_get_underlying_typeon the type argument of theRuntimeHelpers.CreateSpan<T>call.The non-intrinsic implementation doesn't have the underlying type logic. So (if it was possible to call ref-struct methods using reflection) calling CreateSpan indirectly will probably still assert in net8.
lambdageek commentedon Jun 1, 2023
Ok, so this throws on net8 (and probably on net7):
So now I can add a one-line fix for net 8 and backport to net7
[mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom
[mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom
codemonkey85 commentedon Jun 1, 2023
Amazing. Thank you so much @lambdageek!
[mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom (#87019)
[release/7.0-staging] [mono] Use underlying type in RuntimeHelpers.Ge…
codemonkey85 commentedon Jun 14, 2023
@lambdageek Just wanted to check in - looks like this fix didn't make it for .NET SDK 7.0.7 / 7.0.304?
lambdageek commentedon Jun 14, 2023
I think that's right. For non-security related issues it can sometimes take a month or so for the changes to make it to a servicing release, depending on when the changes go into the staging branchm, and from the staging branch to the release branch