-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Cannot initialize array of non-primitive type #86865
Comments
@lambdageek, could you take a look? |
Works with the net8 runtime, fails with the net7 runtime |
@lambdageek Thanks for looking into this. Is there a possibility this can be fixed in an update to .NET 7? |
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... |
Oh, I see. it's due to #81695 and #82093 Which both 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. |
Ok, so this throws on net8 (and probably on net7): using System.Reflection;
ReadOnlySpan<MyEnum> myEnums = new[]
{
MyEnum.A,
MyEnum.B,
MyEnum.C
};
Console.WriteLine(string.Join(", ", myEnums.ToArray()));
var types = new Type[] {
typeof(RuntimeFieldHandle),
typeof(RuntimeTypeHandle),
typeof(int).MakeByRefType(),
};
var mi = typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("GetSpanDataFrom", BindingFlags.Static | BindingFlags.NonPublic, types);
Console.WriteLine (mi);
var pid = typeof(MyEnum).Assembly.GetType("<PrivateImplementationDetails>");
Console.WriteLine (pid);
var fis = pid.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
var fi = fis[0];
Console.WriteLine (fi);
var parms = new object[] {
fi.FieldHandle,
typeof(MyEnum).TypeHandle,
new int()
};
var result = mi.Invoke(null, parms);
Console.WriteLine (result);
Console.WriteLine (parms[2]);
enum MyEnum
{
A,
B,
C
} So now I can add a one-line fix for net 8 and backport to net7 |
Make it work correctly for spans of enums Fixes dotnet#86865 Note that in net8 RuntimeHelpers.CreateSpan<T> is an intrinsic, so GetSpanDataFrom is never called directly. But in net7 CreateSpan is not intrinsified on Mono, so the underlying method really does get called.
Make it work correctly for spans of enums Fixes #86865 Note that in net8 RuntimeHelpers.CreateSpan<T> is an intrinsic, so GetSpanDataFrom is never called directly. But in net7 CreateSpan is not intrinsified on Mono, so the underlying method really does get called.
Amazing. Thank you so much @lambdageek! |
* [mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom Make it work correctly for spans of enums Fixes #86865 Note that in net8 RuntimeHelpers.CreateSpan<T> is an intrinsic, so GetSpanDataFrom is never called directly. But in net7 CreateSpan is not intrinsified on Mono, so the underlying method really does get called. * test: Print all hidden field names if we can't find the right one
…tSpanDataFrom (#87021) * [mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom Make it work correctly for spans of enums Fixes #86865 Note that in net8 RuntimeHelpers.CreateSpan<T> is an intrinsic, so GetSpanDataFrom is never called directly. But in net7 CreateSpan is not intrinsified on Mono, so the underlying method really does get called. * test: Print all hidden field names if we can't find the right one Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
@lambdageek Just wanted to check in - looks like this fix didn't make it for .NET SDK 7.0.7 / 7.0.304? |
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 |
Is there an existing issue for this?
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:
Exceptions (if any)
.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.
The text was updated successfully, but these errors were encountered: