Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

082-PartialManaged.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Reflection;
  3. using NUnit.Framework;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Burst;
  6. using Burst.Compiler.IL.Tests.Helpers;
  7. #if BURST_TESTS_ONLY
  8. using Burst.Compiler.IL.DebugInfo;
  9. using Burst.Compiler.IL.Jit;
  10. namespace Unity.Collections.LowLevel.Unsafe
  11. {
  12. internal class DisposeSentinel
  13. {
  14. }
  15. }
  16. #endif
  17. namespace Burst.Compiler.IL.Tests
  18. {
  19. /// <summary>
  20. /// Tests related to usage of partial managed objects (e.g loading null or storing null
  21. /// reference to a struct, typically used by NativeArray DisposeSentinel)
  22. /// </summary>
  23. internal class PartialManaged
  24. {
  25. #if BURST_TESTS_ONLY || ENABLE_UNITY_COLLECTIONS_CHECKS
  26. [TestCompiler]
  27. public static int TestWriteNullReference()
  28. {
  29. var element = new Element();
  30. WriteNullReference(out element.Reference);
  31. return element.Value;
  32. }
  33. [BurstDiscard]
  34. private static void WriteNullReference(out DisposeSentinel reference)
  35. {
  36. reference = null;
  37. }
  38. private struct Element
  39. {
  40. #pragma warning disable 0649
  41. public int Value;
  42. public DisposeSentinel Reference;
  43. #pragma warning restore 0649
  44. }
  45. #endif
  46. [TestCompiler]
  47. public static void AssignNullToLocalVariableClass()
  48. {
  49. MyClass x = null;
  50. #pragma warning disable 0219
  51. MyClass value = x;
  52. #pragma warning restore 0219
  53. }
  54. #if BURST_TESTS_ONLY
  55. [Test]
  56. public void TestThrowExceptionOnNonExistingMethod()
  57. {
  58. MethodInfo info = typeof(PartialManaged).GetMethod("NonExistingMethod");
  59. using JitCompiler compiler = JitCompilerHelper.CreateJitCompiler("NonExistingMethod");
  60. Assert.Throws<ArgumentNullException>(() => compiler.CompileMethod(info));
  61. }
  62. #endif
  63. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_CallingManagedMethodNotSupported)]
  64. public static int GetIndexOfCharFomString()
  65. {
  66. return "abc".IndexOf('b');
  67. }
  68. struct StructWithManaged
  69. {
  70. #pragma warning disable 0649
  71. public MyClass myClassValue;
  72. public string stringValue;
  73. public object objectValue;
  74. public float[] arrayValue;
  75. public int value;
  76. #pragma warning restore 0649
  77. }
  78. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
  79. public static int AccessClassFromStruct()
  80. {
  81. var val = new StructWithManaged();
  82. val.myClassValue.value = val.value;
  83. return val.myClassValue.value;
  84. }
  85. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
  86. public static void AccessStringFromStruct()
  87. {
  88. var val = new StructWithManaged();
  89. #pragma warning disable 0219
  90. var p = val.stringValue = "abc";
  91. #pragma warning restore 0219
  92. }
  93. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
  94. public static void AccessObjectFromStruct()
  95. {
  96. var val = new StructWithManaged();
  97. #pragma warning disable 0219
  98. var p = val.objectValue;
  99. p = new object();
  100. #pragma warning restore 0219
  101. }
  102. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
  103. public static void AccessArrayFromStruct()
  104. {
  105. var val = new StructWithManaged();
  106. var p = val.arrayValue;
  107. p[0] = val.value;
  108. }
  109. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
  110. public static int GetValueFromStructWithClassField()
  111. {
  112. var val = new StructWithManaged();
  113. val.value = 5;
  114. return val.value;
  115. }
  116. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_InstructionNewobjWithManagedTypeNotSupported)]
  117. public static void NewMyClass()
  118. {
  119. #pragma warning disable 0219
  120. var value = new MyClass();
  121. #pragma warning restore 0219
  122. }
  123. private class MyClass
  124. {
  125. public int value;
  126. }
  127. private class SomeClassWithMixedStatics
  128. {
  129. public static int SomeInt = 42;
  130. public static readonly SharedStatic<int> SomeSharedStatic = SharedStatic<int>.GetOrCreate<int>();
  131. [BurstDiscard]
  132. private static void DoSomethingWithStaticInt(ref int x) => x = SomeInt;
  133. public static int DoSomething()
  134. {
  135. ref var data = ref SomeSharedStatic.Data;
  136. DoSomethingWithStaticInt(ref data);
  137. return SomeSharedStatic.Data;
  138. }
  139. }
  140. [TestCompiler(OverrideManagedResult = 0)]
  141. public static int DoSomethingThatUsesMixedStatics()
  142. {
  143. return SomeClassWithMixedStatics.DoSomething();
  144. }
  145. private class SomeClassWithMixedStaticsWithExplicitStaticConstructor
  146. {
  147. public static int SomeInt = 42;
  148. public static readonly SharedStatic<int> SomeSharedStatic = SharedStatic<int>.GetOrCreate<int>();
  149. static SomeClassWithMixedStaticsWithExplicitStaticConstructor()
  150. {
  151. SomeInt = 1;
  152. }
  153. [BurstDiscard]
  154. private static void DoSomethingWithStaticInt(ref int x) => x = SomeInt;
  155. public static int DoSomething()
  156. {
  157. ref var data = ref SomeSharedStatic.Data;
  158. DoSomethingWithStaticInt(ref data);
  159. return SomeSharedStatic.Data;
  160. }
  161. }
  162. [TestCompiler(OverrideManagedResult = 0)]
  163. public static int DoSomethingThatUsesMixedStaticsWithExplicitStaticConstructor()
  164. {
  165. return SomeClassWithMixedStaticsWithExplicitStaticConstructor.DoSomething();
  166. }
  167. }
  168. }