123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Collections.LowLevel.Unsafe;
- using System.Runtime.CompilerServices;
-
- namespace Burst.Compiler.IL.Tests
- {
- internal class ControlFlowsTryCatchFinally
- {
- [TestCompiler(-10)]
- [TestCompiler(0)]
- [TestCompiler(10)]
- public static int TryFinallySimple(int i)
- {
- try
- {
- if (i == 0) // case 0
- {
- return 1;
- }
- else if (i > 0) // case 10
- {
- i = i * 2;
- }
- else
- {
- i = i * 3; // case -10
- }
- }
- finally
- {
- i = i + 1;
- }
-
- return i; // both case 10 and -10
- }
-
- static void Oof()
- {
- }
-
- [TestCompiler]
- public static void TryFinallyFirstBlock()
- {
- try
- {
- }
- finally
- {
- Oof();
- }
- }
-
- static int MagicA(int b, int f, int h, CustomBuffer s)
- {
- return b+s.Hash()+f-h;
- }
-
- static bool MagicB(int c,out int t)
- {
- t = 0;
- if (c>10)
- {
- t = c;
- return true;
- }
- return false;
- }
-
- // This test catches an issue with the de-stackifier. (see ILBuilder.cs:1254 (flushStack))
- // Needs to be unoptimised to trigger
- [TestCompiler(0)]
- [TestCompiler(99)]
- [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
- public static int TryUnbalancedFinally(int i)
- {
- // this if is required to force the destackifier to process the final block, before processing the block the contains the endfinally
- if (i == 99)
- {
- return default;
- }
-
- int resultB = i;
-
- using var buffer = new CustomBuffer(32);
-
- return resultB + MagicA(i,
- MagicB(i*2, out var r) ? r : default,
- MagicB(i, out var t) ? t : default,
- buffer);
- }
-
-
- [TestCompiler(-3)]
- [TestCompiler(0)]
- [TestCompiler(3)]
- public static int TryFinallyComplex1(int i)
- {
- try
- {
- try
- {
- if (i == 0)
- {
- return i - 1;
- }
-
- i += 3;
- }
- finally
- {
- if (i == 0) // case i: -3
- {
- i = 1;
- }
- else
- {
- i = i * 10; // case i: 3
- }
- }
- }
- finally
- {
- i = i * 2; // both -3 and 3
- }
-
- return i + 1;
- }
-
- [TestCompiler(-10)]
- [TestCompiler(0)] // case 0
- [TestCompiler(10)]
- public static int TryFinallyComplex2(int i)
- {
- // First block of nested try/catch
- try
- {
- try
- {
- if (i == 0) // case 0
- {
- return i - 1;
- }
-
- i = i * 2;
- }
- finally
- {
- i++;
- }
- }
- finally
- {
- i = i * 3;
- }
-
- // Second block of nested try/catch
- try
- {
- i = i - 2;
-
- try
- {
- if (i < 0) // case -10
- {
- return i * 5;
- }
-
- i += 3; // case 10
- }
- finally
- {
- i += 11;
- }
- }
- finally
- {
- i = i * 3;
- }
-
- return i + 1; // case 10
- }
-
-
- [TestCompiler(0)]
- [TestCompiler(1)]
- [TestCompiler(10)]
- [TestCompiler(20)]
- public static int TryFinallyComplex3(int x)
- {
- bool k = true;
- int num = 0;
- try
- {
- while (k)
- {
- if (x < 10)
- {
- num |= 2;
- try
- {
- if (x == 1) return num;
- }
- finally
- {
- k = false;
- }
-
- continue;
- }
-
- num |= 1;
- try
- {
- if (x == 20) return num;
- }
- finally
- {
- k = false;
- }
- }
- }
- finally
- {
- num |= 4;
- }
-
- return num;
- }
-
- [TestCompiler]
- public static int TryUsingDispose()
- {
- using (var buffer = new CustomBuffer(32))
- {
- return buffer.Hash();
- }
- }
-
- [TestCompiler]
- public static int ForEachTryFinally()
- {
- int hashCode = 0;
- foreach (var value in new RangeEnumerable(1, 100))
- {
- hashCode = (hashCode * 397) ^ value;
- }
- return hashCode;
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_CatchConstructionNotSupported)]
- public static int TryCatch()
- {
- try
- {
- return default(int);
- }
- catch (InvalidOperationException)
- {
- return 1;
- }
- }
-
- private unsafe struct CustomBuffer : IDisposable
- {
- private readonly int _size;
- private byte* _buffer;
-
- public CustomBuffer(int size)
- {
- _size = size;
- _buffer = (byte*)UnsafeUtility.Malloc(size, 4, Allocator.Persistent);
- for (int i = 0; i < size; i++)
- {
- _buffer[i] = (byte)(i + 1);
- }
- }
-
- public int Hash()
- {
- int hashCode = _size;
- for (int i = 0; i < _size; i++)
- {
- hashCode = (hashCode * 397) ^ (byte)_buffer[i];
- }
- return hashCode;
- }
-
- public unsafe void Dispose()
- {
- if (_buffer != null)
- {
- UnsafeUtility.Free(_buffer, Allocator.Persistent);
- _buffer = (byte*) 0;
- }
- }
- }
-
- private struct RangeEnumerable : IEnumerable<int>
- {
- private readonly int _from;
- private readonly int _to;
-
- public RangeEnumerable(int from, int to)
- {
- _from = @from;
- _to = to;
- }
-
- public Enumerator GetEnumerator()
- {
- return new Enumerator();
- }
-
- IEnumerator<int> IEnumerable<int>.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- public struct Enumerator : IEnumerator<int>
- {
- private readonly int _from;
- private readonly int _to;
-
- public Enumerator(int from, int to)
- {
- _from = @from;
- _to = to;
- Current = -1;
- }
-
- public void Dispose()
- {
- // nothing to do
- }
-
- public bool MoveNext()
- {
- if (Current < 0)
- {
- Current = _from;
- return true;
- }
-
- int nextIndex = Current + 1;
- if (nextIndex >= _from && nextIndex <= _to)
- {
- Current = nextIndex;
- return true;
- }
- return false;
- }
-
- public void Reset()
- {
- }
-
- public int Current { get; private set; }
-
- object IEnumerator.Current => Current;
- }
- }
- }
- }
|