[interpreter] if unboxing is required on virtcall, point into object instead of copying
[mono.git] / mono / mini / generics.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.CompilerServices;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 #if __MOBILE__
9 class GenericsTests
10 #else
11 class Tests
12 #endif
13 {
14         struct TestStruct {
15                 public int i;
16                 public int j;
17
18                 public TestStruct (int i, int j) {
19                         this.i = i;
20                         this.j = j;
21                 }
22         }
23
24 #if !__MOBILE__
25         class Enumerator <T> : MyIEnumerator <T> {
26                 T MyIEnumerator<T>.Current {
27                         get {
28                                 return default(T);
29                         }
30                 }
31
32                 bool MyIEnumerator<T>.MoveNext () {
33                         return true;
34                 }
35         }
36
37         class Comparer <T> : IComparer <T> {
38                 bool IComparer<T>.Compare (T x, T y) {
39                         return true;
40                 }
41         }
42 #endif
43
44 #if !__MOBILE__
45         static int Main (string[] args)
46         {
47                 return TestDriver.RunTests (typeof (Tests), args);
48         }
49 #endif
50
51         public static int test_1_nullable_unbox ()
52         {
53                 return Unbox<int?> (1).Value;
54         }
55
56         public static int test_1_nullable_unbox_null ()
57         {
58                 return Unbox<int?> (null).HasValue ? 0 : 1;
59         }
60
61         public static int test_1_nullable_box ()
62         {
63                 return (int) Box<int?> (1);
64         }
65
66         public static int test_1_nullable_box_null ()
67         {
68                 return Box<int?> (null) == null ? 1 : 0;
69         }
70
71         public static int test_1_isinst_nullable ()
72         {
73                 object o = 1;
74                 return (o is int?) ? 1 : 0;
75         }
76
77         public static int test_1_nullable_unbox_vtype ()
78         {
79                 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
80         }
81
82         public static int test_1_nullable_unbox_null_vtype ()
83         {
84                 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
85         }
86
87         public static int test_1_nullable_box_vtype ()
88         {
89                 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
90         }
91
92         public static int test_1_nullable_box_null_vtype ()
93         {
94                 return Box<TestStruct?> (null) == null ? 1 : 0;
95         }
96
97         public static int test_1_isinst_nullable_vtype ()
98         {
99                 object o = new TestStruct (1, 2);
100                 return (o is TestStruct?) ? 1 : 0;
101         }
102
103         public static int test_0_nullable_normal_unbox ()
104         {
105                 int? i = 5;
106
107                 object o = i;
108                 // This uses unbox instead of unbox_any
109                 int? j = (int?)o;
110
111                 if (j != 5)
112                         return 1;
113
114                 return 0;
115         }
116
117         public static void stelem_any<T> (T[] arr, T elem) {
118                 arr [0] = elem;
119         }
120
121         public static T ldelem_any<T> (T[] arr) {
122                 return arr [0];
123         }
124
125         public static int test_1_ldelem_stelem_any_int () {
126                 int[] arr = new int [3];
127                 stelem_any (arr, 1);
128
129                 return ldelem_any (arr);
130         }
131
132         public static T return_ref<T> (ref T t) {
133                 return t;
134         }
135
136         public static T ldelema_any<T> (T[] arr) {
137                 return return_ref<T> (ref arr [0]);
138         }
139
140         public static int test_0_ldelema () {
141                 string[] arr = new string [1];
142
143                 arr [0] = "Hello";
144
145                 if (ldelema_any <string> (arr) == "Hello")
146                         return 0;
147                 else
148                         return 1;
149         }
150
151         public static T[,] newarr_multi<T> () {
152                 return new T [1, 1];
153         }
154
155         public static int test_0_newarr_multi_dim () {
156                 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
157         }
158
159         interface ITest
160         {
161                 void Foo<T> ();
162         }
163
164         public static int test_0_iface_call_null_bug_77442 () {
165                 ITest test = null;
166
167                 try {
168                         test.Foo<int> ();
169                 }
170                 catch (NullReferenceException) {
171                         return 0;
172                 }
173                 
174                 return 1;
175         }
176
177         public static int test_18_ldobj_stobj_generics () {
178                 GenericClass<int> t = new GenericClass <int> ();
179                 int i = 5;
180                 int j = 6;
181                 return t.ldobj_stobj (ref i, ref j) + i + j;
182         }
183
184         public static int test_5_ldelem_stelem_generics () {
185                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
186
187                 TestStruct s = new TestStruct (5, 5);
188                 return t.ldelem_stelem (s).i;
189         }
190
191         public static int test_0_constrained_vtype_box () {
192                 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
193
194 #if __MOBILE__
195                 return t.toString (new TestStruct ()) == "GenericsTests+TestStruct" ? 0 : 1;
196 #else
197                 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
198 #endif
199         }
200
201         public static int test_0_constrained_vtype () {
202                 GenericClass<int> t = new GenericClass<int> ();
203
204                 return t.toString (1234) == "1234" ? 0 : 1;
205         }
206
207         public static int test_0_constrained_reftype () {
208                 GenericClass<String> t = new GenericClass<String> ();
209
210                 return t.toString ("1234") == "1234" ? 0 : 1;
211         }
212
213         public static int test_0_box_brtrue_optimizations () {
214                 if (IsNull<int>(5))
215                         return 1;
216
217                 if (!IsNull<object>(null))
218                         return 1;
219
220                 return 0;
221         }
222
223         [Category ("!FULLAOT")]
224         public static int test_0_generic_get_value_optimization_int () {
225                 int[] x = new int[] {100, 200};
226
227                 if (GenericClass<int>.Z (x, 0) != 100)
228                         return 2;
229
230                 if (GenericClass<int>.Z (x, 1) != 200)
231                         return 3;
232
233                 return 0;
234         }
235
236         interface NonGenericInterface {
237                 int return_field ();
238         }
239
240         interface GenericInterface<T> : NonGenericInterface {
241                 T not_used ();
242         }
243
244         struct ImplementGenericInterface<T> : GenericInterface<T> {
245                 public Object padding1;
246                 public Object padding2;
247                 public Object padding3;
248                 public T[] arr_t;
249
250                 public ImplementGenericInterface (T[] arr_t) {
251                         this.padding1 = null;
252                         this.padding2 = null;
253                         this.padding3 = null;
254                         this.arr_t = arr_t;
255                 }
256
257                 public T not_used () {
258                         return arr_t [0];
259                 }
260
261                 public int return_field () {
262                         return arr_t.Length;
263                 }
264         }
265
266         public static int test_8_struct_implements_generic_interface () {
267                 int[] arr = {1, 2, 3, 4};
268                 NonGenericInterface s = new ImplementGenericInterface<int> (arr);
269                 return s.return_field () + s.return_field ();
270         }
271
272         public static int test_0_generic_get_value_optimization_vtype () {
273                 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
274                 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
275                 TestStruct s;
276                 int sum = 0;
277                 while (enumerator.MoveNext ()) {
278                         s = enumerator.Current;
279                         sum += s.i + s.j;
280                 }
281
282                 if (sum != 1000)
283                         return 1;
284
285                 s = GenericClass<TestStruct>.Z (arr, 0);
286                 if (s.i != 100 || s.j != 200)
287                         return 2;
288
289                 s = GenericClass<TestStruct>.Z (arr, 1);
290                 if (s.i != 300 || s.j != 400)
291                         return 3;
292
293                 return 0;
294         }
295
296         public static int test_0_nullable_ldflda () {
297                 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
298         }
299
300         public struct GenericStruct<T> {
301                 public T t;
302
303                 public GenericStruct (T t) {
304                         this.t = t;
305                 }
306         }
307
308         public class GenericClass<T> {
309                 public T t;
310
311                 public GenericClass (T t) {
312                         this.t = t;
313                 }
314
315                 public GenericClass () {
316                 }
317
318                 public T ldobj_stobj (ref T t1, ref T t2) {
319                         t1 = t2;
320                         T t = t1;
321
322                         return t;
323                 }
324
325                 public T ldelem_stelem (T t) {
326                         T[] arr = new T [10];
327                         arr [0] = t;
328
329                         return arr [0];
330                 }
331
332                 public String toString (T t) {
333                         return t.ToString ();
334                 }
335
336                 public static IEnumerator<T> Y (IEnumerable <T> x)
337                 {
338                         return x.GetEnumerator ();
339                 }
340
341                 public static T Z (IList<T> x, int index)
342                 {
343                         return x [index];
344                 }
345
346         protected static T NullB = default(T);       
347         private static Nullable<bool>  _BIsA = null;
348         public static bool BIsAClazz {
349             get {
350                 _BIsA = false;
351                 return _BIsA.Value;
352             }
353         }
354         }
355
356         public class MRO : MarshalByRefObject {
357                 public GenericStruct<int> struct_field;
358                 public GenericClass<int> class_field;
359         }
360
361         public class MRO<T> : MarshalByRefObject {
362                 public T gen_field;
363
364                 public T stfld_ldfld (T t) {
365                         var m = this;
366                         m.gen_field = t;
367                         return m.gen_field;
368                 }
369         }
370
371         public static int test_0_ldfld_stfld_mro () {
372                 MRO m = new MRO ();
373                 GenericStruct<int> s = new GenericStruct<int> (5);
374                 // This generates stfld
375                 m.struct_field = s;
376
377                 // This generates ldflda
378                 if (m.struct_field.t != 5)
379                         return 1;
380
381                 // This generates ldfld
382                 GenericStruct<int> s2 = m.struct_field;
383                 if (s2.t != 5)
384                         return 2;
385
386                 if (m.struct_field.t != 5)
387                         return 3;
388
389                 m.class_field = new GenericClass<int> (5);
390                 if (m.class_field.t != 5)
391                         return 4;
392
393                 // gshared
394                 var m2 = new MRO<string> ();
395                 if (m2.stfld_ldfld ("A") != "A")
396                         return 5;
397
398                 return 0;
399         }
400
401         // FIXME:
402         [Category ("!FULLAOT")]
403     public static int test_0_generic_virtual_call_on_vtype_unbox () {
404                 object o = new Object ();
405         IFoo h = new Handler(o);
406
407         if (h.Bar<object> () != o)
408                         return 1;
409                 else
410                         return 0;
411     }
412
413         public static int test_0_box_brtrue_opt () {
414                 Foo<int> f = new Foo<int> (5);
415
416                 f [123] = 5;
417
418                 return 0;
419         }
420
421         public static int test_0_box_brtrue_opt_regress_81102 () {
422                 if (new Foo<int>(5).ToString () == "null")
423                         return 0;
424                 else
425                         return 1;
426         }
427
428         struct S {
429                 public int i;
430         }
431
432         public static int test_0_ldloca_initobj_opt () {
433                 if (new Foo<S> (new S ()).get_default ().i != 0)
434                         return 1;
435                 if (new Foo<object> (null).get_default () != null)
436                         return 2;
437                 return 0;
438         }
439
440 #if !__MOBILE__
441         public static int test_0_variance_reflection () {
442                 // covariance on IEnumerator
443                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
444                         return 1;
445                 // covariance on IEnumerator and covariance on arrays
446                 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
447                         return 2;
448                 // covariance and implemented interfaces
449                 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
450                         return 3;
451
452                 // contravariance on IComparer
453                 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
454                         return 4;
455                 // contravariance on IComparer, contravariance on arrays
456                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
457                         return 5;
458                 // contravariance and interface inheritance
459                 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
460                         return 6;
461                 return 0;
462         }
463 #endif
464
465         [Category ("!INTERPRETER")]
466         public static int test_0_ldvirtftn_generic_method () {
467                 new GenericsTests ().ldvirtftn<string> ();
468
469                 return the_type == typeof (string) ? 0 : 1;
470         }
471
472         public static int test_0_throw_dead_this () {
473         new Foo<string> ("").throw_dead_this ();
474                 return 0;
475         }
476
477         struct S<T> {}
478
479         public static int test_0_inline_infinite_polymorphic_recursion () {
480            f<int>(0);
481
482                    return 0;
483         }
484
485         private static void f<T>(int i) {
486                 if(i==42) f<S<T>>(i);
487         }
488
489         // This cannot be made to work with full-aot, since there it is impossible to
490         // statically determine that Foo<string>.Bar <int> is needed, the code only
491         // references IFoo.Bar<int>
492         [Category ("!INTERPRETER")]
493         [Category ("!FULLAOT")]
494         public static int test_0_generic_virtual_on_interfaces () {
495                 Foo<string>.count1 = 0;
496                 Foo<string>.count2 = 0;
497                 Foo<string>.count3 = 0;
498
499                 IFoo f = new Foo<string> ("");
500                 for (int i = 0; i < 1000; ++i) {
501                         f.Bar <int> ();
502                         f.Bar <string> ();
503                         f.NonGeneric ();
504                 }
505
506                 if (Foo<string>.count1 != 1000)
507                         return 1;
508                 if (Foo<string>.count2 != 1000)
509                         return 2;
510                 if (Foo<string>.count3 != 1000)
511                         return 3;
512
513                 VirtualInterfaceCallFromGenericMethod<long> (f);
514
515                 return 0;
516         }
517
518         [Category ("!INTERPRETER")]
519         public static int test_0_generic_virtual_on_interfaces_ref () {
520                 Foo<string>.count1 = 0;
521                 Foo<string>.count2 = 0;
522                 Foo<string>.count3 = 0;
523                 Foo<string>.count4 = 0;
524
525                 IFoo f = new Foo<string> ("");
526                 for (int i = 0; i < 1000; ++i) {
527                         f.Bar <string> ();
528                         f.Bar <object> ();
529                         f.NonGeneric ();
530                 }
531
532                 if (Foo<string>.count2 != 1000)
533                         return 2;
534                 if (Foo<string>.count3 != 1000)
535                         return 3;
536                 if (Foo<string>.count4 != 1000)
537                         return 4;
538
539                 return 0;
540         }
541
542         //repro for #505375
543         [Category ("!FULLAOT")]
544         public static int test_2_cprop_bug () {
545                 int idx = 0;
546                 int a = 1;
547                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
548                 if (cmp.Compare (a, 0) > 0)
549                         a = 0;
550                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
551                 return idx;
552         }
553
554         enum MyEnumUlong : ulong {
555                 Value_2 = 2
556         }
557
558         [Category ("!INTERPRETER")]
559         public static int test_0_regress_550964_constrained_enum_long () {
560         MyEnumUlong a = MyEnumUlong.Value_2;
561         MyEnumUlong b = MyEnumUlong.Value_2;
562
563         return Pan (a, b) ? 0 : 1;
564         }
565
566     static bool Pan<T> (T a, T b)
567     {
568         return a.Equals (b);
569     }
570
571         public class XElement {
572                 public string Value {
573                         get; set;
574                 }
575         }
576
577         [Category ("!INTERPRETER")]
578         public static int test_0_fullaot_linq () {
579                 var allWords = new XElement [] { new XElement { Value = "one" } };
580                 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
581                 return filteredWords.Count ();
582         }
583
584         public static int test_0_fullaot_comparer_t () {
585                 var l = new SortedList <TimeSpan, int> ();
586                 return l.Count;
587         }
588
589         public static int test_0_fullaot_comparer_t_2 () {
590                 var l = new Dictionary <TimeSpan, int> ();
591                 return l.Count;
592         }
593
594         static void enumerate<T> (IEnumerable<T> arr) {
595                 foreach (var o in arr)
596                         ;
597                 int c = ((ICollection<T>)arr).Count;
598         }
599
600         [Category ("!INTERPRETER")]
601         /* Test that treating arrays as generic collections works with full-aot */
602         public static int test_0_fullaot_array_wrappers () {
603                 GenericsTests[] arr = new GenericsTests [10];
604                 enumerate<GenericsTests> (arr);
605                 return 0;
606         }
607
608         static int cctor_count = 0;
609
610     public abstract class Beta<TChanged> 
611     {           
612         static Beta()
613         {
614                         cctor_count ++;
615         }
616     }   
617     
618     public class Gamma<T> : Beta<T> 
619     {   
620         static Gamma()
621         {
622         }
623     }
624
625         // #519336    
626         public static int test_2_generic_class_init_gshared_ctor () {
627                 new Gamma<object>();
628                 new Gamma<string>();
629
630                 return cctor_count;
631         }
632
633         static int cctor_count2 = 0;
634
635         class ServiceController<T> {
636                 static ServiceController () {
637                         cctor_count2 ++;
638                 }
639
640                 public ServiceController () {
641                 }
642         }
643
644         static ServiceController<T> Create<T>() {
645                 return new ServiceController<T>();
646         }
647
648         // #631409
649         public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
650                 Create<object> ();
651                 Create<string> ();
652
653                 return cctor_count2;
654         }
655
656         public static Type get_type<T> () {
657                 return typeof (T);
658         }
659
660         [Category ("!INTERPRETER")]
661         public static int test_0_gshared_delegate_rgctx () {
662                 Func<Type> t = new Func<Type> (get_type<string>);
663
664                 if (t () == typeof (string))
665                         return 0;
666                 else
667                         return 1;
668         }
669
670         [Category ("!INTERPRETER")]
671         // Creating a delegate from a generic method from gshared code
672         public static int test_0_gshared_delegate_from_gshared () {
673                 if (gshared_delegate_from_gshared <object> () != 0)
674                         return 1;
675                 if (gshared_delegate_from_gshared <string> () != 0)
676                         return 2;
677                 return 0;
678         }
679
680         public static int gshared_delegate_from_gshared <T> () {
681                 Func<Type> t = new Func<Type> (get_type<T>);
682
683                 return t () == typeof (T) ? 0 : 1;
684         }
685
686         public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
687                 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
688                 Class1<object> o = new Class1<object> ();
689                 o.Do (new Class2<object> ());
690                 return 0;
691         }
692
693         class Pair<TKey, TValue> {
694                 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
695                         {
696                                 return new KeyValuePair<TKey, TValue> (key, value);
697                         }
698
699                 public delegate TRet Transform<TRet> (TKey key, TValue value);
700         }
701
702         [Category ("!INTERPRETER")]
703         public static int test_0_bug_620864 () {
704                 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
705
706                 var p = d ("FOO", typeof (int));
707                 if (p.Key != "FOO" || p.Value != typeof (int))
708                         return 1;
709
710                 return 0;
711         }
712
713
714         struct RecStruct<T> {
715                 public void foo (RecStruct<RecStruct<T>> baz) {
716                 }
717         }
718
719         public static int test_0_infinite_generic_recursion () {
720                 // Check that the AOT compile can deal with infinite generic recursion through
721                 // parameter types
722                 RecStruct<int> bla;
723
724                 return 0;
725         }
726
727         struct FooStruct {
728         }
729
730         bool IsNull2 <T> (object value) where T : struct {
731                 T? item = (T?) value;
732
733                 if (item.HasValue)
734                         return false;
735
736                 return true;
737         }
738
739         public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
740                 if (!new GenericsTests ().IsNull2<FooStruct> (null))
741                         return 1;
742                 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
743                         return 2;
744                 return 0;
745         }
746
747         public static int test_0_partial_sharing () {
748                 if (PartialShared1 (new List<string> (), 1) != typeof (string))
749                         return 1;
750                 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
751                         return 2;
752                 if (PartialShared2 (new List<string> (), 1) != typeof (int))
753                         return 3;
754                 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
755                         return 4;
756                 return 0;
757         }
758
759         [Category ("!INTERPRETER")]
760         [Category ("GSHAREDVT")]
761         public static int test_6_partial_sharing_linq () {
762                 var messages = new List<Message> ();
763
764                 messages.Add (new Message () { MessageID = 5 });
765                 messages.Add (new Message () { MessageID = 6 });
766
767                 return messages.Max(i => i.MessageID);
768         }
769
770         [Category ("!INTERPRETER")]
771         public static int test_0_partial_shared_method_in_nonshared_class () {
772                 var c = new Class1<double> ();
773                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
774         }
775
776         class Message {
777                 public int MessageID {
778                         get; set;
779                 }
780         }
781
782         public static Type PartialShared1<T, K> (List<T> list, K k) {
783                 return typeof (T);
784         }
785
786         public static Type PartialShared2<T, K> (List<T> list, K k) {
787                 return typeof (K);
788         }
789
790     public class Class1<T> {
791                 public virtual void Do (Class2<T> t) {
792                         t.Foo ();
793                 }
794
795                 public virtual object Foo<U> (T t) {
796                         return new Class1<U> ();
797                 }
798         }
799
800         public interface IFace1<T> {
801                 void Foo ();
802         }
803
804         public class Class2<T> : MarshalByRefObject, IFace1<T> {
805                 public void Foo () {
806                 }
807         }
808
809
810
811         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
812                 f.Bar <T> ();
813         }
814
815         public static Type the_type;
816
817         public void ldvirtftn<T> () {
818                 Foo <T> binding = new Foo <T> (default (T));
819
820                 binding.GenericEvent += event_handler;
821                 binding.fire ();
822         }
823
824         public virtual void event_handler<T> (Foo<T> sender) {
825                 the_type = typeof (T);
826         }
827
828         public interface IFoo {
829                 void NonGeneric ();
830                 object Bar<T>();
831         }
832
833         public class Foo<T1> : IFoo
834         {
835                 public Foo(T1 t1)
836                 {
837                         m_t1 = t1;
838                 }
839                 
840                 public override string ToString()
841                 {
842                         return Bar(m_t1 == null ? "null" : "null");
843                 }
844
845                 public String Bar (String s) {
846                         return s;
847                 }
848
849                 public int this [T1 key] {
850                         set {
851                                 if (key == null)
852                                         throw new ArgumentNullException ("key");
853                         }
854                 }
855
856                 public void throw_dead_this () {
857                         try {
858                                 new SomeClass().ThrowAnException();
859                         }
860                         catch {
861                         }
862                 }
863
864                 public T1 get_default () {
865                         return default (T1);
866                 }
867                 
868                 readonly T1 m_t1;
869
870                 public delegate void GenericEventHandler (Foo<T1> sender);
871
872                 public event GenericEventHandler GenericEvent;
873
874                 public void fire () {
875                         GenericEvent (this);
876                 }
877
878                 public static int count1, count2, count3, count4;
879
880                 public void NonGeneric () {
881                         count3 ++;
882                 }
883
884                 public object Bar <T> () {
885                         if (typeof (T) == typeof (int))
886                                 count1 ++;
887                         else if (typeof (T) == typeof (string))
888                                 count2 ++;
889                         else if (typeof (T) == typeof (object))
890                                 count4 ++;
891                         return null;
892                 }
893         }
894
895         public class SomeClass {
896                 public void ThrowAnException() {
897                         throw new Exception ("Something went wrong");
898                 }
899         }               
900
901         struct Handler : IFoo {
902                 object o;
903
904                 public Handler(object o) {
905                         this.o = o;
906                 }
907
908                 public void NonGeneric () {
909                 }
910
911                 public object Bar<T>() {
912                         return o;
913                 }
914         }
915
916         static bool IsNull<T> (T t)
917         {
918                 if (t == null)
919                         return true;
920                 else
921                         return false;
922         }
923
924         static object Box<T> (T t)
925         {
926                 return t;
927         }
928         
929         static T Unbox <T> (object o) {
930                 return (T) o;
931         }
932
933         interface IDefaultRetriever
934         {
935                 T GetDefault<T>();
936         }
937
938         class DefaultRetriever : IDefaultRetriever
939         {
940                 [MethodImpl(MethodImplOptions.Synchronized)]
941                 public T GetDefault<T>()
942                 {
943                         return default(T);
944                 }
945         }
946
947         [Category ("!INTERPRETER")]
948         [Category ("!FULLAOT")]
949         [Category ("!BITCODE")]
950         public static int test_0_regress_668095_synchronized_gshared () {
951                 return DoSomething (new DefaultRetriever ());
952         }
953
954     static int DoSomething(IDefaultRetriever foo) {
955                 int result = foo.GetDefault<int>();
956                 return result;
957         }
958
959         class SyncClass<T> {
960                 [MethodImpl(MethodImplOptions.Synchronized)]
961                 public Type getInstance() {
962                         return typeof (T);
963                 }
964         }
965
966         [Category ("!INTERPRETER")]
967         [Category ("GSHAREDVT")]
968         static int test_0_synchronized_gshared () {
969                 var c = new SyncClass<string> ();
970                 if (c.getInstance () != typeof (string))
971                         return 1;
972                 return 0;
973         }
974
975         class Response {
976         }
977
978         public static int test_0_687865_isinst_with_cache_wrapper () {
979                 object o = new object ();
980                 if (o is Action<IEnumerable<Response>>)
981                         return 1;
982                 else
983                         return 0;
984         }
985
986         enum DocType {
987                 One,
988                 Two,
989                 Three
990         }
991
992         class Doc {
993                 public string Name {
994                         get; set;
995                 }
996
997                 public DocType Type {
998                         get; set;
999                 }
1000         }
1001
1002         // #2155
1003         [Category ("!INTERPRETER")]
1004         [Category ("GSHAREDVT")]
1005         public static int test_0_fullaot_sflda_cctor () {
1006                 List<Doc> documents = new List<Doc>();
1007                 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
1008                 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
1009                 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
1010                 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
1011                 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
1012                 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
1013                 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
1014                 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
1015                 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
1016
1017                 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
1018                 foreach(DocType cat in categories) {
1019                         List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
1020                 }
1021                 return 0;
1022         }
1023
1024         class A { }
1025
1026     static List<A> sources = new List<A>();
1027
1028         // #6112
1029         [Category ("!INTERPRETER")]
1030     public static int test_0_fullaot_imt () {
1031         sources.Add(null);
1032         sources.Add(null);
1033
1034         int a = sources.Count;
1035         var enumerator = sources.GetEnumerator() as IEnumerator<object>;
1036
1037         while (enumerator.MoveNext())
1038         {
1039             object o = enumerator.Current;
1040         }
1041
1042                 return 0;
1043         }
1044
1045         class AClass {
1046         }
1047
1048         class BClass : AClass {
1049         }
1050
1051         [Category ("!INTERPRETER")]
1052         public static int test_0_fullaot_variant_iface () {
1053                 var arr = new BClass [10];
1054                 var enumerable = (IEnumerable<AClass>)arr;
1055                 enumerable.GetEnumerator ();
1056                 return 0;
1057         }
1058
1059         struct Record : Foo2<Record>.IRecord {
1060                 int counter;
1061                 int Foo2<Record>.IRecord.DoSomething () {
1062                         return counter++;
1063                 }
1064         }
1065
1066         class Foo2<T> where T : Foo2<T>.IRecord {
1067                 public interface IRecord {
1068                         int DoSomething ();
1069                 }
1070
1071                 public static int Extract (T[] t) {
1072                         return t[0].DoSomething ();
1073                 }
1074         }
1075
1076         class Foo3<T> where T : IComparable {
1077                 public static int CompareTo (T[] t) {
1078                         // This is a constrained call to Enum.CompareTo ()
1079                         return t[0].CompareTo (t [0]);
1080                 }
1081         }
1082
1083         [Category ("!INTERPRETER")]
1084         public static int test_1_regress_constrained_iface_call_7571 () {
1085         var r = new Record [10];
1086         Foo2<Record>.Extract (r);
1087                 return Foo2<Record>.Extract (r);
1088         }
1089
1090         enum ConstrainedEnum {
1091                 Val = 1
1092         }
1093
1094         [Category ("!INTERPRETER")]
1095         public static int test_0_regress_constrained_iface_call_enum () {
1096                 var r = new ConstrainedEnum [10];
1097                 return Foo3<ConstrainedEnum>.CompareTo (r);
1098         }
1099
1100         public interface IFoo2 {
1101                 void MoveNext ();
1102         }
1103
1104         public struct Foo2 : IFoo2 {
1105                 public void MoveNext () {
1106                 }
1107         }
1108
1109         public static Action Dingus (ref Foo2 f) {
1110                 return new Action (f.MoveNext);
1111         }
1112
1113         public static int test_0_delegate_unbox_full_aot () {
1114                 Foo2 foo = new Foo2 ();
1115                 Dingus (ref foo) ();
1116                 return 0;
1117         }
1118
1119         public static int test_0_arrays_ireadonly () {
1120                 int[] arr = new int [10];
1121                 for (int i = 0; i < 10; ++i)
1122                         arr [i] = i;
1123                 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1124                 if (a.Count != 10)
1125                         return 1;
1126                 if (a [0] != 0)
1127                         return 2;
1128                 if (a [1] != 1)
1129                         return 3;
1130                 return 0;
1131         }
1132
1133         public static int test_0_volatile_read_write () {
1134                 string foo = "ABC";
1135                 Volatile.Write (ref foo, "DEF");
1136                 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1137         }
1138
1139         // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1140 #if FALSE
1141         public static async Task<T> FooAsync<T> (int i, int j) {
1142                 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1143                 var response = await t;
1144                 return default(T);
1145         }
1146
1147         public static int test_0_fullaot_generic_async () {
1148                 Task<string> t = FooAsync<string> (1, 2);
1149                 t.RunSynchronously ();
1150                 return 0;
1151         }
1152 #endif
1153
1154         [Category ("!INTERPRETER")]
1155         public static int test_0_delegate_callvirt_fullaot () {
1156                 Func<string> f = delegate () { return "A"; };
1157         var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1158 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1159
1160         var s = f2 (f);
1161                 return s == "A" ? 0 : 1;
1162         }
1163
1164     public interface ICovariant<out R>
1165     {
1166     }
1167
1168     // Deleting the `out` modifier from this line stop the problem
1169     public interface IExtCovariant<out R> : ICovariant<R>
1170     {
1171     }
1172
1173     public class Sample<R> : ICovariant<R>
1174     {
1175     }
1176
1177     public interface IMyInterface
1178     {
1179     }
1180
1181         public static int test_0_variant_cast_cache () {
1182                 object covariant = new Sample<IMyInterface>();
1183
1184                 var foo = (ICovariant<IMyInterface>)(covariant);
1185
1186                 try {
1187                         var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1188                         return 1;
1189                 } catch {
1190                         return 0;
1191                 }
1192         }
1193
1194         struct FooStruct2 {
1195                 public int a1, a2, a3;
1196         }
1197
1198         class MyClass<T> where T: struct {
1199                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1200                 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1201                 }
1202
1203                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1204                 public static MyClass<T> foo () {
1205                         Nullable<T> a = new Nullable<T> ();
1206                         return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1207                 }
1208         }
1209
1210         public static int test_0_newobj_generic_context () {
1211                 MyClass<FooStruct2>.foo ();
1212                 return 0;
1213         }
1214
1215         enum AnEnum {
1216                 A,
1217                 B
1218         }
1219
1220         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1221         public static string constrained_tostring<T> (T t) {
1222                 return t.ToString ();
1223         }
1224
1225         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1226         public static bool constrained_equals<T> (T t1, T t2) {
1227                 var c = EqualityComparer<T>.Default;
1228
1229                 return c.Equals (t1, t2);
1230         }
1231
1232         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1233         public static int constrained_gethashcode<T> (T t) {
1234                 return t.GetHashCode ();
1235         }
1236
1237         [Category ("!INTERPRETER")]
1238         public static int test_0_constrained_partial_sharing () {
1239                 string s;
1240
1241                 s = constrained_tostring<int> (5);
1242                 if (s != "5")
1243                         return 1;
1244                 s = constrained_tostring<AnEnum> (AnEnum.B);
1245                 if (s != "B")
1246                         return 2;
1247
1248                 if (!constrained_equals<int> (1, 1))
1249                         return 3;
1250                 if (constrained_equals<int> (1, 2))
1251                         return 4;
1252                 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1253                         return 5;
1254                 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1255                         return 6;
1256
1257                 int i = constrained_gethashcode<int> (5);
1258                 if (i != 5)
1259                         return 7;
1260                 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1261                 if (i != 1)
1262                         return 8;
1263                 return 0;
1264         }
1265
1266         enum Enum1 {
1267                 A,
1268                 B
1269         }
1270
1271         enum Enum2 {
1272                 A,
1273                 B
1274         }
1275
1276         public static int test_0_partial_sharing_ginst () {
1277                 var l1 = new List<KeyValuePair<int, Enum1>> ();
1278                 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1279                 if (l1 [0].Key != 5)
1280                         return 1;
1281                 if (l1 [0].Value != Enum1.A)
1282                         return 2;
1283                 var l2 = new List<KeyValuePair<int, Enum2>> ();
1284                 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1285                 if (l2 [0].Key != 5)
1286                         return 3;
1287                 if (l2 [0].Value != Enum2.B)
1288                         return 4;
1289                 return 0;
1290         }
1291
1292         static object delegate_8_args_res;
1293
1294         [Category ("!INTERPRETER")]
1295         public static int test_0_delegate_8_args () {
1296                 delegate_8_args_res = null;
1297                 Action<string, string, string, string, string, string, string,
1298                         string> test = (a, b, c, d, e, f, g, h) =>
1299             {
1300                                 delegate_8_args_res = h;
1301             };
1302                 test("a", "b", "c", "d", "e", "f", "g", "h");
1303                 return delegate_8_args_res == "h" ? 0 : 1;
1304         }
1305
1306         static void throw_catch_t<T> () where T: Exception {
1307                 try {
1308                         throw new NotSupportedException ();
1309                 } catch (T) {
1310                 }
1311         }
1312
1313         public static int test_0_gshared_catch_open_type () {
1314                 throw_catch_t<NotSupportedException> ();
1315                 return 0;
1316         }
1317
1318         class ThrowClass<T> where T: Exception {
1319                 public void throw_catch_t () {
1320                         try {
1321                                 throw new NotSupportedException ();
1322                         } catch (T) {
1323                         }
1324                 }
1325         }
1326
1327         public static int test_0_gshared_catch_open_type_instance () {
1328                 var c = new ThrowClass<NotSupportedException> ();
1329                 c.throw_catch_t ();
1330                 return 0;
1331         }
1332 }
1333
1334 #if !__MOBILE__
1335 class GenericsTests : Tests
1336 {
1337 }
1338 #endif