[test] Generic typebuilder field in another typebuilder should work.
authorAleksey Kliger <aleksey@xamarin.com>
Tue, 29 Nov 2016 00:40:10 +0000 (19:40 -0500)
committerAleksey Kliger <aleksey@xamarin.com>
Tue, 29 Nov 2016 16:40:16 +0000 (11:40 -0500)
Even if the generic typebuilder hasn't had CreateType() called on it
yet.

Regression tests for https://bugzilla.xamarin.com/show_bug.cgi?id=47867

mcs/class/corlib/Test/System.Reflection.Emit/TypeBuilderTest.cs

index ac3b1f149fa3d8e587cb8cf5a691fcc62eef46a5..6d056da47a33590723a552c93aeef3711b577dd9 100644 (file)
@@ -11190,5 +11190,63 @@ namespace MonoTests.System.Reflection.Emit
 
                interface IFoo {
                }
+
+               [Test]
+               public void GenericFieldInCreatedType () {
+                       /*
+                        * Regression test for #47867.
+                        * We construct the following, but only call CreateType on R.
+                        *
+                        * public class S<T> {
+                        *   public T t;
+                        * }
+                        * public class R {
+                        *   public static S<R> sr;
+                        * }
+                        */
+                       var aname = new AssemblyName ("example1");
+                       var ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
+                       var mb = ab.DefineDynamicModule (aname.Name);
+                       var tbS = mb.DefineType ("S", TypeAttributes.Public);
+                       tbS.DefineGenericParameters (new String [] { "T" });
+                       var tbR = mb.DefineType ("R", TypeAttributes.Public);
+                       tbR.DefineField ("sr", tbS.MakeGenericType(new Type[] { tbR }), FieldAttributes.Public | FieldAttributes.Static);
+
+                       Type r = tbR.CreateType ();
+
+                       Assert.IsNotNull  (r);
+               }
+
+               [Test]
+               public void GenericFieldInCreatedTypeIncompleteTypeTLE () {
+                       /*
+                        * Regression test for #47867.
+                        * We construct the following, but only call CreateType on R.
+                        * Then we try to use R.sr which is expected throw a
+                        * TLE because S hasn't been created yet.
+                        *
+                        * public class S<T> {
+                        *   public T t;
+                        * }
+                        * public class R {
+                        *   public static S<R> sr;
+                        * }
+                        */
+                       var aname = new AssemblyName ("example1");
+                       var ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
+                       var mb = ab.DefineDynamicModule (aname.Name);
+                       var tbS = mb.DefineType ("S", TypeAttributes.Public);
+                       tbS.DefineGenericParameters (new String [] { "T" });
+                       var tbR = mb.DefineType ("R", TypeAttributes.Public);
+                       tbR.DefineField ("sr", tbS.MakeGenericType(new Type[] { tbR }), FieldAttributes.Public | FieldAttributes.Static);
+
+                       Type r = tbR.CreateType ();
+
+                       Assert.IsNotNull  (r);
+
+                       // N.B.  tbS has not had CreateType called yet, so expect this to fail.
+                       Assert.Throws<TypeLoadException> (delegate { var ft = r.GetField("sr").FieldType; });
+               }
+               
        }
 }