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.

010-NotSupported.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using NUnit.Framework;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using Unity.Burst;
  5. using Unity.Mathematics;
  6. namespace Burst.Compiler.IL.Tests
  7. {
  8. /// <summary>
  9. /// Tests types
  10. /// </summary>
  11. [BurstCompile]
  12. internal class NotSupported
  13. {
  14. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_OnlyStaticMethodsAllowed)]
  15. public int InstanceMethod()
  16. {
  17. return 1;
  18. }
  19. [TestCompiler(1, ExpectCompilerException = true, ExpectedDiagnosticIds = new[] { DiagnosticId.ERR_CallingManagedMethodNotSupported })]
  20. public static int TestDelegate(int data)
  21. {
  22. return ProcessData(i => i + 1, data);
  23. }
  24. private static int ProcessData(Func<int, int> yo, int value)
  25. {
  26. return yo(value);
  27. }
  28. public struct HasMarshalAttribute
  29. {
  30. [MarshalAs(UnmanagedType.U1)] public bool A;
  31. }
  32. //[TestCompiler(ExpectCompilerException = true)]
  33. [TestCompiler] // Because MarshalAs is used in mathematics we cannot disable it for now
  34. public static void TestStructWithMarshalAs()
  35. {
  36. #pragma warning disable 0219
  37. var x = new HasMarshalAttribute();
  38. #pragma warning restore 0219
  39. }
  40. public struct HasMarshalAsSysIntAttribute
  41. {
  42. [MarshalAs(UnmanagedType.SysInt)] public bool A;
  43. }
  44. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_MarshalAsOnFieldNotSupported)]
  45. public static void TestStructWithMarshalAsSysInt()
  46. {
  47. #pragma warning disable 0219
  48. var x = new HasMarshalAsSysIntAttribute();
  49. #pragma warning restore 0219
  50. }
  51. [TestCompiler(42, ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_MarshalAsOnParameterNotSupported)]
  52. public static void TestMethodWithMarshalAsParameter([MarshalAs(UnmanagedType.I8)] int x)
  53. {
  54. }
  55. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_MarshalAsOnReturnTypeNotSupported)]
  56. [return: MarshalAs(UnmanagedType.I8)]
  57. public static int TestMethodWithMarshalAsReturnType()
  58. {
  59. return 42;
  60. }
  61. private static float3 a = new float3(1, 2, 3);
  62. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_LoadingFromNonReadonlyStaticFieldNotSupported)]
  63. public static bool TestStaticLoad()
  64. {
  65. var cmp = a == new float3(1, 2, 3);
  66. return cmp.x && cmp.y && cmp.z;
  67. }
  68. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_LoadingFromManagedNonReadonlyStaticFieldNotSupported)]
  69. public static void TestStaticStore()
  70. {
  71. a.x = 42;
  72. }
  73. private interface ISomething
  74. {
  75. void DoSomething();
  76. }
  77. private struct Something : ISomething
  78. {
  79. public byte A;
  80. public void DoSomething()
  81. {
  82. A = 42;
  83. }
  84. }
  85. private static ISomething something = new Something { A = 13 };
  86. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_LoadingFromManagedNonReadonlyStaticFieldNotSupported)]
  87. public static void TestStaticInterfaceStore()
  88. {
  89. something.DoSomething();
  90. }
  91. private static int i = 42;
  92. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_LoadingFromNonReadonlyStaticFieldNotSupported)]
  93. public static int TestStaticIntLoad()
  94. {
  95. return i;
  96. }
  97. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_InstructionStsfldNotSupported)]
  98. public static void TestStaticIntStore()
  99. {
  100. i = 13;
  101. }
  102. public delegate char CharbyValueDelegate(char c);
  103. #if BURST_TESTS_ONLY
  104. [BurstCompile]
  105. #endif
  106. public static char CharbyValue(char c)
  107. {
  108. return c;
  109. }
  110. public struct CharbyValueFunc : IFunctionPointerProvider
  111. {
  112. public FunctionPointer<CharbyValueDelegate> FunctionPointer;
  113. public object FromIntPtr(IntPtr ptr)
  114. {
  115. return new CharbyValueFunc() { FunctionPointer = new FunctionPointer<CharbyValueDelegate>(ptr) };
  116. }
  117. }
  118. [TestCompiler(nameof(CharbyValue), 0x1234, ExpectCompilerException = true, ExpectedDiagnosticIds = new[] { DiagnosticId.ERR_TypeNotBlittableForFunctionPointer, DiagnosticId.ERR_StructsWithNonUnicodeCharsNotSupported })]
  119. public static int TestCharbyValue(ref CharbyValueFunc fp, int i)
  120. {
  121. var c = (char)i;
  122. return fp.FunctionPointer.Invoke(c);
  123. }
  124. struct Halfs
  125. {
  126. public static readonly half3 h3_h = new half3(new half(42.0f));
  127. public static readonly half3 h3_d = new half3(0.5);
  128. public static readonly half3 h3_v2s = new half3(new half2(new half(1.0f), new half(2.0f)), new half(0.5f));
  129. public static readonly half3 h3_sv2 = new half3(new half(0.5f), new half2(new half(1.0f), new half(2.0f)));
  130. public static readonly half3 h3_v3 = new half3(new half(0.5f), new half(42.0f), new half(13.0f));
  131. }
  132. [TestCompiler]
  133. public static float TestStaticHalf3()
  134. {
  135. var result = (float3)Halfs.h3_h + Halfs.h3_d + Halfs.h3_v2s + Halfs.h3_sv2 + Halfs.h3_v3;
  136. return result.x + result.y + result.z;
  137. }
  138. [TestCompiler(42, 13, ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_AssertTypeNotSupported)]
  139. public static void TestAreEqual(int a, int b)
  140. {
  141. Assert.AreEqual(a, b, "unsupported", new object[0]);
  142. }
  143. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_InstructionLdtokenTypeNotSupported)]
  144. public static bool TestTypeof()
  145. {
  146. return typeof(int).IsPrimitive;
  147. }
  148. public class AwfulClass
  149. {
  150. public int Foo;
  151. }
  152. public struct BetterStruct
  153. {
  154. public int Foo;
  155. }
  156. public struct MixedStaticInits
  157. {
  158. public static readonly AwfulClass AC = new AwfulClass { Foo = 42 };
  159. public static readonly BetterStruct BS = new BetterStruct { Foo = 42 };
  160. }
  161. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticIds = new[] { DiagnosticId.ERR_InstructionNewobjWithManagedTypeNotSupported, DiagnosticId.ERR_ManagedStaticConstructor })]
  162. public static int TestMixedStaticInits()
  163. {
  164. return MixedStaticInits.BS.Foo;
  165. }
  166. public struct StaticArrayWrapper
  167. {
  168. private const int ArrayLength = 4;
  169. public static readonly int[] StaticArray = new int[4];
  170. static StaticArrayWrapper()
  171. {
  172. for (int i = 0; i < ArrayLength; ++i)
  173. {
  174. StaticArray[i] = i;
  175. }
  176. }
  177. }
  178. [TestCompiler]
  179. public unsafe static int TestStaticArrayWrapper()
  180. {
  181. return StaticArrayWrapper.StaticArray[0];
  182. }
  183. class NestedArrayHolder
  184. {
  185. public static readonly int4[][] SomeOffsetThing =
  186. {
  187. new[] {new int4(0), new int4(0, 0, 1, 0), new int4(0, 1, 0, 0), new int4(0, 1, 1, 0)},
  188. new[] {new int4(0), new int4(1, 0, 0, 0), new int4(0, 0, 1, 0), new int4(1, 0, 1, 0)},
  189. new[] {new int4(0), new int4(0, 1, 0, 0), new int4(1, 0, 0, 0), new int4(1, 1, 0, 0)},
  190. };
  191. }
  192. [TestCompiler]
  193. public unsafe static int TestNestedManagedArrays()
  194. {
  195. return NestedArrayHolder.SomeOffsetThing[0][0].x;
  196. }
  197. public static readonly int[,] SomeMultiDimensionalThing = new int[2, 4]
  198. {
  199. { 1, 2, 3, 4 },
  200. { -1, -2, -3, -4 },
  201. };
  202. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticIds = new[] { DiagnosticId.ERR_ConstructorNotSupported, DiagnosticId.ERR_MultiDimensionalArrayUnsupported })]
  203. public static int TestMultiDimensionalArray() => SomeMultiDimensionalThing[1, 1];
  204. }
  205. }