-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add array range support #179
Conversation
looks like a bug in roslyn. can you try rains a bug in https://github.com/dotnet/roslyn |
Honestly, I have no idea how to describe this bug. I'm not sure they will love the reference to this PR as a repro... |
This is the exception, and it's thrown by all the projects with
|
then here is a minimal repro https://github.com/SimonCropp/RuntimeHelpersRepro |
/// <param name="fldHandle">A field handle that specifies the location of the data used to initialize the array</param> | ||
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimehelpers.initializearray")] | ||
[Intrinsic] | ||
public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimonCropp, after adding this method, everything compiles and tests are green. But I have a few questions:
-
This method should already exist in all the TFMs. Can it cause runtime errors?
-
While all tests run fine, the ReSharper runner shows the following errors. Any idea how to fix them? Is it safe to ignore?
-
I created 2 partial
RuntimeHelpers
classes, asInitializeArray
is not necessarily related to theIndexRange
feature and theoretically can be required by other features added in the future. Is it ok, or do you prefer a single class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try this
// <auto-generated />
#if (NET46X && FeatureValueTuple) || NET47X || NET48X || NETSTANDARD2_0 || NETCOREAPP2X
namespace System.Runtime.CompilerServices;
using System.Reflection;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;
/// <summary>
/// Provides a set of static methods and properties that provide support for compilers.
/// This class cannot be inherited.
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
#if PolyPublic
public
#endif
static partial class RuntimeHelpers
{
static readonly Action<Array,RuntimeFieldHandle> initializeArrayAction;
static RuntimeHelpers()
{
//CompilationRelaxations is co located with RuntimeHelpers
var runtimeHelpersType = typeof(CompilationRelaxations)
.Assembly
.GetType("System.Runtime.CompilerServices.RuntimeHelpers");
var initializeArrayMethod = runtimeHelpersType.GetMethod(
"InitializeArray",
BindingFlags.Static | BindingFlags.Public,
null,
[typeof(Array), typeof(RuntimeFieldHandle)],
null)!;
initializeArrayAction = (Action<Array,RuntimeFieldHandle>)initializeArrayMethod.CreateDelegate(typeof(Action<Array,RuntimeFieldHandle>));
}
/// <summary>
/// Provides a fast way to initialize an array from data that is stored in a module.
/// </summary>
/// <param name="array">The array to be initialized.</param>
/// <param name="fldHandle">A field handle that specifies the location of the data used to initialize the array</param>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimehelpers.initializearray")]
public static void InitializeArray(Array array, RuntimeFieldHandle fldHandle) =>
initializeArrayAction(array, fldHandle);
}
#endif
and then we dont need IntrinsicAttribute
|
||
Array.Copy(array, offset, dest, 0, length); | ||
|
||
return dest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you prefer this implementation or one that will be released with .NET 9?
https://github.com/dotnet/runtime/blob/v9.0.0-preview.3.24172.9/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs#L47-L54
ok. going to merge this one. i will clean up the last remaining things after. thanks for all the research |
Fixes #178
@SimonCropp I tried adding
RuntimeHelpers.GetSubArray
(the pre .NET 9 version) to support array range, but for some reason, I'm getting a compiler error