-
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
[release/7.0-staging] [mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom #87021
Merged
lambdageek
merged 4 commits into
release/7.0-staging
from
backport/pr-87019-to-release/7.0-staging
Jun 7, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace test86865; | ||
|
||
public class test86865 | ||
{ | ||
public static int Main() | ||
{ | ||
|
||
// Regression test for https://github.com/dotnet/runtime/issues/86865 | ||
// Verify that the RuntimeHelpers.GetSpanDataFrom method underlying RuntimeHelpers.CreateSpan<T> | ||
// works correctly with enums. | ||
|
||
ReadOnlySpan<MyEnum> myEnums = new[] | ||
{ | ||
MyEnum.A, | ||
MyEnum.B, | ||
MyEnum.C, | ||
MyEnum.B, | ||
MyEnum.C, | ||
}; | ||
|
||
if (string.Join(", ", myEnums.ToArray()) != "A, B, C, B, C") | ||
return 1; | ||
|
||
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); | ||
if (mi == null) | ||
return 2; | ||
|
||
var pid = typeof(MyEnum).Assembly.GetType("<PrivateImplementationDetails>"); | ||
if (pid == null) | ||
return 3; | ||
|
||
var fi = pid.GetField("0B77DC554B4A81403D62BE25FB5404020AD451151D4203D544BF60E3FEDBD8AE", BindingFlags.Static | BindingFlags.NonPublic); | ||
if (fi == null) | ||
{ | ||
Console.WriteLine("Could not find the expected array data in <PrivateImplementationDetails>. The available static non-public fields are:"); | ||
foreach (var f in pid.GetFields(BindingFlags.Static | BindingFlags.NonPublic)) { | ||
Console.WriteLine($" - '{f}'"); | ||
} | ||
return 4; | ||
} | ||
|
||
var parms = new object[] { | ||
fi.FieldHandle, | ||
typeof(MyEnum).TypeHandle, | ||
new int() | ||
}; | ||
var result = mi.Invoke(null, parms); | ||
if (result == null) | ||
return 6; | ||
if ((int)parms[2] != myEnums.Length) | ||
return 7; | ||
|
||
return 100; | ||
} | ||
} | ||
|
||
enum MyEnum | ||
{ | ||
A, | ||
B, | ||
C | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="test86865.cs" /> | ||
</ItemGroup> | ||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
just curious, is this some kind of internal hash? is it stable?
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.
I believe it's a hash of the constant value. It's fairly stable - in local testing the same field is generated with the net8.0 and net7.0 SDKs when targeting both the net8.0 and net7.0 runtimes. In CI testing, for some reason the 7.0 branch creates a field with a name that is 1 character longer (ie that final
"E"
is here on therelease/7.0-staging
version of the test, but not in themain
branch).I could just blindly take the first matching field in the assembly (and maybe do some minimal checking of its attributes to make sure it's plausibly the correct one), but it seemed more straightforward to just hardcode the name and to return a distinct failure code and a print out of the available fields.