Browse Source

Fix #2786: Structs with static reference type fields not recognized as unmanaged

pull/2798/head
Siegfried Pammer 3 years ago
parent
commit
b87f3753e2
  1. 5
      ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
  2. 2
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 44
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  4. 2
      ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

5
ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
UseMcs5_23 = 0x2000, UseMcs5_23 = 0x2000,
UseTestRunner = 0x4000, UseTestRunner = 0x4000,
NullableEnable = 0x8000, NullableEnable = 0x8000,
ReferenceUnsafe = 0x10000,
UseMcsMask = UseMcs2_6_4 | UseMcs5_23, UseMcsMask = UseMcs2_6_4 | UseMcs5_23,
UseRoslynMask = UseRoslyn1_3_2 | UseRoslyn2_10_0 | UseRoslyn3_11_0 | UseRoslynLatest 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\"" }); 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 " + string otherOptions = $"-noconfig " +
$"-langversion:{languageVersion} " + $"-langversion:{languageVersion} " +
$"-unsafe -o{(flags.HasFlag(CompilerOptions.Optimize) ? "+ " : "- ")}"; $"-unsafe -o{(flags.HasFlag(CompilerOptions.Optimize) ? "+ " : "- ")}";

2
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -376,7 +376,7 @@ namespace ICSharpCode.Decompiler.Tests
[Test] [Test]
public async Task UnsafeCode([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) public async Task UnsafeCode([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{ {
await RunForLibrary(cscOptions: cscOptions); await RunForLibrary(cscOptions: cscOptions | CompilerOptions.ReferenceUnsafe);
} }
[Test] [Test]

44
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -17,10 +17,54 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System; using System;
#if !NET40 && ROSLYN
using System.Runtime.CompilerServices;
#endif
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty 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<T>() 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<UnmanagedStruct>();
#endif
s1 = sizeof(UnmanagedStruct);
#if !NET40 && ROSLYN
s2 = Unsafe.SizeOf<UnmanagedStruct>();
s3 = Unsafe.SizeOf<ManagedStruct>();
#endif
}
}
public class UnsafeCode public class UnsafeCode
{ {
public struct SimpleStruct public struct SimpleStruct

2
ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

@ -286,7 +286,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
types = new HashSet<IType>(); types = new HashSet<IType>();
} }
types.Add(type); types.Add(type);
foreach (var f in type.GetFields()) foreach (var f in type.GetFields(f => !f.IsStatic))
{ {
if (types.Contains(f.Type)) if (types.Contains(f.Type))
{ {

Loading…
Cancel
Save