暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

041-ControlFlowsTryCatchFinally.cs 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using System.Runtime.CompilerServices;
  8. namespace Burst.Compiler.IL.Tests
  9. {
  10. internal class ControlFlowsTryCatchFinally
  11. {
  12. [TestCompiler(-10)]
  13. [TestCompiler(0)]
  14. [TestCompiler(10)]
  15. public static int TryFinallySimple(int i)
  16. {
  17. try
  18. {
  19. if (i == 0) // case 0
  20. {
  21. return 1;
  22. }
  23. else if (i > 0) // case 10
  24. {
  25. i = i * 2;
  26. }
  27. else
  28. {
  29. i = i * 3; // case -10
  30. }
  31. }
  32. finally
  33. {
  34. i = i + 1;
  35. }
  36. return i; // both case 10 and -10
  37. }
  38. static void Oof()
  39. {
  40. }
  41. [TestCompiler]
  42. public static void TryFinallyFirstBlock()
  43. {
  44. try
  45. {
  46. }
  47. finally
  48. {
  49. Oof();
  50. }
  51. }
  52. static int MagicA(int b, int f, int h, CustomBuffer s)
  53. {
  54. return b+s.Hash()+f-h;
  55. }
  56. static bool MagicB(int c,out int t)
  57. {
  58. t = 0;
  59. if (c>10)
  60. {
  61. t = c;
  62. return true;
  63. }
  64. return false;
  65. }
  66. // This test catches an issue with the de-stackifier. (see ILBuilder.cs:1254 (flushStack))
  67. // Needs to be unoptimised to trigger
  68. [TestCompiler(0)]
  69. [TestCompiler(99)]
  70. [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
  71. public static int TryUnbalancedFinally(int i)
  72. {
  73. // this if is required to force the destackifier to process the final block, before processing the block the contains the endfinally
  74. if (i == 99)
  75. {
  76. return default;
  77. }
  78. int resultB = i;
  79. using var buffer = new CustomBuffer(32);
  80. return resultB + MagicA(i,
  81. MagicB(i*2, out var r) ? r : default,
  82. MagicB(i, out var t) ? t : default,
  83. buffer);
  84. }
  85. [TestCompiler(-3)]
  86. [TestCompiler(0)]
  87. [TestCompiler(3)]
  88. public static int TryFinallyComplex1(int i)
  89. {
  90. try
  91. {
  92. try
  93. {
  94. if (i == 0)
  95. {
  96. return i - 1;
  97. }
  98. i += 3;
  99. }
  100. finally
  101. {
  102. if (i == 0) // case i: -3
  103. {
  104. i = 1;
  105. }
  106. else
  107. {
  108. i = i * 10; // case i: 3
  109. }
  110. }
  111. }
  112. finally
  113. {
  114. i = i * 2; // both -3 and 3
  115. }
  116. return i + 1;
  117. }
  118. [TestCompiler(-10)]
  119. [TestCompiler(0)] // case 0
  120. [TestCompiler(10)]
  121. public static int TryFinallyComplex2(int i)
  122. {
  123. // First block of nested try/catch
  124. try
  125. {
  126. try
  127. {
  128. if (i == 0) // case 0
  129. {
  130. return i - 1;
  131. }
  132. i = i * 2;
  133. }
  134. finally
  135. {
  136. i++;
  137. }
  138. }
  139. finally
  140. {
  141. i = i * 3;
  142. }
  143. // Second block of nested try/catch
  144. try
  145. {
  146. i = i - 2;
  147. try
  148. {
  149. if (i < 0) // case -10
  150. {
  151. return i * 5;
  152. }
  153. i += 3; // case 10
  154. }
  155. finally
  156. {
  157. i += 11;
  158. }
  159. }
  160. finally
  161. {
  162. i = i * 3;
  163. }
  164. return i + 1; // case 10
  165. }
  166. [TestCompiler(0)]
  167. [TestCompiler(1)]
  168. [TestCompiler(10)]
  169. [TestCompiler(20)]
  170. public static int TryFinallyComplex3(int x)
  171. {
  172. bool k = true;
  173. int num = 0;
  174. try
  175. {
  176. while (k)
  177. {
  178. if (x < 10)
  179. {
  180. num |= 2;
  181. try
  182. {
  183. if (x == 1) return num;
  184. }
  185. finally
  186. {
  187. k = false;
  188. }
  189. continue;
  190. }
  191. num |= 1;
  192. try
  193. {
  194. if (x == 20) return num;
  195. }
  196. finally
  197. {
  198. k = false;
  199. }
  200. }
  201. }
  202. finally
  203. {
  204. num |= 4;
  205. }
  206. return num;
  207. }
  208. [TestCompiler]
  209. public static int TryUsingDispose()
  210. {
  211. using (var buffer = new CustomBuffer(32))
  212. {
  213. return buffer.Hash();
  214. }
  215. }
  216. [TestCompiler]
  217. public static int ForEachTryFinally()
  218. {
  219. int hashCode = 0;
  220. foreach (var value in new RangeEnumerable(1, 100))
  221. {
  222. hashCode = (hashCode * 397) ^ value;
  223. }
  224. return hashCode;
  225. }
  226. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_CatchConstructionNotSupported)]
  227. public static int TryCatch()
  228. {
  229. try
  230. {
  231. return default(int);
  232. }
  233. catch (InvalidOperationException)
  234. {
  235. return 1;
  236. }
  237. }
  238. private unsafe struct CustomBuffer : IDisposable
  239. {
  240. private readonly int _size;
  241. private byte* _buffer;
  242. public CustomBuffer(int size)
  243. {
  244. _size = size;
  245. _buffer = (byte*)UnsafeUtility.Malloc(size, 4, Allocator.Persistent);
  246. for (int i = 0; i < size; i++)
  247. {
  248. _buffer[i] = (byte)(i + 1);
  249. }
  250. }
  251. public int Hash()
  252. {
  253. int hashCode = _size;
  254. for (int i = 0; i < _size; i++)
  255. {
  256. hashCode = (hashCode * 397) ^ (byte)_buffer[i];
  257. }
  258. return hashCode;
  259. }
  260. public unsafe void Dispose()
  261. {
  262. if (_buffer != null)
  263. {
  264. UnsafeUtility.Free(_buffer, Allocator.Persistent);
  265. _buffer = (byte*) 0;
  266. }
  267. }
  268. }
  269. private struct RangeEnumerable : IEnumerable<int>
  270. {
  271. private readonly int _from;
  272. private readonly int _to;
  273. public RangeEnumerable(int from, int to)
  274. {
  275. _from = @from;
  276. _to = to;
  277. }
  278. public Enumerator GetEnumerator()
  279. {
  280. return new Enumerator();
  281. }
  282. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  283. {
  284. return GetEnumerator();
  285. }
  286. IEnumerator IEnumerable.GetEnumerator()
  287. {
  288. return GetEnumerator();
  289. }
  290. public struct Enumerator : IEnumerator<int>
  291. {
  292. private readonly int _from;
  293. private readonly int _to;
  294. public Enumerator(int from, int to)
  295. {
  296. _from = @from;
  297. _to = to;
  298. Current = -1;
  299. }
  300. public void Dispose()
  301. {
  302. // nothing to do
  303. }
  304. public bool MoveNext()
  305. {
  306. if (Current < 0)
  307. {
  308. Current = _from;
  309. return true;
  310. }
  311. int nextIndex = Current + 1;
  312. if (nextIndex >= _from && nextIndex <= _to)
  313. {
  314. Current = nextIndex;
  315. return true;
  316. }
  317. return false;
  318. }
  319. public void Reset()
  320. {
  321. }
  322. public int Current { get; private set; }
  323. object IEnumerator.Current => Current;
  324. }
  325. }
  326. }
  327. }