No Description
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 5.6KB

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