-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Use ArrayExpansion for [InlineArray] structs
#81254
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
Merged
Merged
Changes from 1 commit
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 hidden or 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
35 changes: 35 additions & 0 deletions
35
src/ExpressionEvaluator/CSharp/Test/ResultProvider/NetCoreTests/InlineArrayExpansionTests.cs
This file contains hidden or 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,35 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.CodeAnalysis.ExpressionEvaluator; | ||
| using Microsoft.VisualStudio.Debugger.Evaluation; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.ExpressionEvaluator.UnitTests.NetCoreTests; | ||
|
|
||
| public class InlineArrayExpansionTests : CSharpResultProviderTestBase | ||
| { | ||
| [Fact] | ||
| public void InlineArrayExpansion() | ||
| { | ||
| var hostObject = new SampleInlineArray<int>(); | ||
| for (int i = 0; i < SampleInlineArray<int>.Length; i++) | ||
| { | ||
| hostObject[i] = i; | ||
| } | ||
|
|
||
| var value = CreateDkmClrValue(hostObject, typeof(SampleInlineArray<int>), evalFlags: DkmEvaluationResultFlags.None); | ||
|
|
||
| const string rootExpr = "new SampleInlineArray<int>()"; | ||
| var evalResult = (DkmSuccessEvaluationResult)FormatResult(rootExpr, value); | ||
| Verify(evalResult, | ||
| EvalResult(rootExpr, "0,1,2,3", "Microsoft.CodeAnalysis.ExpressionEvaluator.SampleInlineArray<int>", rootExpr, DkmEvaluationResultFlags.Expandable)); | ||
|
|
||
| Verify(GetChildren(evalResult), | ||
| EvalResult("[0]", "0", "int", "(new SampleInlineArray<int>())[0]", DkmEvaluationResultFlags.None), | ||
| EvalResult("[1]", "1", "int", "(new SampleInlineArray<int>())[1]", DkmEvaluationResultFlags.None), | ||
| EvalResult("[2]", "2", "int", "(new SampleInlineArray<int>())[2]", DkmEvaluationResultFlags.None), | ||
| EvalResult("[3]", "3", "int", "(new SampleInlineArray<int>())[3]", DkmEvaluationResultFlags.None)); | ||
| } | ||
| } |
This file contains hidden or 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
72 changes: 72 additions & 0 deletions
72
src/ExpressionEvaluator/Core/Source/ResultProvider/Helpers/InlineArrayHelpers.cs
This file contains hidden or 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,72 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using BindingFlags = Microsoft.VisualStudio.Debugger.Metadata.BindingFlags; | ||
| using FieldInfo = Microsoft.VisualStudio.Debugger.Metadata.FieldInfo; | ||
| using CustomAttributeData = Microsoft.VisualStudio.Debugger.Metadata.CustomAttributeData; | ||
| using Type = Microsoft.VisualStudio.Debugger.Metadata.Type; | ||
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.ExpressionEvaluator; | ||
|
|
||
| internal static class InlineArrayHelpers | ||
| { | ||
| private const string InlineArrayAttributeName = "System.Runtime.CompilerServices.InlineArrayAttribute"; | ||
|
|
||
| public static bool IsInlineArray(Type t) | ||
| { | ||
asundheimMSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (t.IsValueType) | ||
| { | ||
| IList<CustomAttributeData> attributes = t.GetCustomAttributesData(); | ||
| foreach (var attribute in attributes) | ||
| { | ||
| if (attribute.Constructor?.DeclaringType?.FullName?.Equals(InlineArrayAttributeName, StringComparison.Ordinal) == true) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public static bool TryGetInlineArrayInfo(Type t, out int arrayLength, [NotNullWhen(true)] out Type? tElementType) | ||
| { | ||
| arrayLength = -1; | ||
| tElementType = null; | ||
|
|
||
| IList<CustomAttributeData> customAttributes = t.GetCustomAttributesData(); | ||
asundheimMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach (var attribute in customAttributes) | ||
| { | ||
| if (attribute.Constructor?.DeclaringType?.FullName?.Equals(InlineArrayAttributeName, StringComparison.Ordinal) == true) | ||
333fred marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
asundheimMSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
asundheimMSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (attribute.ConstructorArguments.Count == 1 && attribute.ConstructorArguments[0].Value is int length) | ||
| { | ||
| arrayLength = length; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Inline arrays must have length > 0 | ||
| if (arrayLength <= 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); | ||
| if (fields.Length == 1) | ||
| { | ||
| tElementType = fields[0].FieldType; | ||
| } | ||
| else | ||
| { | ||
| // Inline arrays must have exactly one field | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
src/ExpressionEvaluator/Core/Test/ResultProvider/SampleInlineArray.cs
This file contains hidden or 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,37 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using Microsoft.VisualStudio.Debugger.Evaluation; | ||
| using Microsoft.VisualStudio.Debugger.Evaluation.ClrCompilation; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.ExpressionEvaluator; | ||
|
|
||
| #if NET8_0_OR_GREATER | ||
|
|
||
| /// <summary> | ||
| /// Reflection APIs are inadequate to introspect inline arrays as they box the value type, making | ||
| /// it impossible to take a ref to the first element field and read elements out of it. | ||
| /// This definition is intended to be used when testing inline arrays. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// To support testing new <typeparamref name="T"/> types, | ||
| /// add an appropriate cast in <see cref="DkmClrValue.GetArrayElement(int[], DkmInspectionContext)"/> | ||
| /// </remarks> | ||
| [InlineArray(Length)] | ||
| [DebuggerDisplay("{ToString(),nq}")] | ||
| internal struct SampleInlineArray<T> | ||
| { | ||
| public const int Length = 4; | ||
| public T _elem0; | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return string.Join(",", MemoryMarshal.CreateSpan(ref _elem0, Length).ToArray()); | ||
| } | ||
| } | ||
|
|
||
| #endif |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.