Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mono / mini / gshared.cs
index f545160ec42d6aee9d6eee5ec2314b5ff9e2f3bb..b0b95373fa7e279bcd127e38ced6db4dc5d284a1 100644 (file)
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
 
@@ -212,6 +213,17 @@ public class Tests
                return 0;
        }
 
+       public static int test_0_unbox_any_enum () {
+               IFaceUnbox iface = new ClassUnbox ();
+               AnEnum res = iface.Unbox<AnEnum, int> (AnEnum.One, 0, 1);
+               if (res != AnEnum.Two)
+                       return 1;
+               res = iface.Unbox<AnEnum, int> (AnEnum.One, 0, AnEnum.Two);
+               if (res != AnEnum.Two)
+                       return 2;
+               return 0;
+       }
+
        [MethodImplAttribute (MethodImplOptions.NoInlining)]
        static void ldfld_nongeneric<T> (GFoo<T>[] foo, int[] arr) {
                arr [0] = foo [0].i;
@@ -914,7 +926,8 @@ public class Tests
        }
 
        enum AnEnum {
-               One
+               One,
+               Two
        };
 
        public static int test_0_constrained_tostring () {
@@ -961,6 +974,26 @@ public class Tests
                return 0;
        }
 
+       interface IGetType {
+               Type gettype<T, T2>(T t, T2 t2);
+       }
+
+       public class CGetType : IGetType {
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               public Type gettype<T, T2>(T t, T2 t2) {
+                       return t.GetType ();
+               }
+       }
+
+       public static int test_0_constrained_gettype () {
+               IGetType c = new CGetType ();
+               if (c.gettype<int, int> (1, 1) != typeof (int))
+                       return 1;
+               if (c.gettype<string, int> ("A", 1) != typeof (string))
+                       return 2;
+               return 0;
+       }
+
        struct Pair<T1, T2> {
                public T1 First;
                public T2 Second;
@@ -1218,6 +1251,11 @@ public class Tests
                void foo_ref_arg (string s);
        }
 
+       interface IConstrained<T3> {
+               void foo_gsharedvt_arg (T3 s);
+               T3 foo_gsharedvt_ret (T3 s);
+       }
+
        static object constrained_res;
 
        struct ConsStruct : IConstrained {
@@ -1244,9 +1282,31 @@ public class Tests
                }
        }
 
+       struct ConsStruct<T> : IConstrained<T> {
+               public void foo_gsharedvt_arg (T s) {
+                       constrained_res = s;
+               }
+
+               public T foo_gsharedvt_ret (T s) {
+                       return s;
+               }
+       }
+
+       struct ConsStructThrow : IConstrained {
+               public void foo () {
+                       throw new Exception ();
+               }
+
+               public void foo_ref_arg (string s) {
+               }
+       }
+
        interface IFaceConstrained {
                void constrained_void_iface_call<T, T2>(T t, T2 t2) where T2 : IConstrained;
                void constrained_void_iface_call_ref_arg<T, T2>(T t, T2 t2) where T2 : IConstrained;
+               void constrained_void_iface_call_gsharedvt_arg<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T>;
+               T constrained_iface_call_gsharedvt_ret<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T>;
+               T2 constrained_normal_call<T, T2>(T t, T2 t2) where T2 : VClass;
        }
 
        class ClassConstrained : IFaceConstrained {
@@ -1259,6 +1319,28 @@ public class Tests
                public void constrained_void_iface_call_ref_arg<T, T2>(T t, T2 t2) where T2 : IConstrained {
                        t2.foo_ref_arg ("A");
                }
+
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               public void constrained_void_iface_call_gsharedvt_arg<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T> {
+                       t2.foo_gsharedvt_arg (t);
+               }
+
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               public T constrained_iface_call_gsharedvt_ret<T, T2, T3>(T t, T2 t2, T3 t3) where T2 : IConstrained<T> {
+                       return t2.foo_gsharedvt_ret (t);
+               }
+
+               [MethodImplAttribute (MethodImplOptions.NoInlining)]
+               public T2 constrained_normal_call<T, T2>(T t, T2 t2) where T2 : VClass {
+                       /* This becomes a constrained call even through 't2' is forced to be a reference type by the constraint */
+                       return (T2)t2.foo (5);
+               }
+       }
+
+       class VClass {
+               public virtual VClass foo (int i) {
+                       return this;
+               }
        }
 
        public static int test_0_constrained_void_iface_call () {
@@ -1284,6 +1366,60 @@ public class Tests
                return 0;
        }
 
