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

Handle custom modifiers on parameter constraints #83850

Merged
merged 1 commit into from
Mar 24, 2023
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
50 changes: 50 additions & 0 deletions src/libraries/Common/tests/System/ModifiedTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,51 @@ public static unsafe void GenericMethod_Modified()
Assert.Equal(1, a1.GetOptionalCustomModifiers().Length);
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/71095", TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/71883", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void ParameterConstraints1()
{
MethodInfo mi = typeof(ModifiedTypeHolder).Project().GetMethod(nameof(ModifiedTypeHolder.M_GenericWithParameterConstraint1), Bindings);
Assert.True(mi.ContainsGenericParameters);
Assert.True(mi.IsGenericMethod);
Assert.True(mi.IsGenericMethodDefinition);

Type p = mi.GetParameters()[0].ParameterType;
Assert.False(IsModifiedType(p));
Assert.True(p.ContainsGenericParameters);
Assert.True(p.IsByRef);

Type e = p.GetElementType();
Assert.False(IsModifiedType(e));
Assert.True(e.IsValueType);
Assert.Equal(GenericParameterAttributes.DefaultConstructorConstraint | GenericParameterAttributes.NotNullableValueTypeConstraint, e.GenericParameterAttributes);
Assert.True(e.GetGenericParameterConstraints().Length == 1);
// The 'unmanaged' constraint is a modreq of type 'System.Runtime.InteropServices.' applied to 'ValueType'.
Assert.Equal(typeof(ValueType).Project(), e.GetGenericParameterConstraints()[0]);
// The 'UnmanagedType' modreq is not available for unmodified types.
Assert.Equal(0, e.GetGenericParameterConstraints()[0].GetRequiredCustomModifiers().Length);
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/71095", TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/71883", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public static void ParameterConstraints2()
{
MethodInfo mi = typeof(ModifiedTypeHolder).Project().GetMethod(nameof(ModifiedTypeHolder.M_GenericWithParameterConstraint2), Bindings);
Assert.True(mi.ContainsGenericParameters);
Assert.True(mi.IsGenericMethod);
Assert.True(mi.IsGenericMethodDefinition);

Type p = mi.GetParameters()[0].ParameterType;
Assert.False(IsModifiedType(p));
Assert.True(p.ContainsGenericParameters);
Assert.False(p.IsValueType);
Assert.Equal(GenericParameterAttributes.DefaultConstructorConstraint, p.GenericParameterAttributes);
Assert.True(p.GetGenericParameterConstraints().Length == 1);
Assert.Equal(typeof(ModifiedTypeHolder.MyConstraint).Project(), p.GetGenericParameterConstraints()[0]);
}

private static bool IsModifiedType(Type type)
{
return !ReferenceEquals(type, type.UnderlyingSystemType);
Expand Down Expand Up @@ -736,8 +781,13 @@ public ModifiedTypeHolder(delegate*<out int, void> d) { }
public static void M_P0IntOut(out int i) { i = 42; }
public static void M_P0FcnPtrOut(delegate*<out int, void> fp) { }
public static void M_ArrayOpenGenericFcnPtr<T>(T t, delegate*<out bool, void>[] fp) { }
public static void M_GenericWithParameterConstraint1<T>(out T value) where T : unmanaged { value = default; }
public static void M_GenericWithParameterConstraint2<T>(T value) where T : MyConstraint, new() { value = default; }

public int InitProperty_Int { get; init; }

public class MyConstraint { }

public static delegate*<out int, void> Property_FcnPtr { get; set; }

public static delegate*<out int, void> _fcnPtrP0Out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ protected sealed override RoType[] ComputeGenericParameterConstraints()
foreach (GenericParameterConstraintHandle h in handles)
{
RoType constraint = h.GetGenericParameterConstraint(reader).Type.ResolveTypeDefRefOrSpec(GetEcmaModule(), typeContext);

// A constraint can have modifiers such as 'System.Runtime.InteropServices.UnmanagedType' which here is a 'System.ValueType'
// modified type with a modreq for 'UnmanagedType' which would be obtainable through 'GetRequiredCustomModifiers()'.
// However, for backwards compat, just return the unmodified type ('ValueType' in this case). This also prevents modified types from
// "leaking" into an unmodified type hierarchy.
if (constraint is RoModifiedType)
{
constraint = (RoType)constraint.UnderlyingSystemType;
}

constraints[index++] = constraint;
}
return constraints;
Expand Down