Skip to content

Commit

Permalink
GROOVY-9885
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 16, 2021
1 parent 3bf39d8 commit b84587b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b84587b

Please sign in to comment.