diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java index 23728a3673..bd1cbaae44 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java @@ -5481,6 +5481,28 @@ public void testCompileStatic9883() { "----------\n"); } + @Test + public void testCompileStatic9885() { + //@formatter:off + String[] sources = { + "Main.groovy", + "@groovy.transform.ToString\n" + + "class C {\n" + + " String p\n" + + "}\n" + + "@groovy.transform.CompileStatic\n" + + "void test(String string, whatever) {\n" + + " print new C(p: string.trim() ?: \"$whatever\")\n" + + "}\n" + + "test('x','y')\n" + + "test(' ','y')\n" + + "test(' ',123)\n", + }; + //@formatter:on + + runConformTest(sources, "C(x)C(y)C(123)"); + } + @Test public void testCompileStatic9892() { //@formatter:off diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 71e1543e83..f37ec0b096 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -1355,7 +1355,15 @@ protected void checkGroovyConstructorMap(final Expression receiver, final ClassN ClassNode valueType = getType(entryExpression.getValueExpression()); MethodNode setter = receiverType.getSetterMethod("set" + MetaClassHelper.capitalize(pexp.getPropertyAsString()), false); ClassNode toBeAssignedTo = setter == null ? lookup.get() : setter.getParameters()[0].getType(); + /* GRECLIPSE edit -- GROOVY-9885 if (!isAssignableTo(valueType, toBeAssignedTo) + */ + Expression valueExpression = entryExpression.getValueExpression(); + BinaryExpression assign = new BinaryExpression(keyExpr, GeneralUtils.ASSIGN, valueExpression); + assign.setSourcePosition(entryExpression); + ClassNode resultType = getResultType(toBeAssignedTo, ASSIGN, valueType, assign); + if (!checkCompatibleAssignmentTypes(toBeAssignedTo, resultType, valueExpression) + // GRECLIPSE end && !extension.handleIncompatibleAssignment(toBeAssignedTo, valueType, entryExpression)) { addAssignmentError(toBeAssignedTo, valueType, entryExpression); } diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 3f6270930b..d2ac39c0c5 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -94,6 +94,7 @@ import org.codehaus.groovy.ast.stmt.SwitchStatement; import org.codehaus.groovy.ast.stmt.TryCatchStatement; import org.codehaus.groovy.ast.stmt.WhileStatement; +import org.codehaus.groovy.ast.tools.GeneralUtils; import org.codehaus.groovy.ast.tools.GenericsUtils; import org.codehaus.groovy.ast.tools.WideningCategories; import org.codehaus.groovy.classgen.ReturnAdder; @@ -1314,7 +1315,15 @@ protected void checkGroovyConstructorMap(final Expression receiver, final ClassN ClassNode valueType = getType(entryExpression.getValueExpression()); MethodNode setter = receiverType.getSetterMethod(getSetterName(pexp.getPropertyAsString()), false); ClassNode toBeAssignedTo = setter == null ? lookup.get() : setter.getParameters()[0].getType(); + /* GRECLIPSE edit -- GROOVY-9885 if (!isAssignableTo(valueType, toBeAssignedTo) + */ + Expression valueExpression = entryExpression.getValueExpression(); + BinaryExpression assign = new BinaryExpression(keyExpr, GeneralUtils.ASSIGN, valueExpression); + assign.setSourcePosition(entryExpression); + ClassNode resultType = getResultType(toBeAssignedTo, ASSIGN, valueType, assign); + if (!checkCompatibleAssignmentTypes(toBeAssignedTo, resultType, valueExpression) + // GRECLIPSE end && !extension.handleIncompatibleAssignment(toBeAssignedTo, valueType, entryExpression)) { addAssignmentError(toBeAssignedTo, valueType, entryExpression); } diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 2d1d8f5442..a0a1575c59 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -94,6 +94,7 @@ import org.codehaus.groovy.ast.stmt.SwitchStatement; import org.codehaus.groovy.ast.stmt.TryCatchStatement; import org.codehaus.groovy.ast.stmt.WhileStatement; +import org.codehaus.groovy.ast.tools.GeneralUtils; import org.codehaus.groovy.ast.tools.GenericsUtils; import org.codehaus.groovy.ast.tools.WideningCategories; import org.codehaus.groovy.classgen.ReturnAdder; @@ -1275,10 +1276,7 @@ protected void typeCheckAssignment(final BinaryExpression assignmentExpression, ClassNode wrappedRHS = adjustTypeForSpreading(inferredRightExpressionType, leftExpression); // check types are compatible for assignment - boolean compatible = checkCompatibleAssignmentTypes(leftRedirect, wrappedRHS, rightExpression); - - - if (!compatible) { + if (!checkCompatibleAssignmentTypes(leftRedirect, wrappedRHS, rightExpression)) { if (!extension.handleIncompatibleAssignment(leftExpressionType, inferredRightExpressionType, assignmentExpression)) { addAssignmentError(leftExpressionType, inferredRightExpressionType, assignmentExpression.getRightExpression()); } @@ -1309,7 +1307,15 @@ protected void checkGroovyConstructorMap(final Expression receiver, final ClassN ClassNode valueType = getType(entryExpression.getValueExpression()); MethodNode setter = receiverType.getSetterMethod(getSetterName(pexp.getPropertyAsString()), false); ClassNode toBeAssignedTo = setter == null ? lookup.get() : setter.getParameters()[0].getType(); + /* GRECLIPSE edit -- GROOVY-9885 if (!isAssignableTo(valueType, toBeAssignedTo) + */ + Expression valueExpression = entryExpression.getValueExpression(); + BinaryExpression assign = new BinaryExpression(keyExpr, GeneralUtils.ASSIGN, valueExpression); + assign.setSourcePosition(entryExpression); + ClassNode resultType = getResultType(toBeAssignedTo, ASSIGN, valueType, assign); + if (!checkCompatibleAssignmentTypes(toBeAssignedTo, resultType, valueExpression) + // GRECLIPSE end && !extension.handleIncompatibleAssignment(toBeAssignedTo, valueType, entryExpression)) { addAssignmentError(toBeAssignedTo, valueType, entryExpression); }