[interp] implement constrained calls to interface methods correctly
[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         public static int test_0_ldvirtftn_generic_method () {
466                 new GenericsTests ().ldvirtftn<string> ();
467
468                 return the_type == typeof (string) ? 0 : 1;
469         }
470
471         public static int test_0_throw_dead_this () {
472         new Foo<string> ("").throw_dead_this ();
473                 return 0;
474         }
475
476         struct S<T> {}
477
478         public static int test_0_inline_infinite_polymorphic_recursion () {
479            f<int>(0);
480
481                    return 0;
482         }
483
484         private static void f<T>(int i) {
485                 if(i==42) f<S<T>>(i);
486         }
487
488         // This cannot be made to work with full-aot, since there it is impossible to
489         // statically determine that Foo<string>.Bar <int> is needed, the code only
490         // references IFoo.Bar<int>
491         [Category ("!FULLAOT")]
492         public static int test_0_generic_virtual_on_interfaces () {
493                 Foo<string>.count1 = 0;
494                 Foo<string>.count2 = 0;
495                 Foo<string>.count3 = 0;
496
497                 IFoo f = new Foo<string> ("");
498                 for (int i = 0; i < 1000; ++i) {
499                         f.Bar <int> ();
500                         f.Bar <string> ();
501                         f.NonGeneric ();
502                 }
503
504                 if (Foo<string>.count1 != 1000)
505                         return 1;
506                 if (Foo<string>.count2 != 1000)
507                         return 2;
508                 if (Foo<string>.count3 != 1000)
509                         return 3;
510
511                 VirtualInterfaceCallFromGenericMethod<long> (f);
512
513                 return 0;
514         }
515
516         public static int test_0_generic_virtual_on_interfaces_ref () {
517                 Foo<string>.count1 = 0;
518                 Foo<string>.count2 = 0;
519                 Foo<string>.count3 = 0;
520                 Foo<string>.count4 = 0;
521
522                 IFoo f = new Foo<string> ("");
523                 for (int i = 0; i < 1000; ++i) {
524                         f.Bar <string> ();
525                         f.Bar <object> ();
526                         f.NonGeneric ();
527                 }
528
529                 if (Foo<string>.count2 != 1000)
530                         return 2;
531                 if (Foo<string>.count3 != 1000)
532                         return 3;
533                 if (Foo<string>.count4 != 1000)
534                         return 4;
535
536                 return 0;
537         }
538
539         //repro for #505375
540         [Category ("!FULLAOT")]
541         public static int test_2_cprop_bug () {
542                 int idx = 0;
543                 int a = 1;
544                 var cmp = System.Collections.Generic.Comparer<int>.Default ;
545                 if (cmp.Compare (a, 0) > 0)
546                         a = 0;
547                 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
548                 return idx;
549         }
550
551         enum MyEnumUlong : ulong {
552                 Value_2 = 2
553         }
554
555         public static int test_0_regress_550964_constrained_enum_long () {
556         MyEnumUlong a = MyEnumUlong.Value_2;
557         MyEnumUlong b = MyEnumUlong.Value_2;
558
559         return Pan (a, b) ? 0 : 1;
560         }
561
562     static bool Pan<T> (T a, T b)
563     {
564         return a.Equals (b);
565     }
566
567         public class XElement {
568                 public string Value {
569                         get; set;
570                 }
571         }
572
573         [Category ("!INTERPRETER")]
574         public static int test_0_fullaot_linq () {
575                 var allWords = new XElement [] { new XElement { Value = "one" } };
576                 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
577                 return filteredWords.Count ();
578         }
579
580         public static int test_0_fullaot_comparer_t () {
581                 var l = new SortedList <TimeSpan, int> ();
582                 return l.Count;
583         }
584
585         public static int test_0_fullaot_comparer_t_2 () {
586                 var l = new Dictionary <TimeSpan, int> ();
587                 return l.Count;
588         }
589
590         static void enumerate<T> (IEnumerable<T> arr) {
591                 foreach (var o in arr)
592                         ;
593                 int c = ((ICollection<T>)arr).Count;
594         }
595
596         /* Test that treating arrays as generic collections works with full-aot */
597         public static int test_0_fullaot_array_wrappers () {
598                 GenericsTests[] arr = new GenericsTests [10];
599                 enumerate<GenericsTests> (arr);
600                 return 0;
601         }
602
603         static int cctor_count = 0;
604
605     public abstract class Beta<TChanged> 
606     {           
607         static Beta()
608         {
609                         cctor_count ++;
610         }
611     }   
612     
613     public class Gamma<T> : Beta<T> 
614     {   
615         static Gamma()
616         {
617         }
618     }
619
620         // #519336    
621         public static int test_2_generic_class_init_gshared_ctor () {
622                 new Gamma<object>();
623                 new Gamma<string>();
624
625                 return cctor_count;
626         }
627
628         static int cctor_count2 = 0;
629
630         class ServiceController<T> {
631                 static ServiceController () {
632                         cctor_count2 ++;
633                 }
634
635                 public ServiceController () {
636                 }
637         }
638
639         static ServiceController<T> Create<T>() {
640                 return new ServiceController<T>();
641         }
642
643         // #631409
644         public static int test_2_generic_class_init_gshared_ctor_from_gshared () {
645                 Create<object> ();
646                 Create<string> ();
647
648                 return cctor_count2;
649         }
650
651         public static Type get_type<T> () {
652                 return typeof (T);
653         }
654
655         public static int test_0_gshared_delegate_rgctx () {
656                 Func<Type> t = new Func<Type> (get_type<string>);
657
658                 if (t () == typeof (string))
659                         return 0;
660                 else
661                         return 1;
662         }
663
664         // Creating a delegate from a generic method from gshared code
665         public static int test_0_gshared_delegate_from_gshared () {
666                 if (gshared_delegate_from_gshared <object> () != 0)
667                         return 1;
668                 if (gshared_delegate_from_gshared <string> () != 0)
669                         return 2;
670                 return 0;
671         }
672
673         public static int gshared_delegate_from_gshared <T> () {
674                 Func<Type> t = new Func<Type> (get_type<T>);
675
676                 return t () == typeof (T) ? 0 : 1;
677         }
678
679         public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
680                 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
681                 Class1<object> o = new Class1<object> ();
682                 o.Do (new Class2<object> ());
683                 return 0;
684         }
685
686         class Pair<TKey, TValue> {
687                 public static KeyValuePair<TKey, TValue> make_pair (TKey key, TValue value)
688                         {
689                                 return new KeyValuePair<TKey, TValue> (key, value);
690                         }
691
692                 public delegate TRet Transform<TRet> (TKey key, TValue value);
693         }
694
695         public static int test_0_bug_620864 () {
696                 var d = new Pair<string, Type>.Transform<KeyValuePair<string, Type>> (Pair<string, Type>.make_pair);
697
698                 var p = d ("FOO", typeof (int));
699                 if (p.Key != "FOO" || p.Value != typeof (int))
700                         return 1;
701
702                 return 0;
703         }
704
705
706         struct RecStruct<T> {
707                 public void foo (RecStruct<RecStruct<T>> baz) {
708                 }
709         }
710
711         public static int test_0_infinite_generic_recursion () {
712                 // Check that the AOT compile can deal with infinite generic recursion through
713                 // parameter types
714                 RecStruct<int> bla;
715
716                 return 0;
717         }
718
719         struct FooStruct {
720         }
721
722         bool IsNull2 <T> (object value) where T : struct {
723                 T? item = (T?) value;
724
725                 if (item.HasValue)
726                         return false;
727
728                 return true;
729         }
730
731         public static int test_0_full_aot_nullable_unbox_from_gshared_code () {
732                 if (!new GenericsTests ().IsNull2<FooStruct> (null))
733                         return 1;
734                 if (new GenericsTests ().IsNull2<FooStruct> (new FooStruct ()))
735                         return 2;
736                 return 0;
737         }
738
739         public static int test_0_partial_sharing () {
740                 if (PartialShared1 (new List<string> (), 1) != typeof (string))
741                         return 1;
742                 if (PartialShared1 (new List<GenericsTests> (), 1) != typeof (GenericsTests))
743                         return 2;
744                 if (PartialShared2 (new List<string> (), 1) != typeof (int))
745                         return 3;
746                 if (PartialShared2 (new List<GenericsTests> (), 1) != typeof (int))
747                         return 4;
748                 return 0;
749         }
750
751         [Category ("GSHAREDVT")]
752         public static int test_6_partial_sharing_linq () {
753                 var messages = new List<Message> ();
754
755                 messages.Add (new Message () { MessageID = 5 });
756                 messages.Add (new Message () { MessageID = 6 });
757
758                 return messages.Max(i => i.MessageID);
759         }
760
761         public static int test_0_partial_shared_method_in_nonshared_class () {
762                 var c = new Class1<double> ();
763                 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
764         }
765
766         class Message {
767                 public int MessageID {
768                         get; set;
769                 }
770         }
771
772         public static Type PartialShared1<T, K> (List<T> list, K k) {
773                 return typeof (T);
774         }
775
776         public static Type PartialShared2<T, K> (List<T> list, K k) {
777                 return typeof (K);
778         }
779
780     public class Class1<T> {
781                 public virtual void Do (Class2<T> t) {
782                         t.Foo ();
783                 }
784
785                 public virtual object Foo<U> (T t) {
786                         return new Class1<U> ();
787                 }
788         }
789
790         public interface IFace1<T> {
791                 void Foo ();
792         }
793
794         public class Class2<T> : MarshalByRefObject, IFace1<T> {
795                 public void Foo () {
796                 }
797         }
798
799
800
801         public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
802                 f.Bar <T> ();
803         }
804
805         public static Type the_type;
806
807         public void ldvirtftn<T> () {
808                 Foo <T> binding = new Foo <T> (default (T));
809
810                 binding.GenericEvent += event_handler;
811                 binding.fire ();
812         }
813
814         public virtual void event_handler<T> (Foo<T> sender) {
815                 the_type = typeof (T);
816         }
817
818         public interface IFoo {
819                 void NonGeneric ();
820                 object Bar<T>();
821         }
822
823         public class Foo<T1> : IFoo
824         {
825                 public Foo(T1 t1)
826                 {
827                         m_t1 = t1;
828                 }
829                 
830                 public override string ToString()
831                 {
832                         return Bar(m_t1 == null ? "null" : "null");
833                 }
834
835                 public String Bar (String s) {
836                         return s;
837                 }
838
839                 public int this [T1 key] {
840                         set {
841                                 if (key == null)
842                                         throw new ArgumentNullException ("key");
843                         }
844                 }
845
846                 public void throw_dead_this () {
847                         try {
848                                 new SomeClass().ThrowAnException();
849                         }
850                         catch {
851                         }
852                 }
853
854                 public T1 get_default () {
855                         return default (T1);
856                 }
857                 
858                 readonly T1 m_t1;
859
860                 public delegate void GenericEventHandler (Foo<T1> sender);
861
862                 public event GenericEventHandler GenericEvent;
863
864                 public void fire () {
865                         GenericEvent (this);
866                 }
867
868                 public static int count1, count2, count3, count4;
869
870                 public void NonGeneric () {
871                         count3 ++;
872                 }
873
874                 public object Bar <T> () {
875                         if (typeof (T) == typeof (int))
876                                 count1 ++;
877                         else if (typeof (T) == typeof (string))
878                                 count2 ++;
879                         else if (typeof (T) == typeof (object))
880                                 count4 ++;
881                         return null;
882                 }
883         }
884
885         public class SomeClass {
886                 public void ThrowAnException() {
887                         throw new Exception ("Something went wrong");
888                 }
889         }               
890
891         struct Handler : IFoo {
892                 object o;
893
894                 public Handler(object o) {
895                         this.o = o;
896                 }
897
898                 public void NonGeneric () {
899                 }
900
901                 public object Bar<T>() {
902                         return o;
903                 }
904         }
905
906         static bool IsNull<T> (T t)
907         {
908                 if (t == null)
909                         return true;
910                 else
911                         return false;
912         }
913
914         static object Box<T> (T t)
915         {
916                 return t;
917         }
918         
919         static T Unbox <T> (object o) {
920                 return (T) o;
921         }
922
923         interface IDefaultRetriever
924         {
925                 T GetDefault<T>();
926         }
927
928         class DefaultRetriever : IDefaultRetriever
929         {
930                 [MethodImpl(MethodImplOptions.Synchronized)]
931                 public T GetDefault<T>()
932                 {
933                         return default(T);
934                 }
935         }
936
937         [Category ("!FULLAOT")]
938         [Category ("!BITCODE")]
939         public static int test_0_regress_668095_synchronized_gshared () {
940                 return DoSomething (new DefaultRetriever ());
941         }
942
943     static int DoSomething(IDefaultRetriever foo) {
944                 int result = foo.GetDefault<int>();
945                 return result;
946         }
947
948         class SyncClass<T> {
949                 [MethodImpl(MethodImplOptions.Synchronized)]
950                 public Type getInstance() {
951                         return typeof (T);
952                 }
953         }
954
955         [Category ("GSHAREDVT")]
956         static int test_0_synchronized_gshared () {
957                 var c = new SyncClass<string> ();
958                 if (c.getInstance () != typeof (string))
959                         return 1;
960                 return 0;
961         }
962
963         class Response {
964         }
965
966         public static int test_0_687865_isinst_with_cache_wrapper () {
967                 object o = new object ();
968                 if (o is Action<IEnumerable<Response>>)
969                         return 1;
970                 else
971                         return 0;
972         }
973
974         enum DocType {
975                 One,
976                 Two,
977                 Three
978         }
979
980         class Doc {
981                 public string Name {
982                         get; set;
983                 }
984
985                 public DocType Type {
986                         get; set;
987                 }
988         }
989
990         // #2155
991         [Category ("!INTERPRETER")]
992         [Category ("GSHAREDVT")]
993         public static int test_0_fullaot_sflda_cctor () {
994                 List<Doc> documents = new List<Doc>();
995                 documents.Add(new Doc { Name = "Doc1", Type = DocType.One } );
996                 documents.Add(new Doc { Name = "Doc2", Type = DocType.Two } );
997                 documents.Add(new Doc { Name = "Doc3", Type = DocType.Three } );
998                 documents.Add(new Doc { Name = "Doc4", Type = DocType.One } );
999                 documents.Add(new Doc { Name = "Doc5", Type = DocType.Two } );
1000                 documents.Add(new Doc { Name = "Doc6", Type = DocType.Three } );
1001                 documents.Add(new Doc { Name = "Doc7", Type = DocType.One } );
1002                 documents.Add(new Doc { Name = "Doc8", Type = DocType.Two } );
1003                 documents.Add(new Doc { Name = "Doc9", Type = DocType.Three } );
1004
1005                 List<DocType> categories = documents.Select(d=>d.Type).Distinct().ToList<DocType>().OrderBy(d => d).ToList();
1006                 foreach(DocType cat in categories) {
1007                         List<Doc> catDocs = documents.Where(d => d.Type == cat).OrderBy(d => d.Name).ToList<Doc>();
1008                 }
1009                 return 0;
1010         }
1011
1012         class A { }
1013
1014     static List<A> sources = new List<A>();
1015
1016         // #6112
1017     public static int test_0_fullaot_imt () {
1018         sources.Add(null);
1019         sources.Add(null);
1020
1021         int a = sources.Count;
1022         var enumerator = sources.GetEnumerator() as IEnumerator<object>;
1023
1024         while (enumerator.MoveNext())
1025         {
1026             object o = enumerator.Current;
1027         }
1028
1029                 return 0;
1030         }
1031
1032         class AClass {
1033         }
1034
1035         class BClass : AClass {
1036         }
1037
1038         public static int test_0_fullaot_variant_iface () {
1039                 var arr = new BClass [10];
1040                 var enumerable = (IEnumerable<AClass>)arr;
1041                 enumerable.GetEnumerator ();
1042                 return 0;
1043         }
1044
1045         struct Record : Foo2<Record>.IRecord {
1046                 int counter;
1047                 int Foo2<Record>.IRecord.DoSomething () {
1048                         return counter++;
1049                 }
1050         }
1051
1052         class Foo2<T> where T : Foo2<T>.IRecord {
1053                 public interface IRecord {
1054                         int DoSomething ();
1055                 }
1056
1057                 public static int Extract (T[] t) {
1058                         return t[0].DoSomething ();
1059                 }
1060         }
1061
1062         class Foo3<T> where T : IComparable {
1063                 public static int CompareTo (T[] t) {
1064                         // This is a constrained call to Enum.CompareTo ()
1065                         return t[0].CompareTo (t [0]);
1066                 }
1067         }
1068
1069         public static int test_1_regress_constrained_iface_call_7571 () {
1070         var r = new Record [10];
1071         Foo2<Record>.Extract (r);
1072                 return Foo2<Record>.Extract (r);
1073         }
1074
1075         enum ConstrainedEnum {
1076                 Val = 1
1077         }
1078
1079         public static int test_0_regress_constrained_iface_call_enum () {
1080                 var r = new ConstrainedEnum [10];
1081                 return Foo3<ConstrainedEnum>.CompareTo (r);
1082         }
1083
1084         public interface IFoo2 {
1085                 void MoveNext ();
1086         }
1087
1088         public struct Foo2 : IFoo2 {
1089                 public void MoveNext () {
1090                 }
1091         }
1092
1093         public static Action Dingus (ref Foo2 f) {
1094                 return new Action (f.MoveNext);
1095         }
1096
1097         public static int test_0_delegate_unbox_full_aot () {
1098                 Foo2 foo = new Foo2 ();
1099                 Dingus (ref foo) ();
1100                 return 0;
1101         }
1102
1103         public static int test_0_arrays_ireadonly () {
1104                 int[] arr = new int [10];
1105                 for (int i = 0; i < 10; ++i)
1106                         arr [i] = i;
1107                 IReadOnlyList<int> a = (IReadOnlyList<int>)(object)arr;
1108                 if (a.Count != 10)
1109                         return 1;
1110                 if (a [0] != 0)
1111                         return 2;
1112                 if (a [1] != 1)
1113                         return 3;
1114                 return 0;
1115         }
1116
1117         public static int test_0_volatile_read_write () {
1118                 string foo = "ABC";
1119                 Volatile.Write (ref foo, "DEF");
1120                 return Volatile.Read (ref foo) == "DEF" ? 0 : 1;
1121         }
1122
1123         // FIXME: Doesn't work with --regression as Interlocked.Add(ref long) is only implemented as an intrinsic
1124 #if FALSE
1125         public static async Task<T> FooAsync<T> (int i, int j) {
1126                 Task<int> t = new Task<int> (delegate () { Console.WriteLine ("HIT!"); return 0; });
1127                 var response = await t;
1128                 return default(T);
1129         }
1130
1131         public static int test_0_fullaot_generic_async () {
1132                 Task<string> t = FooAsync<string> (1, 2);
1133                 t.RunSynchronously ();
1134                 return 0;
1135         }
1136 #endif
1137
1138         [Category ("!INTERPRETER")]
1139         public static int test_0_delegate_callvirt_fullaot () {
1140                 Func<string> f = delegate () { return "A"; };
1141         var f2 = (Func<Func<string>, string>)Delegate.CreateDelegate (typeof
1142 (Func<Func<string>, string>), null, f.GetType ().GetMethod ("Invoke"));
1143
1144         var s = f2 (f);
1145                 return s == "A" ? 0 : 1;
1146         }
1147
1148     public interface ICovariant<out R>
1149     {
1150     }
1151
1152     // Deleting the `out` modifier from this line stop the problem
1153     public interface IExtCovariant<out R> : ICovariant<R>
1154     {
1155     }
1156
1157     public class Sample<R> : ICovariant<R>
1158     {
1159     }
1160
1161     public interface IMyInterface
1162     {
1163     }
1164
1165         public static int test_0_variant_cast_cache () {
1166                 object covariant = new Sample<IMyInterface>();
1167
1168                 var foo = (ICovariant<IMyInterface>)(covariant);
1169
1170                 try {
1171                         var extCovariant = (IExtCovariant<IMyInterface>)covariant;
1172                         return 1;
1173                 } catch {
1174                         return 0;
1175                 }
1176         }
1177
1178         struct FooStruct2 {
1179                 public int a1, a2, a3;
1180         }
1181
1182         class MyClass<T> where T: struct {
1183                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1184                 public MyClass(int a1, int a2, int a3, int a4, int a5, int a6, Nullable<T> a) {
1185                 }
1186
1187                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1188                 public static MyClass<T> foo () {
1189                         Nullable<T> a = new Nullable<T> ();
1190                         return new MyClass<T> (0, 0, 0, 0, 0, 0, a);
1191                 }
1192         }
1193
1194         public static int test_0_newobj_generic_context () {
1195                 MyClass<FooStruct2>.foo ();
1196                 return 0;
1197         }
1198
1199         enum AnEnum {
1200                 A,
1201                 B
1202         }
1203
1204         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1205         public static string constrained_tostring<T> (T t) {
1206                 return t.ToString ();
1207         }
1208
1209         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1210         public static bool constrained_equals<T> (T t1, T t2) {
1211                 var c = EqualityComparer<T>.Default;
1212
1213                 return c.Equals (t1, t2);
1214         }
1215
1216         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1217         public static int constrained_gethashcode<T> (T t) {
1218                 return t.GetHashCode ();
1219         }
1220
1221         [Category ("!INTERPRETER")]
1222         public static int test_0_constrained_partial_sharing () {
1223                 string s;
1224
1225                 s = constrained_tostring<int> (5);
1226                 if (s != "5")
1227                         return 1;
1228                 s = constrained_tostring<AnEnum> (AnEnum.B);
1229                 if (s != "B")
1230                         return 2;
1231
1232                 if (!constrained_equals<int> (1, 1))
1233                         return 3;
1234                 if (constrained_equals<int> (1, 2))
1235                         return 4;
1236                 if (!constrained_equals<AnEnum> (AnEnum.A, AnEnum.A))
1237                         return 5;
1238                 if (constrained_equals<AnEnum> (AnEnum.A, AnEnum.B))
1239                         return 6;
1240
1241                 int i = constrained_gethashcode<int> (5);
1242                 if (i != 5)
1243                         return 7;
1244                 i = constrained_gethashcode<AnEnum> (AnEnum.B);
1245                 if (i != 1)
1246                         return 8;
1247                 return 0;
1248         }
1249
1250         enum Enum1 {
1251                 A,
1252                 B
1253         }
1254
1255         enum Enum2 {
1256                 A,
1257                 B
1258         }
1259
1260         public static int test_0_partial_sharing_ginst () {
1261                 var l1 = new List<KeyValuePair<int, Enum1>> ();
1262                 l1.Add (new KeyValuePair<int, Enum1>(5, Enum1.A));
1263                 if (l1 [0].Key != 5)
1264                         return 1;
1265                 if (l1 [0].Value != Enum1.A)
1266                         return 2;
1267                 var l2 = new List<KeyValuePair<int, Enum2>> ();
1268                 l2.Add (new KeyValuePair<int, Enum2>(5, Enum2.B));
1269                 if (l2 [0].Key != 5)
1270                         return 3;
1271                 if (l2 [0].Value != Enum2.B)
1272                         return 4;
1273                 return 0;
1274         }
1275
1276         static object delegate_8_args_res;
1277
1278         public static int test_0_delegate_8_args () {
1279                 delegate_8_args_res = null;
1280                 Action<string, string, string, string, string, string, string,
1281                         string> test = (a, b, c, d, e, f, g, h) =>
1282             {
1283                                 delegate_8_args_res = h;
1284             };
1285                 test("a", "b", "c", "d", "e", "f", "g", "h");
1286                 return delegate_8_args_res == "h" ? 0 : 1;
1287         }
1288
1289         static void throw_catch_t<T> () where T: Exception {
1290                 try {
1291                         throw new NotSupportedException ();
1292                 } catch (T) {
1293                 }
1294         }
1295
1296         public static int test_0_gshared_catch_open_type () {
1297                 throw_catch_t<NotSupportedException> ();
1298                 return 0;
1299         }
1300
1301         class ThrowClass<T> where T: Exception {
1302                 public void throw_catch_t () {
1303                         try {
1304                                 throw new NotSupportedException ();
1305                         } catch (T) {
1306                         }
1307                 }
1308         }
1309
1310         public static int test_0_gshared_catch_open_type_instance () {
1311                 var c = new ThrowClass<NotSupportedException> ();
1312                 c.throw_catch_t ();
1313                 return 0;
1314         }
1315 }
1316
1317 #if !__MOBILE__
1318 class GenericsTests : Tests
1319 {
1320 }
1321 #endif