Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

[ILVerify] Add additional tests for delegate assignment/return #5027

Merged
merged 2 commits into from
Nov 27, 2017
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: 49 additions & 1 deletion src/ILVerify/tests/ILTests/DelegateTests.il
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
{
}

.class private auto ansi beforefieldinit C
.class public auto ansi sealed ByteEnum
extends [System.Runtime]System.Enum
{
.field public specialname rtspecialname uint8 value__
.field public static literal valuetype ByteEnum A = uint8(0)
.field public static literal valuetype ByteEnum B = uint8(0)
}

.class private auto ansi beforefieldinit DelegateTestsType
extends [System.Runtime]System.Object
{
// assignment from Func<int, string> to Func<int, object> is valid
Expand Down Expand Up @@ -45,6 +53,46 @@
ret
}

// assignment from Func<int> to Func<byte> is invalid
.method private hidebysig instance class [System.Runtime]System.Func`1<uint8>
AssignIntFuncToByteFunc_Invalid_StackUnexpected(class [System.Runtime]System.Func`2<int32> input) cil managed
{
ldarg.1
ret
}

// assignment from Func<byte> to Func<int> is invalid
.method private hidebysig instance class [System.Runtime]System.Func`1<int32>
AssignByteFuncToIntFunc_Invalid_StackUnexpected(class [System.Runtime]System.Func`1<uint8> input) cil managed
{
ldarg.1
ret
}
Copy link
Member

@jkotas jkotas Nov 27, 2017

Choose a reason for hiding this comment

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

Also cover arguments and enums here?

  • Action<int8> to Action<int32>
  • Action<int8 enum> to Action<int8>


// assignment from Func<ByteEnum> to Func<byte> is invalid
.method private hidebysig instance class [System.Runtime]System.Func`1<uint8>
AssignByteActionToIntAction_Invalid_StackUnexpected(class [System.Runtime]System.Func`1<valuetype ByteEnum> input) cil managed
{
ldarg.1
ret
}

// assignment from Action<byte> to Action<int> is invalid
.method private hidebysig instance class [System.Runtime]System.Action`1<int32>
AssignByteActionToIntAction_Invalid_StackUnexpected(class [System.Runtime]System.Action`1<uint8> input) cil managed
{
ldarg.1
ret
}

// assignment from Action<ByteEnum> to Action<byte> is invalid
.method private hidebysig instance class [System.Runtime]System.Action`1<uint8>
AssignByteActionToByteEnumAction_Invalid_StackUnexpected(class [System.Runtime]System.Action`1<valuetype ByteEnum> input) cil managed
{
ldarg.1
ret
}

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
Expand Down
132 changes: 132 additions & 0 deletions src/ILVerify/tests/ILTests/FtnTests.il
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,68 @@
}
}

// Type containing test methods for delegate-assignment
.class public auto ansi beforefieldinit TestMethodsType
extends [System.Runtime]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
ldarg.0
call instance void [System.Runtime]System.Object::.ctor()
ret
}

.method public hidebysig static uint8 ByteReturnMethod() cil managed
{
ldc.i4.0
ret
}

.method public hidebysig static valuetype ByteEnum ByteEnumReturnMethod() cil managed
{
ldc.i4.0
box ByteEnum
ret
}

.method public hidebysig static int32 IntReturnMethod() cil managed
{
ldc.i4.0
ret
}

.method public hidebysig static string StringReturnMethod() cil managed
{
ldnull
ret
}

.method public hidebysig static object ObjectReturnMethod() cil managed
{
ldnull
ret
}

.method public hidebysig static void ByteParamMethod(uint8 param) cil managed
{
ret
}

.method public hidebysig static void ByteEnumParamMethod(valuetype ByteEnum param) cil managed
{
ret
}
}

.class public auto ansi sealed ByteEnum
extends [System.Runtime]System.Enum
{
.field public specialname rtspecialname uint8 value__
.field public static literal valuetype ByteEnum A = uint8(0)
.field public static literal valuetype ByteEnum B = uint8(0)
}

.class public auto ansi beforefieldinit FtnTestsType
extends [System.Runtime]System.Object
{
Expand Down Expand Up @@ -363,4 +425,74 @@
callvirt instance void [System.Runtime]System.Action::Invoke()
ret
}

// creating a Func<object> from a string return method is valid
.method private hidebysig instance void ObjectFuncFromStringMethod_Valid() cil managed
{
ldnull
ldftn string TestMethodsType::StringReturnMethod()
newobj instance void class [System.Runtime]System.Func`1<object>::.ctor(object, native int)
pop
ret
}

// creating a Func<string> from a object return method is invalid
.method private hidebysig instance void StringFuncFromObjectMethod_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn object TestMethodsType::ObjectReturnMethod()
newobj instance void class [System.Runtime]System.Func`1<string>::.ctor(object, native int)
pop
ret
}

// creating a Func<byte> from an int return method is invalid
.method private hidebysig instance void ByteFuncFromIntMethod_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn int32 TestMethodsType::IntReturnMethod()
newobj instance void class [System.Runtime]System.Func`1<uint8>::.ctor(object, native int)
pop
ret
}

// creating a Func<int> from a byte return method is invalid
.method private hidebysig instance void IntFuncFromByteMethod_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn uint8 TestMethodsType::ByteReturnMethod()
newobj instance void class [System.Runtime]System.Func`1<int32>::.ctor(object, native int)
pop
ret
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here.


// creating a Func<byte> from a ByteEnum return method is invalid
.method private hidebysig instance void IntFuncFromByteEnumMethod_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn valuetype ByteEnum TestMethodsType::ByteEnumReturnMethod()
newobj instance void class [System.Runtime]System.Func`1<int32>::.ctor(object, native int)
pop
ret
}

// creating an Action<int> from a method with byte parameter is invalid
.method private hidebysig instance void IntActionFromByteMethod_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn void TestMethodsType::ByteParamMethod(uint8)
newobj instance void class [System.Runtime]System.Action`1<int32>::.ctor(object, native int)
pop
ret
}

// creating an Action<byte> from a method with an enum (with underlying type byte) as parameter is invalid
.method private hidebysig instance void ByteActionFromByteEnum_Invalid_DelegateCtor() cil managed
{
ldnull
ldftn void TestMethodsType::ByteEnumParamMethod(valuetype ByteEnum)
newobj instance void class [System.Runtime]System.Action`1<uint8>::.ctor(object, native int)
pop
ret
}
}