From 648c83897b5b1d086bfb05fddef1a3f73917435a Mon Sep 17 00:00:00 2001 From: gaschd Date: Wed, 6 Nov 2024 00:36:28 +0100 Subject: [PATCH 1/4] Added test case for issue #1148 containing delegates with interface as TResult --- .../CSharp/ExpressionTests/ExpressionTests.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Tests/CSharp/ExpressionTests/ExpressionTests.cs b/Tests/CSharp/ExpressionTests/ExpressionTests.cs index 1002400d..17c9a723 100644 --- a/Tests/CSharp/ExpressionTests/ExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/ExpressionTests.cs @@ -1440,6 +1440,74 @@ private void TestMethod() }"); } + [Fact] + public async Task Issue1148_AddressOfSignatureCompatibilityAsync() + { + await TestConversionVisualBasicToCSharpAsync(@" +Imports System + +Public Class Issue1148 + Public Shared FuncClass As Func(Of TestObjClass) = AddressOf FunctionReturningClass + Public Shared FuncBaseClass As Func(Of TestBaseObjClass) = AddressOf FunctionReturningClass + Public Shared FuncInterface As Func(Of ITestObj) = AddressOf FunctionReturningClass + Public Shared FuncInterfaceParam As Func(Of ITestObj, ITestObj) = AddressOf CastObj + + Public Shared Function FunctionReturningClass() As TestObjClass + Return New TestObjClass() + End Function + + Public Shared Function CastObj(obj As ITestObj) As TestObjClass + Return CType(obj, TestObjClass) + End Function + +End Class + +Public Class TestObjClass + Inherits TestBaseObjClass + Implements ITestObj +End Class + +Public Class TestBaseObjClass +End Class + +Public Interface ITestObj +End Interface +", @" +using System; + +public partial class Issue1148 +{ + public static Func FuncClass = FunctionReturningClass; + public static Func FuncBaseClass = FunctionReturningClass; + public static Func FuncInterface = FunctionReturningClass; + public static Func FuncInterfaceParam = CastObj; + + public static TestObjClass FunctionReturningClass() + { + return new TestObjClass(); + } + + public static TestObjClass CastObj(ITestObj obj) + { + return (TestObjClass)obj; + } + +} + +public partial class TestObjClass : TestBaseObjClass, ITestObj +{ +} + +public partial class TestBaseObjClass +{ +} + +public partial interface ITestObj +{ +} +"); + } + [Fact] public async Task LambdaImmediatelyExecutedAsync() { From 51b0ff1446d77aef28d5eaaf88c5f6e684275b31 Mon Sep 17 00:00:00 2001 From: gaschd Date: Wed, 6 Nov 2024 00:47:48 +0100 Subject: [PATCH 2/4] AddressOf delegate signatures can accept interfaces as compatible return types --- CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs b/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs index 3852eb6c..00ac9adf 100644 --- a/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs +++ b/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs @@ -23,7 +23,7 @@ public static bool CompatibleSignatureToDelegate(this IMethodSymbol method, INam return false; } - if (!method.ReturnType.InheritsFromOrEquals(invoke.ReturnType)) { + if (!method.ReturnType.InheritsFromOrEquals(invoke.ReturnType, true)) { return false; } From 9a08d6ead67af15aef1b773d627ba17f3eef1b97 Mon Sep 17 00:00:00 2001 From: gaschd Date: Wed, 6 Nov 2024 00:48:51 +0100 Subject: [PATCH 3/4] Extending test to check delegates with classes that implement the interface --- Tests/CSharp/ExpressionTests/ExpressionTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/CSharp/ExpressionTests/ExpressionTests.cs b/Tests/CSharp/ExpressionTests/ExpressionTests.cs index 17c9a723..1595a234 100644 --- a/Tests/CSharp/ExpressionTests/ExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/ExpressionTests.cs @@ -1451,6 +1451,7 @@ Public Shared FuncClass As Func(Of TestObjClass) = AddressOf FunctionReturningCl Public Shared FuncBaseClass As Func(Of TestBaseObjClass) = AddressOf FunctionReturningClass Public Shared FuncInterface As Func(Of ITestObj) = AddressOf FunctionReturningClass Public Shared FuncInterfaceParam As Func(Of ITestObj, ITestObj) = AddressOf CastObj + Public Shared FuncClassParam As Func(Of TestObjClass, ITestObj) = AddressOf CastObj Public Shared Function FunctionReturningClass() As TestObjClass Return New TestObjClass() @@ -1481,6 +1482,7 @@ public partial class Issue1148 public static Func FuncBaseClass = FunctionReturningClass; public static Func FuncInterface = FunctionReturningClass; public static Func FuncInterfaceParam = CastObj; + public static Func FuncClassParam = CastObj; public static TestObjClass FunctionReturningClass() { From 552f89dce92000c786fd7e46d4e7db3f7098fb18 Mon Sep 17 00:00:00 2001 From: gaschd Date: Wed, 6 Nov 2024 00:50:35 +0100 Subject: [PATCH 4/4] AddressOf delegate signatures can accept classes implementing the parameter interface as compatible parameter types --- CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs b/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs index 00ac9adf..445d169c 100644 --- a/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs +++ b/CodeConverter/Util/FromRoslyn/IMethodSymbolExtensions.cs @@ -28,7 +28,7 @@ public static bool CompatibleSignatureToDelegate(this IMethodSymbol method, INam } for (var i = 0; i < method.Parameters.Length; i++) { - if (!invoke.Parameters[i].Type.InheritsFromOrEquals(method.Parameters[i].Type)) { + if (!invoke.Parameters[i].Type.InheritsFromOrEquals(method.Parameters[i].Type, true)) { return false; } }