Bug 318438 (MONO75454) - [GMCS] NullRef inside BindGenericParameters when using Type from CreateType ()
Summary: [GMCS] NullRef inside BindGenericParameters when using Type from CreateType ()
Status: RESOLVED FIXED
Alias: MONO75454
Product: Mono: Runtime
Classification: Mono
Component: misc (show other bugs)
Version: 1.0
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Martin Baulig
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-03 21:43 UTC by Kamil Skalski
Modified: 2007-09-15 21:24 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 19:24:11 UTC


---- Reported by nazgul@omega.pl 2005-07-03 14:43:53 MST ----

Please fill in this template when reporting a bug, unless you know what you
are doing.
Description of Problem:
There is a problem in binding generic parameters of System.Type returned
from TypeBuilder.CreateType()

Steps to reproduce the problem:
1. Compile and run program:

using System;
using System.Reflection;
using System.Reflection.Emit;

class M {
         static void Main () {
            /// INITIAL ASSEMBLY CREATION
            AssemblyName name = new AssemblyName();
            name.Name = "o";
            AssemblyBuilder builder =
AppDomain.CurrentDomain.DefineDynamicAssembly(name,
AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder mb = builder.DefineDynamicModule("out", "bla.dll");
            ///////////

           
            /// abstract class Func ['a]
            /// { public abstract apply () : 'a; }
            TypeBuilder func = mb.DefineType("Func", TypeAttributes.Abstract);
            GenericTypeParameterBuilder[] func_gp =
func.DefineGenericParameters("'a");
            func.SetParent(typeof (object));

            MethodBuilder func_apply = func.DefineMethod("apply",
MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
                 func_gp[0], new Type[] { });

            Type func_c = func.CreateType();

            /// class Prox : Func[string] {
            /// } 

            // this causes NullRef in BindGenericParameters
            Type func_string = func_c.MakeGenericType(new Type [] {
typeof(string)});

            // and this causes InvalidOperation in GetGenericTypeDefinition()
            // Type func_string = func_c.GetGenericTypeDefinition
().MakeGenericType(new Type [] { typeof(string)});

            TypeBuilder prox = mb.DefineType("Prox", TypeAttributes.Public);
            prox.SetParent(func_string);

            prox.CreateType();

            builder.Save("bla.dll");
         }
}

Actual Results:
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
in <0x00000> <unknown method>
in (wrapper managed-to-native) System.Type:BindGenericParameters
(System.Type,System.Type[])
in <0x00068> System.Type:BindGenericParameters (System.Type[] types)
in <0x00010> System.Type:MakeGenericType (System.Type[] types)
in <0x0018d> M:Main ()

Expected Results:
clear run and valid bla.dll created

How often does this happen? 
Always

Additional Information:
Also GetGenericTypeDefinition do not work for type returned from CreateType().
It can be workarounded by using original TypeBuilder instance instead of
one returned from CreateType, but it is important for us to have the
created type also working, since some MS.NET bugs are triggered when using
original TypeBuilder. I hope there is no reason why
GetGenericTypeDefinition or BindGenericParameters should not work...



---- Additional Comments From martin@ximian.com 2005-07-11 12:25:31 MST ----

Fixed in SVN.



---- Additional Comments From nazgul@omega.pl 2005-07-11 13:13:01 MST ----

One more thing... When I change assembly attribute to Save only, it
crashes again:

using System;
using System.Reflection;
using System.Reflection.Emit;

class M {
         static void Main () {
            /// INITIAL ASSEMBLY CREATION
            AssemblyName name = new AssemblyName();
            name.Name = "o";
            AssemblyBuilder builder =
AppDomain.CurrentDomain.DefineDynamicAssembly(name,
AssemblyBuilderAccess.Save);
            ModuleBuilder mb = builder.DefineDynamicModule("out",
"bla.dll");
            ///////////

           
            /// abstract class Func ['a]
            /// { public abstract apply () : 'a; }
            TypeBuilder func = mb.DefineType("Func",
TypeAttributes.Abstract);
            GenericTypeParameterBuilder[] func_gp =
func.DefineGenericParameters("'a");
            func.SetParent(typeof (object));

            MethodBuilder func_apply = func.DefineMethod("apply",
MethodAttributes.Public | MethodAttributes.Abstract |
MethodAttributes.Virtual,
                 func_gp[0], new Type[] { });

            Type func_c = func.CreateType();

            /// class Prox : Func[string] {
            /// } 

            // this causes NullRef in BindGenericParameters
            Type func_string = func_c.MakeGenericType(new Type [] {
typeof(string)});

            // and this causes InvalidOperation in
GetGenericTypeDefinition()
            //Type func_string = func_c.GetGenericTypeDefinition
().MakeGenericType(new Type [] { typeof(string)});

            TypeBuilder prox = mb.DefineType("Prox",
TypeAttributes.Public);
            prox.SetParent(func_string);

            prox.CreateType();

            builder.Save("bla.dll");
         }
}






---- Additional Comments From martin@ximian.com 2005-07-12 08:42:39 MST ----

Fixed in SVN.


Unknown bug field "cf_op_sys_details" encountered while moving bug
   <cf_op_sys_details>mono svn</cf_op_sys_details>
Unknown operating system unknown. Setting to default OS "Other".