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

Commit

Permalink
Fix direction of variance check in delegate assignment (#4945)
Browse files Browse the repository at this point in the history
* Fix direction of variance check in delegate assignment

* Add tests
  • Loading branch information
jcouv authored and jkotas committed Nov 16, 2017
1 parent 7f651b7 commit cfd5af4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ILVerify/src/ILImporter.Verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,12 @@ bool IsDelegateAssignable(MethodDesc ftn, TypeDesc delegateType)

for (int i = 0; i < ftnSignature.Length; i++)
{
if (!IsAssignable(ftnSignature[i], delegateSignature[i]))
if (!IsAssignable(delegateSignature[i], ftnSignature[i]))
return false;
}

// Compare return type
return IsAssignable(delegateSignature.ReturnType, ftnSignature.ReturnType);
return IsAssignable(ftnSignature.ReturnType, delegateSignature.ReturnType);
}

ILOpcode GetOpcodeAt(int instructionOffset)
Expand Down
1 change: 1 addition & 0 deletions src/ILVerify/src/ILVerify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
<CLSCompliant>false</CLSCompliant>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
Expand Down
4 changes: 3 additions & 1 deletion src/ILVerify/tests/ILMethodTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using Internal.IL;
using Internal.TypeSystem.Ecma;
Expand Down Expand Up @@ -60,7 +61,8 @@ void TestMethodsWithInvalidIL(InvalidILTestCase invalidIL)

foreach (var item in invalidIL.ExpectedVerifierErrors)
{
Assert.True(verifierErrors.Contains(item));
var actual = verifierErrors.Select(e => e.ToString());
Assert.True(verifierErrors.Contains(item), $"Actual errors where: {string.Join(',', actual)}");
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/ILVerify/tests/ILTests/DelegateTests.il
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

.assembly extern System.Runtime
{
}

.assembly DelegateTests
{
}

.class private auto ansi beforefieldinit C
extends [System.Runtime]System.Object
{
// assignment from Func<int, string> to Func<int, object> is valid
.method private hidebysig instance class [System.Runtime]System.Func`2<int32,object>
DelegateAssignmentReturn_Valid(class [System.Runtime]System.Func`2<int32,string> input) cil managed
{
ldarg.1
ret
}

// assignment from Func<object, int> to Func<string, int> is valid
.method private hidebysig instance class [System.Runtime]System.Func`2<string,int32>
DelegateAssignmentParameter_Valid(class [System.Runtime]System.Func`2<object,int32> input) cil managed
{
ldarg.1
ret
}

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

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

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
ldarg.0
call instance void [System.Runtime]System.Object::.ctor()
ret
}
}

0 comments on commit cfd5af4

Please sign in to comment.