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/12/12 16:07:36 UTC
(groovy) branch GROOVY_3_0_X updated (d8213f1769 -> 98c01ca48b)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
from d8213f1769 GROOVY-9909, GROOVY-10380, GROOVY-10494: SC: fix `super.defaultMethod()`
new da5bfaf838 GROOVY-10094: STC: check the compatibility of method default argument(s)
new 98c01ca48b GROOVY-10325, GROOVY-10326: STC: `map*.value = x` and `map*.key = x`
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../transform/stc/StaticTypeCheckingVisitor.java | 60 +++++++++++++---------
.../transform/stc/TypeCheckingExtension.java | 25 +++++----
.../bugs/{Groovy8046.groovy => Groovy10094.groovy} | 36 +++++++------
.../transform/stc/TypeInferenceSTCTest.groovy | 38 +++++++++++++-
4 files changed, 107 insertions(+), 52 deletions(-)
copy src/test/groovy/bugs/{Groovy8046.groovy => Groovy10094.groovy} (57%)
(groovy) 01/02: GROOVY-10094: STC: check the compatibility of method default argument(s)
Posted by em...@apache.org.
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 da5bfaf838a3e658998710eff14e444f70b2ab9f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Sep 10 13:59:32 2021 -0500
GROOVY-10094: STC: check the compatibility of method default argument(s)
3_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 15 +++++
src/test/groovy/bugs/Groovy10094.groovy | 67 ++++++++++++++++++++++
2 files changed, 82 insertions(+)
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 1a132b69d5..f1c5030785 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2226,6 +2226,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
if (!isSkipMode(node) && !shouldSkipMethodNode(node)) {
super.visitConstructorOrMethod(node, isConstructor);
+
+ if (node.hasDefaultValue()) {
+ for (Parameter parameter : node.getParameters()) {
+ if (!parameter.hasInitialExpression()) continue;
+ // GROOVY-10094: visit param default argument expression
+ visitInitialExpression(parameter.getInitialExpression(), varX(parameter), parameter);
+ // GROOVY-10104: remove direct target setting to prevent errors
+ parameter.getInitialExpression().visit(new CodeVisitorSupport() {
+ @Override
+ public void visitMethodCallExpression(final MethodCallExpression mce) {
+ mce.setMethodTarget(null); super.visitMethodCallExpression(mce);
+ }
+ });
+ }
+ }
}
if (!isConstructor) {
returnAdder.visitMethod(node); // GROOVY-7753: we cannot count these auto-generated return statements, see `typeCheckingContext.pushEnclosingReturnStatement`
diff --git a/src/test/groovy/bugs/Groovy10094.groovy b/src/test/groovy/bugs/Groovy10094.groovy
new file mode 100644
index 0000000000..fb32df911f
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10094.groovy
@@ -0,0 +1,67 @@
+/*
+ * 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 groovy.bugs
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy10094 {
+
+ @Test
+ void testMethodDefaultArgumentSTC() {
+ def err = shouldFail '''
+ @groovy.transform.TypeChecked
+ class C {
+ int m(int i = 'error') {
+ return i
+ }
+ }
+ '''
+
+ assert err =~ /Cannot assign value of type java.lang.String to variable of type int/
+ }
+
+ @Test
+ void testClosureDefaultArgumentSTC() {
+ def err = shouldFail '''
+ @groovy.transform.TypeChecked
+ class C {
+ def c = { int i = 'error' ->
+ return i
+ }
+ }
+ '''
+
+ assert err =~ /Cannot assign value of type java.lang.String to variable of type int/
+ }
+
+ @Test
+ void testConstructorDefaultArgumentSTC() {
+ def err = shouldFail '''
+ @groovy.transform.TypeChecked
+ class C {
+ C(int i = 'error') {
+ }
+ }
+ '''
+
+ assert err =~ /Cannot assign value of type java.lang.String to variable of type int/
+ }
+}
(groovy) 02/02: GROOVY-10325, GROOVY-10326: STC: `map*.value = x` and `map*.key = x`
Posted by em...@apache.org.
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 98c01ca48b3a28befe0cf052791cfde6c9b37ea8
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Dec 12 09:51:55 2023 -0600
GROOVY-10325, GROOVY-10326: STC: `map*.value = x` and `map*.key = x`
3_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 45 ++++++++++------------
.../transform/stc/TypeCheckingExtension.java | 25 ++++++------
.../transform/stc/TypeInferenceSTCTest.groovy | 38 +++++++++++++++++-
3 files changed, 72 insertions(+), 36 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 f1c5030785..ad744ac55d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1217,17 +1217,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
return null;
}
- private static ClassNode adjustTypeForSpreading(final ClassNode inferredRightExpressionType, final Expression leftExpression) {
- // imagine we have: list*.foo = 100
- // then the assignment must be checked against [100], not 100
- ClassNode wrappedRHS = inferredRightExpressionType;
+ private ClassNode adjustTypeForSpreading(final ClassNode rightExpressionType, final Expression leftExpression) {
+ // given "list*.foo = 100" or "map*.value = 100", then the assignment must be checked against [100], not 100
if (leftExpression instanceof PropertyExpression && ((PropertyExpression) leftExpression).isSpreadSafe()) {
- wrappedRHS = LIST_TYPE.getPlainNodeReference();
- wrappedRHS.setGenericsTypes(new GenericsType[]{
- new GenericsType(wrapTypeIfNecessary(inferredRightExpressionType))
- });
+ return extension.buildListType(rightExpressionType);
}
- return wrappedRHS;
+ return rightExpressionType;
}
private boolean addedReadOnlyPropertyError(final Expression expr) {
@@ -1743,10 +1738,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
List<ClassNode> propertyTypes = new ArrayList<>();
if (existsProperty(propX(varX("_", itemType), prop), true, new PropertyLookup(itemType, propertyTypes::add))) {
- gts = new GenericsType[]{new GenericsType(wrapTypeIfNecessary(propertyTypes.get(0)))};
- ClassNode listType = LIST_TYPE.getPlainNodeReference();
- listType.setGenericsTypes(gts);
- return listType;
+ return extension.buildListType(propertyTypes.get(0));
}
return null;
}
@@ -1777,23 +1769,28 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (isOrImplements(testClass, MAP_TYPE)) {
ClassNode mapType = testClass.equals(MAP_TYPE) ? testClass
: GenericsUtils.parameterizeType(testClass, MAP_TYPE);
- GenericsType[] gts = mapType.getGenericsTypes();//<K,V>
- if (gts == null || gts.length != 2) return OBJECT_TYPE;
+ GenericsType[] gts = mapType.getGenericsTypes();//<K,V> params
+ if (gts == null || gts.length != 2) gts = new GenericsType[] {
+ OBJECT_TYPE.asGenericsType(), OBJECT_TYPE.asGenericsType()
+ };
if (!pexp.isSpreadSafe()) {
return getCombinedBoundType(gts[1]);
} else {
// map*.property syntax acts on Entry
switch (pexp.getPropertyAsString()) {
- case "key":
- ClassNode keyList = LIST_TYPE.getPlainNodeReference();
- keyList.setGenericsTypes(new GenericsType[]{gts[0]});
- return keyList;
- case "value":
- ClassNode valueList = LIST_TYPE.getPlainNodeReference();
- valueList.setGenericsTypes(new GenericsType[]{gts[1]});
- return valueList;
- default:
+ case "key":
+ pexp.putNodeMetaData(READONLY_PROPERTY,Boolean.TRUE); // GROOVY-10326
+ return GenericsUtils.makeClassSafe0(LIST_TYPE, gts[0]);
+ case "value":
+ GenericsType v = gts[1];
+ if (!v.isWildcard()
+ && !Modifier.isFinal(v.getType().getModifiers())
+ && typeCheckingContext.isTargetOfEnclosingAssignment(pexp)) {
+ v = GenericsUtils.buildWildcardType(v.getType()); // GROOVY-10325
+ }
+ return GenericsUtils.makeClassSafe0(LIST_TYPE, v);
+ default:
addStaticTypeError("Spread operator on map only allows one of [key,value]", pexp);
}
}
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingExtension.java b/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingExtension.java
index d28d652346..c662a08158 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingExtension.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/TypeCheckingExtension.java
@@ -36,6 +36,7 @@ import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -324,19 +325,21 @@ public class TypeCheckingExtension {
return null;
}
- public ClassNode parameterizedType(ClassNode baseType, ClassNode... genericsTypeArguments) {
+ public ClassNode parameterizedType(final ClassNode baseType, final ClassNode... genericsTypeArguments) {
+ if (baseType.isArray()) {
+ return parameterizedType(baseType.getComponentType(), genericsTypeArguments).makeArray();
+ }
ClassNode result = baseType.getPlainNodeReference();
- if (result.isUsingGenerics()) {
- GenericsType[] gts = new GenericsType[genericsTypeArguments.length];
- int expectedLength = result.getGenericsTypes().length;
- if (expectedLength!=genericsTypeArguments.length) {
- throw new GroovyBugError("Expected number of generic type arguments for "+baseType.toString(false)+" is "+expectedLength
- + " but you gave "+genericsTypeArguments.length);
- }
- for (int i = 0; i < gts.length; i++) {
- gts[i] = new GenericsType(genericsTypeArguments[i]);
+ GenericsType[] generics = baseType.redirect().getGenericsTypes();
+ if (generics != null) {
+ int expectedLength = generics.length;
+ if (expectedLength != genericsTypeArguments.length) {
+ throw new GroovyBugError("Expected number of generic type arguments for " + StaticTypeCheckingSupport.prettyPrintType(baseType) + " is " + expectedLength + " but you gave " + genericsTypeArguments.length);
}
- result.setGenericsTypes(gts);
+ result.setGenericsTypes(Arrays.stream(genericsTypeArguments)
+ .map(StaticTypeCheckingVisitor::wrapTypeIfNecessary)
+ .map(GenericsType::new).toArray(GenericsType[]::new)
+ );
}
return result;
}
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 7254e4c21c..575cda5f58 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -845,7 +845,8 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
a.with {
method().toUpperCase()
}
- ''', 'Cannot find matching method int#toUpperCase()'
+ ''',
+ 'Cannot find matching method int#toUpperCase()'
}
void testDeclarationTypeInference() {
@@ -945,6 +946,41 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'Cannot find matching method java.lang.Integer#toUpperCase()'
}
+ void testStarOperatorOnMap4() {
+ assertScript '''
+ def map = [x:1,y:2,z:3]
+ map*.value = 0
+
+ assert map*.value == [0,0,0]
+ '''
+
+ assertScript '''
+ Map<String,? extends Object> map = [x:1,y:2,z:3]
+ map*.value = 0
+
+ assert map*.value == [0,0,0]
+ '''
+
+ // GROOVY-10325
+ assertScript '''
+ Map<String,Object> map = [x:1,y:2,z:3]
+ map*.value = 0 // was: Cannot assign List<Integer> to List<Object>
+
+ assert map*.value == [0,0,0]
+ '''
+
+ shouldFailWithMessages '''
+ [x:1,y:2,z:3]*.value = ""
+ ''',
+ 'Cannot assign java.util.List <java.lang.String> to: java.util.List <java.lang.Integer>'
+
+ // GROOVY-10326
+ shouldFailWithMessages '''
+ [x:1,y:2,z:3]*.key = null
+ ''',
+ 'Cannot set read-only property: key'
+ }
+
// GROOVY-7247
void testStarOperatorOnMapKey() {
assertScript '''