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

Fix direction of variance check in delegate assignment #4945

Merged
merged 3 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
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]))
Copy link
Member

@VSadov VSadov Nov 16, 2017

Choose a reason for hiding this comment

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

Technically the types should be "Compatible". In the spec "Compatible" is more strict than "Assignable", since "Assignable" allows some reinterpretations. I.E. an int32 is Assignable to int8 - one can be assigned to another with implicit value truncation.
However int32 and int8 are not "Compatible" and therefore int M1() cannot be used as Func<byte>

I am not sure what IsAssignable implements. It could as well implement "IsCompatible", then everything is correct.
Perhaps it is worth adding a test while touching this?

Copy link
Member

Choose a reason for hiding this comment

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

I am sorry - I have not noticed that you have commented on this before merging. I agree it would be nice to add test for this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Filed follow-up issue.

Copy link
Member

Choose a reason for hiding this comment

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

I would not block merging on this. Just a good thing to have.

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
{
call instance void [System.Runtime]System.Object::.ctor()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a nit, but doesn't this require the this pointer to be loaded first?

It's not really a problem, since this method is not verified anyway, but I think it is still good practice to keep the non-invalid IL code valid in these test files.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, looks like I mangled it. Thanks :-)

By the way, I'd love to sync up (email or skype) at some point, so that we're not duplicating efforts. Would you mind contacting me ([email protected])? Cheers

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, I'll send you a message.

nop
ret
}
}