Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Using TypeBuilder as generic argument for a parent type throws #99771

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ private static MemberInfo GetOriginalMemberIfConstructedType(MethodBase methodBa
Type declaringType = methodBase.DeclaringType!;
if (declaringType.IsConstructedGenericType &&
declaringType.GetGenericTypeDefinition() is not TypeBuilderImpl &&
!ContainsNotBakedTypeBuilder(declaringType.GetGenericArguments()))
!ContainsTypeBuilder(declaringType.GetGenericArguments()))
{
return declaringType.GetGenericTypeDefinition().GetMemberWithSameMetadataDefinitionAs(methodBase);
}
Expand Down Expand Up @@ -883,19 +883,19 @@ private static int GetTokenForHandle(EntityHandle handle)

private EntityHandle GetHandleForMember(MemberInfo member)
{
if (IsConstructedFromNotBakedTypeBuilder(member.DeclaringType!))
if (IsConstructedFromTypeBuilder(member.DeclaringType!))
{
return default;
}

return GetMemberReferenceHandle(member);
}

private static bool IsConstructedFromNotBakedTypeBuilder(Type type) => type.IsConstructedGenericType &&
Copy link
Contributor Author

@buyaa-n buyaa-n Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods initially used for checking if constructed from a not baked TypeBuilder, but now the check for not baked part is removed and its used for all cases no matter if the TypeBuilder was baked or not

private static bool IsConstructedFromTypeBuilder(Type type) => type.IsConstructedGenericType &&
(type.GetGenericTypeDefinition() is TypeBuilderImpl ||
ContainsNotBakedTypeBuilder(type.GetGenericArguments()));
ContainsTypeBuilder(type.GetGenericArguments()));

private static bool ContainsNotBakedTypeBuilder(Type[] genericArguments)
internal static bool ContainsTypeBuilder(Type[] genericArguments)
{
foreach (Type type in genericArguments)
{
Expand All @@ -904,7 +904,7 @@ private static bool ContainsNotBakedTypeBuilder(Type[] genericArguments)
return true;
}

if (IsConstructedFromNotBakedTypeBuilder(type))
if (IsConstructedFromTypeBuilder(type))
{
return true;
}
Expand All @@ -925,7 +925,7 @@ internal EntityHandle TryGetTypeHandle(Type type)
return eb._typeBuilder._handle;
}

if (IsConstructedFromNotBakedTypeBuilder(type))
if (IsConstructedFromTypeBuilder(type))
{
return default;
}
Expand Down Expand Up @@ -954,20 +954,20 @@ internal EntityHandle TryGetMethodHandle(MethodInfo method)
return mb._handle;
}

if (IsConstructedMethodFromNotBakedMethodBuilder(method) ||
IsArrayMethodFromNotBakedTypeBuilder(method))
if (IsConstructedFromMethodBuilderOrTypeBuilder(method) ||
IsArrayMethodTypeIsTypeBuilder(method))
{
return default;
}

return GetHandleForMember(method);
}

private static bool IsArrayMethodFromNotBakedTypeBuilder(MethodInfo method) => method is ArrayMethod arrayMethod &&
arrayMethod.DeclaringType!.GetElementType() is TypeBuilderImpl tb && tb._handle == default;
private static bool IsArrayMethodTypeIsTypeBuilder(MethodInfo method) => method is ArrayMethod arrayMethod &&
arrayMethod.DeclaringType!.GetElementType() is TypeBuilderImpl;

private static bool IsConstructedMethodFromNotBakedMethodBuilder(MethodInfo method) => method.IsConstructedGenericMethod &&
(method.GetGenericMethodDefinition() is MethodBuilderImpl mb && mb._handle == default || ContainsNotBakedTypeBuilder(method.GetGenericArguments()));
private static bool IsConstructedFromMethodBuilderOrTypeBuilder(MethodInfo method) => method.IsConstructedGenericMethod &&
(method.GetGenericMethodDefinition() is MethodBuilderImpl || ContainsTypeBuilder(method.GetGenericArguments()));

internal EntityHandle TryGetMethodHandle(MethodInfo method, Type[] optionalParameterTypes)
{
Expand All @@ -982,7 +982,7 @@ internal EntityHandle TryGetMethodHandle(MethodInfo method, Type[] optionalParam
return mb._handle;
}

if (IsConstructedMethodFromNotBakedMethodBuilder(method))
if (IsConstructedFromMethodBuilderOrTypeBuilder(method))
{
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ private ConstructorBuilderImpl DefineDefaultConstructorInternal(MethodAttributes
{
// Get the parent class's default constructor and add it to the IL
ConstructorInfo? con;
if (_typeParent!.IsConstructedGenericType && _typeParent.GetGenericTypeDefinition() is TypeBuilderImpl typeBuilder)
if (_typeParent!.IsConstructedGenericType &&
(_typeParent.GetGenericTypeDefinition() is TypeBuilderImpl || ModuleBuilderImpl.ContainsTypeBuilder(_typeParent.GetGenericArguments())))
{
con = GetConstructor(_typeParent, typeBuilder.GetConstructor(
// When TypeBuilder involved need to construct the parent constructor using TypeBuilder.GetConstructor() static method
con = GetConstructor(_typeParent, _typeParent.GetGenericTypeDefinition().GetConstructor(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, EmptyTypes, null)!);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,23 @@ public void MethodBuilderGetParametersReturnParameterTest()
Assert.Null(method3.ReturnParameter.Name);
Assert.True(method3.ReturnParameter.IsRetval);
}

public class BaseType<T> { }

[Fact]
public void GenericTypeWithTypeBuilderGenericParameter_UsedAsParent()
{
PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder typeBuilder);

Type type = typeBuilder.CreateType();
var baseType = typeof(BaseType<>).GetGenericTypeDefinition().MakeGenericType(type);

var typeBuilder2 = ab.GetDynamicModule("MyModule")
.DefineType("TestService", TypeAttributes.Public | TypeAttributes.Class, baseType);
typeBuilder2.CreateType();

Assert.NotNull(type.GetConstructor(Type.EmptyTypes)); // Default constructor created
}
}

// Test Types
Expand Down
Loading