You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2023/01/12 21:33:23 UTC
[groovy] 01/02: GROOVY-10062: STC: combine variadic method return type inference checks
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 21515c9efe4970075389aee201143d41c2f8d2ad
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Jan 12 15:28:57 2023 -0600
GROOVY-10062: STC: combine variadic method return type inference checks
3_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 21 +++++------
src/test/groovy/bugs/Groovy10056.groovy | 44 ----------------------
.../groovy/transform/stc/GenericsSTCTest.groovy | 1 -
3 files changed, 9 insertions(+), 57 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 31c1d6a851..38ad511812 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5383,19 +5383,19 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
} else if (nParameters > 0) { // resolve type parameters from call arguments
List<Expression> expressions = InvocationWriter.makeArgumentList(arguments).getExpressions();
+ int nArguments = expressions.size();
boolean isVargs = isVargs(parameters);
- if (expressions.size() >= nParameters) {
- for (int i = 0; i < nParameters; i += 1) {
+ if (isVargs ? nArguments >= nParameters-1 : nArguments == nParameters) {
+ for (int i = 0; i < nArguments; i += 1) {
if (isNullConstant(expressions.get(i)))
continue; // GROOVY-9984: skip null
- boolean lastArg = (i == nParameters - 1);
- ClassNode paramType = parameters[i].getType();
ClassNode argumentType = getDeclaredOrInferredType(expressions.get(i));
- while (paramType.isArray() && argumentType.isArray()) {
- paramType = paramType.getComponentType();
- argumentType = argumentType.getComponentType();
- }
- if (isUsingGenericsOrIsArrayUsingGenerics(paramType)) {
+ ClassNode paramType = parameters[Math.min(i, nParameters-1)].getType();
+ if (GenericsUtils.hasUnresolvedGenerics(paramType)) {
+ // if supplying array param with multiple arguments or single non-array argument, infer using element type
+ if (isVargs && (i >= nParameters || (i == nParameters-1 && (nArguments > nParameters || !argumentType.isArray())))) {
+ paramType = paramType.getComponentType();
+ }
if (argumentType.isDerivedFrom(CLOSURE_TYPE)) {
MethodNode sam = findSAM(paramType);
if (sam != null) { // implicit closure coercion in action!
@@ -5404,9 +5404,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
applyGenericsContextToParameterClass(resolvedPlaceholders, paramType));
}
}
- if (isVargs && lastArg && paramType.isArray() && !argumentType.isArray()) {
- paramType = paramType.getComponentType();
- }
Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
extractGenericsConnections(connections, wrapTypeIfNecessary(argumentType), paramType);
connections.forEach((name, type) -> resolvedPlaceholders.merge(name, type, StaticTypeCheckingSupport::getCombinedGenericsType));
diff --git a/src/test/groovy/bugs/Groovy10056.groovy b/src/test/groovy/bugs/Groovy10056.groovy
deleted file mode 100644
index aa8cc89ed5..0000000000
--- a/src/test/groovy/bugs/Groovy10056.groovy
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package bugs
-
-import org.junit.Test
-
-import static groovy.test.GroovyAssert.assertScript
-
-final class Groovy10056 {
- @Test
- void test() {
- assertScript '''\
- @groovy.transform.CompileStatic
- def test() {
- String[][] strsArray = new String[][] {
- new String[] {'a', 'b', 'c'},
- new String[] {'d', 'e', 'f'}
- }
- Arrays.stream(strsArray)
- .limit(1)
- .forEach(strs -> {
- assert ['a', 'b', 'c'] == Arrays.asList(strs)
- })
- }
- test()
- '''
- }
-}
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 873eb4f1f9..458fd9e5c0 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -216,7 +216,6 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
}
// GROOVY-10062
- @NotYetImplemented
void testReturnTypeInferenceWithMethodGenerics2() {
assertScript '''
def <T> T m(T t, ... zeroOrMore) {