+       public static int test_0_constrained_eh () {
+               var s2 = new ConsStructThrow () { };
+               try {
+                       IFaceConstrained c = new ClassConstrained ();
+                       c.constrained_void_iface_call<int, ConsStructThrow> (1, s2);
+                       return 1;
+               } catch (Exception) {
+                       return 0;
+               }
+       }
+
+       public static int test_0_constrained_void_iface_call_gsharedvt_arg () {
+               // This tests constrained calls through interfaces with one gsharedvt arg, like IComparable<T>.CompareTo ()
+               IFaceConstrained c = new ClassConstrained ();
+
+               var s = new ConsStruct<int> ();
+               constrained_res = null;
+               c.constrained_void_iface_call_gsharedvt_arg<int, ConsStruct<int>, int> (42, s, 55);
+               if (!(constrained_res is int) || ((int)constrained_res) != 42)
+                       return 1;
+
+               var s2 = new ConsStruct<string> ();
+               constrained_res = null;
+               c.constrained_void_iface_call_gsharedvt_arg<string, ConsStruct<string>, int> ("A", s2, 55);
+               if (!(constrained_res is string) || ((string)constrained_res) != "A")
+                       return 2;
+
+               return 0;
+       }
+
+       public static int test_0_constrained_iface_call_gsharedvt_ret () {
+               IFaceConstrained c = new ClassConstrained ();
+
+               var s = new ConsStruct<int> ();
+               int ires = c.constrained_iface_call_gsharedvt_ret<int, ConsStruct<int>, int> (42, s, 55);
+               if (ires != 42)
+                       return 1;
+
+               var s2 = new ConsStruct<string> ();
+               string sres = c.constrained_iface_call_gsharedvt_ret<string, ConsStruct<string>, int> ("A", s2, 55);
+               if (sres != "A")
+                       return 2;
+
+               return 0;
+       }
+
+       public static int test_0_constrained_normal_call () {
+               IFaceConstrained c = new ClassConstrained ();
+
+               var o = new VClass ();
+               var res = c.constrained_normal_call<int, VClass> (1, o);
+               return res == o ? 0 : 1;
+       }
+
        public static async Task<T> FooAsync<T> (int i, int j) {
                Task<int> t = new Task<int> (delegate () { return 42; });
                var response = await t;
@@ -1293,7 +1429,8 @@ public class Tests
        [MethodImplAttribute (MethodImplOptions.NoInlining)]
        public static void call_async<T> (int i, int j) {
                Task<T> t = FooAsync<T> (1, 2);
-               t.RunSynchronously ();
+               // FIXME: This doesn't work
+               //t.RunSynchronously ();
        }
 
        // In AOT mode, the async infrastructure depends on gsharedvt methods
@@ -1301,6 +1438,27 @@ public class Tests
                call_async<string> (1, 2);
                return 0;
        }
+
+       public static int test_0_array_helper_gsharedvt () {
+               var arr = new AnEnum [16];
+               var c = new ReadOnlyCollection<AnEnum> (arr);
+               return c.Contains (AnEnum.Two) == false ? 0 : 1;
+       }
+}
+
+// #13191
+public class MobileServiceCollection<TTable, TCol>
+{
+       public async Task<int> LoadMoreItemsAsync(int count = 0) {
+               await Task.Delay (1000);
+               int results = await ProcessQueryAsync ();
+               return results;
+       }
+
+       protected async virtual Task<int> ProcessQueryAsync() {
+               await Task.Delay (1000);
+               throw new Exception ();
+       }
 }
 
 #if !MOBILE