This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 10e6562f49 GROOVY-8737: STC: no varargs distance for exact match of
variadic method
10e6562f49 is described below
commit 10e6562f4975270f966331b570a17d1cbdded4f6
Author: Eric Milles <[email protected]>
AuthorDate: Tue Aug 23 10:57:45 2022 -0500
GROOVY-8737: STC: no varargs distance for exact match of variadic method
---
.../transform/stc/StaticTypeCheckingSupport.java | 11 +-
.../groovy/transform/stc/MethodCallsSTCTest.groovy | 145 +++++++++++++--------
2 files changed, 96 insertions(+), 60 deletions(-)
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 3ef5df9ea4..b63fc940bb 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1117,15 +1117,14 @@ public abstract class StaticTypeCheckingSupport {
private static int measureParametersAndArgumentsDistance(final Parameter[]
parameters, final ClassNode[] argumentTypes) {
int dist = -1;
if (parameters.length == argumentTypes.length) {
- int allMatch = allParametersAndArgumentsMatch(parameters,
argumentTypes);
- int endMatch = -1;
+ dist = allParametersAndArgumentsMatch(parameters, argumentTypes);
if (isVargs(parameters) &&
firstParametersAndArgumentsMatch(parameters, argumentTypes) >= 0) {
- endMatch = lastArgMatchesVarg(parameters, argumentTypes);
- if (endMatch >= 0) {
- endMatch += getVarargsDistance(parameters);
+ int endDist = lastArgMatchesVarg(parameters, argumentTypes);
+ if (endDist >= 0) {
+ endDist += getVarargsDistance(parameters);
+ dist = (dist < 0 ? endDist : Math.min(dist, endDist)); //
GROOVY-8737
}
}
- dist = (allMatch >= 0 ? Math.max(allMatch, endMatch) : endMatch);
} else if (isVargs(parameters)) {
dist = firstParametersAndArgumentsMatch(parameters, argumentTypes);
if (dist >= 0) {
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index dbe4c33fd5..251bce0fdd 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -1235,7 +1235,7 @@ class MethodCallsSTCTest extends
StaticTypeCheckingTestCase {
'''
}
- void testVargsSelection() {
+ void testVargsSelection1() {
assertScript '''
int foo(int x, Object... args) { 1 }
int foo(Object... args) { 2 }
@@ -1265,6 +1265,96 @@ class MethodCallsSTCTest extends
StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-6147
+ void testVargsSelection4() {
+ assertScript '''
+ int select(Object a, String s) { 1 }
+ int select(Object a, String s, Object[] args) { 2 }
+ def o = new Date()
+ def s = 'String'
+ @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ def method =
node.rightExpression.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
+ assert method.name == 'select'
+ assert method.parameters.length==2
+ })
+ def result = select(o,s)
+ assert result == 1
+ '''
+ }
+
+ // GROOVY-6195
+ void testVargsSelection5() {
+ assertScript '''
+ def list = ['a', 'b', 'c']
+ Object[] arr = list.toArray()
+ println arr
+ '''
+ }
+
+ // GROOVY-6235
+ void testVargsSelection6() {
+ assertScript '''import
org.codehaus.groovy.classgen.asm.sc.support.Groovy6235SupportSub as Support
+ def b = new Support()
+ assert b.overload() == 1
+ assert b.overload('a') == 1
+ assert b.overload('a','b') == 2
+ '''
+ }
+
+ // GROOVY-6646
+ void testVargsSelection7() {
+ assertScript '''
+ def foo(Class... cs) { "Classes" }
+ def foo(String... ss) { "Strings" }
+
+ assert foo(List, Map) == "Classes"
+ assert foo("2","1") == "Strings"
+ '''
+ assertScript '''
+ def foo(Class<?>... cs) { "Classes" }
+ def foo(String... ss) { "Strings" }
+
+ assert foo(List, Map) == "Classes"
+ assert foo("2","1") == "Strings"
+ '''
+ }
+
+ // GROOVY-8737
+ void testVargsSelection8() {
+ String methods = '''
+ String m(String key, Object[] args) {
+ "key=$key, args=$args"
+ }
+ String m(String key, Object[] args, Object[] parts) {
+ "key=$key, args=$args, parts=$parts"
+ }
+ String m(String key, Object[] args, String[] names) {
+ "key=$key, args=$args, names=$names"
+ }
+ '''
+ assertScript methods + '''
+ String result = m( 'hello', new Object[]{'world'} ) // exact match
for m(String,Object[])
+ assert result == 'key=hello, args=[world]'
+ '''
+ assertScript methods + '''
+ String result = m( 'hello', new String[]{'world'} )
+ assert result == 'key=hello, args=[world]'
+ '''
+ assertScript methods + '''
+ String result = m( "${'hello'}", 'world' )
+ assert result == 'key=hello, args=[world]'
+ '''
+ assertScript methods + '''
+ String result = m( 'hello', 'world' )
+ assert result == 'key=hello, args=[world]'
+ '''
+
+ assertScript methods + '''
+ String result = m( 'hello', new String[]{'there'}, 'Steve' )
+ assert result == 'key=hello, args=[there], names=[Steve]'
+ '''
+ }
+
// GROOVY-5702
void testShouldFindInterfaceMethod() {
assertScript '''
@@ -1474,41 +1564,6 @@ class MethodCallsSTCTest extends
StaticTypeCheckingTestCase {
'''
}
- // GROOVY-6147
- void testVargsCallWithOverloadedMethod() {
- assertScript '''
- int select(Object a, String s) { 1 }
- int select(Object a, String s, Object[] args) { 2 }
- def o = new Date()
- def s = 'String'
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
- def method =
node.rightExpression.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
- assert method.name == 'select'
- assert method.parameters.length==2
- })
- def result = select(o,s)
- assert result == 1
- '''
- }
-
- // GROOVY-6195
- void testShouldNotThrowAmbiguousVargs() {
- assertScript '''
- def list = ['a', 'b', 'c']
- Object[] arr = list.toArray()
- println arr
- '''
- }
-
- void testOverloadedMethodWithVargs() {
- assertScript '''import
org.codehaus.groovy.classgen.asm.sc.support.Groovy6235SupportSub as Support
- def b = new Support()
- assert b.overload() == 1
- assert b.overload('a') == 1
- assert b.overload('a','b') == 2
- '''
- }
-
// GROOVY-10720
void testOverloadedMethodWithArray() {
assertScript '''
@@ -1552,24 +1607,6 @@ class MethodCallsSTCTest extends
StaticTypeCheckingTestCase {
''', 'Cannot find matching method java.lang.String#doSomething()'
}
- // GROOVY-6646
- void testNPlusVargsCallInOverloadSituation() {
- assertScript '''
- def foo(Class... cs) { "Classes" }
- def foo(String... ss) { "Strings" }
-
- assert foo(List, Map) == "Classes"
- assert foo("2","1") == "Strings"
- '''
- assertScript '''
- def foo(Class<?>... cs) { "Classes" }
- def foo(String... ss) { "Strings" }
-
- assert foo(List, Map) == "Classes"
- assert foo("2","1") == "Strings"
- '''
- }
-
// GROOVY-6776
void testPrimtiveParameterAndNullArgument() {
shouldFailWithMessages '''