From b87f3753e220727a2c5a43c2b28208d990bd2d00 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 4 Oct 2022 22:15:21 +0200 Subject: [PATCH] Fix #2786: Structs with static reference type fields not recognized as unmanaged --- .../Helpers/Tester.cs | 5 +++ .../PrettyTestRunner.cs | 2 +- .../TestCases/Pretty/UnsafeCode.cs | 44 +++++++++++++++++++ .../TypeSystem/TypeSystemExtensions.cs | 2 +- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs index 5a55e10c2..3b880ca57 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs +++ b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs @@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers UseMcs5_23 = 0x2000, UseTestRunner = 0x4000, NullableEnable = 0x8000, + ReferenceUnsafe = 0x10000, UseMcsMask = UseMcs2_6_4 | UseMcs5_23, UseRoslynMask = UseRoslyn1_3_2 | UseRoslyn2_10_0 | UseRoslyn3_11_0 | UseRoslynLatest } @@ -425,6 +426,10 @@ namespace ICSharpCode.Decompiler.Tests.Helpers { references = references.Concat(new[] { "-r:\"Microsoft.VisualBasic.dll\"" }); } + if (useRoslyn && !targetNet40 && flags.HasFlag(CompilerOptions.ReferenceUnsafe)) + { + references = references.Concat(new[] { "-r:\"System.Runtime.CompilerServices.Unsafe.dll\"" }); + } string otherOptions = $"-noconfig " + $"-langversion:{languageVersion} " + $"-unsafe -o{(flags.HasFlag(CompilerOptions.Optimize) ? "+ " : "- ")}"; diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index b2542be8d..549273f56 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -376,7 +376,7 @@ namespace ICSharpCode.Decompiler.Tests [Test] public async Task UnsafeCode([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) { - await RunForLibrary(cscOptions: cscOptions); + await RunForLibrary(cscOptions: cscOptions | CompilerOptions.ReferenceUnsafe); } [Test] diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs index ce890fcd0..2bb122eb3 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs @@ -17,10 +17,54 @@ // DEALINGS IN THE SOFTWARE. using System; +#if !NET40 && ROSLYN +using System.Runtime.CompilerServices; +#endif using System.Runtime.InteropServices; namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { + internal class SizeofTest + { + private struct StructWithStaticField + { + public static object StaticObj; + + public IntPtr A; + } + + private struct UnmanagedStruct + { + public StructWithStaticField Value; + } + + private struct ManagedStruct + { + public object Obj; + } +#if CS73 + private unsafe int GenericMethod() where T : unmanaged + { + return sizeof(T); + } +#endif + + private unsafe void Test(out int s1, out int s2, out int s3) + { + s1 = 0; + s2 = 0; + s3 = 0; +#if CS73 + GenericMethod(); +#endif + s1 = sizeof(UnmanagedStruct); +#if !NET40 && ROSLYN + s2 = Unsafe.SizeOf(); + s3 = Unsafe.SizeOf(); +#endif + } + } + public class UnsafeCode { public struct SimpleStruct diff --git a/ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs b/ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs index 7115696d1..c0e109de8 100644 --- a/ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs +++ b/ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs @@ -286,7 +286,7 @@ namespace ICSharpCode.Decompiler.TypeSystem types = new HashSet(); } types.Add(type); - foreach (var f in type.GetFields()) + foreach (var f in type.GetFields(f => !f.IsStatic)) { if (types.Contains(f.Type)) {