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 ba00810ed70cef52448bf742e62bcc32447ef6b1 Author: Eric Milles <[email protected]> AuthorDate: Sat May 15 18:55:11 2021 -0500 GROOVY-10087: STC: char can be implicitly boxed to Character at return Conflicts: src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java --- .../transform/stc/StaticTypeCheckingSupport.java | 19 ++++----------- .../groovy/transform/stc/ReturnsSTCTest.groovy | 27 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 14 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 a934833..2692f16 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java @@ -666,27 +666,18 @@ public abstract class StaticTypeCheckingSupport { } } - // if rightExpression is null and leftExpression is not a primitive type, it's ok boolean rightExpressionIsNull = isNullConstant(rightExpression); if (rightExpressionIsNull && !isPrimitiveType(left)) { return true; } - // on an assignment everything that can be done by a GroovyCast is allowed - - // anything can be assigned to an Object, String, Boolean or Class typed variable + // anything can be assigned to an Object, String, [Bb]oolean or Class receiver; except null to boolean if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true; - // char as left expression - if (leftRedirect == char_TYPE) { - if (rightRedirect == Character_TYPE) return true; - if (rightRedirect == STRING_TYPE && rightExpression instanceof ConstantExpression) { - String value = rightExpression.getText(); - return value.length() == 1; - } - } - if (leftRedirect == Character_TYPE && (rightRedirect == STRING_TYPE || rightExpressionIsNull)) { - return rightExpressionIsNull || (rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1); + if (leftRedirect == char_TYPE && rightRedirect == Character_TYPE) return true; + if (leftRedirect == Character_TYPE && rightRedirect == char_TYPE) return true; + if ((leftRedirect == char_TYPE || leftRedirect == Character_TYPE) && rightRedirect == STRING_TYPE) { + return rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1; } // if left is Enum and right is String or GString we do valueOf diff --git a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy index f4cd334..e5e3a24 100644 --- a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy +++ b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy @@ -223,6 +223,33 @@ class ReturnsSTCTest extends StaticTypeCheckingTestCase { ''' } + // GROOVY-10087 + void testImplicitReturnToWrapper() { + assertScript ''' + Integer foo() { + int x = 42 + return x + } + assert foo().intValue() == 42 + ''' + + assertScript ''' + Long foo() { + long x = 42L + return x + } + assert foo().longValue() == 42L + ''' + + assertScript ''' + Character foo() { + char x = 'x' + return x + } + assert foo().charValue() == 'x' + ''' + } + // GROOVY-5835 void testReturnInClosureShouldNotBeConsideredAsReturnOfEnclosingMethod() { assertScript '''
