From 6df753f4445d854521d1e647bf961a565e13c958 Mon Sep 17 00:00:00 2001 From: Victor Milovanov Date: Sat, 29 Aug 2020 15:06:11 -0700 Subject: [PATCH] fix `object[]` parameters taking precedence when should not in overload resolution --- CHANGELOG.md | 6 ++++++ src/runtime/methodbinder.cs | 20 ++++++++++---------- src/testing/methodtest.cs | 3 ++- src/tests/test_method.py | 9 ++++++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51e4f842e..f7cf0859b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ project adheres to [Semantic Versioning][]. This document follows the conventions laid out in [Keep a CHANGELOG][]. + +## Unreleased + +### Fixed +- Fix `object[]` parameters taking precedence when should not in overload resolution + ## [2.5.1][] - 2020-06-18 Bugfix release. diff --git a/src/runtime/methodbinder.cs b/src/runtime/methodbinder.cs index b3186a3f2..2725f5f16 100644 --- a/src/runtime/methodbinder.cs +++ b/src/runtime/methodbinder.cs @@ -203,6 +203,16 @@ internal static int ArgPrecedence(Type t) return 3000; } + if (t.IsArray) + { + Type e = t.GetElementType(); + if (e == objectType) + { + return 2500; + } + return 100 + ArgPrecedence(e); + } + TypeCode tc = Type.GetTypeCode(t); // TODO: Clean up switch (tc) @@ -250,16 +260,6 @@ internal static int ArgPrecedence(Type t) return 40; } - if (t.IsArray) - { - Type e = t.GetElementType(); - if (e == objectType) - { - return 2500; - } - return 100 + ArgPrecedence(e); - } - return 2000; } diff --git a/src/testing/methodtest.cs b/src/testing/methodtest.cs index 91836b727..9a4c408d6 100644 --- a/src/testing/methodtest.cs +++ b/src/testing/methodtest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Runtime.InteropServices; namespace Python.Test @@ -84,7 +85,7 @@ public Type[] TestNullArrayConversion(Type[] v) public static string[] TestStringParamsArg(params string[] args) { - return args; + return args.Concat(new []{"tail"}).ToArray(); } public static object[] TestObjectParamsArg(params object[] args) diff --git a/src/tests/test_method.py b/src/tests/test_method.py index 69f1b5e72..8456ddc9f 100644 --- a/src/tests/test_method.py +++ b/src/tests/test_method.py @@ -208,17 +208,20 @@ def test_null_array_conversion(): def test_string_params_args(): """Test use of string params.""" result = MethodTest.TestStringParamsArg('one', 'two', 'three') - assert result.Length == 3 - assert len(result) == 3, result + assert result.Length == 4 + assert len(result) == 4, result assert result[0] == 'one' assert result[1] == 'two' assert result[2] == 'three' + # ensures params string[] overload takes precedence over params object[] + assert result[3] == 'tail' result = MethodTest.TestStringParamsArg(['one', 'two', 'three']) - assert len(result) == 3 + assert len(result) == 4 assert result[0] == 'one' assert result[1] == 'two' assert result[2] == 'three' + assert result[3] == 'tail' def test_object_params_args():