Skip to content

Commit

Permalink
GROOVY-10356, GROOVY-10623: STC: inferred variable type following null
Browse files Browse the repository at this point in the history
3_0_X backport
  • Loading branch information
eric-milles committed Nov 30, 2023
1 parent 61287d6 commit 53f48ee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4442,11 +4442,12 @@ public void visitTryCatchFinally(final TryCatchStatement statement) {
}

protected void storeType(final Expression exp, ClassNode cn) {
if (cn == UNKNOWN_PARAMETER_TYPE) {
// this can happen for example when "null" is used in an assignment or a method parameter.
// In that case, instead of storing the virtual type, we must "reset" type information
if (cn == UNKNOWN_PARAMETER_TYPE) { // null for assignment or parameter
// instead of storing an "unknown" type, reset the type information
// by determining the declaration type of the expression
cn = getOriginalDeclarationType(exp);
// GROOVY-10356, GROOVY-10623 : "def"
if (cn == DYNAMIC_TYPE) return;
}
if (cn != null && isPrimitiveType(cn)) {
if (exp instanceof VariableExpression && ((VariableExpression) exp).isClosureSharedVariable()) {
Expand Down
1 change: 0 additions & 1 deletion src/test/groovy/transform/stc/ClosuresSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
}

// GROOVY-10356
@NotYetImplemented
void testClosureSharedVariable4() {
assertScript '''
interface A {
Expand Down
35 changes: 25 additions & 10 deletions src/test/groovy/transform/stc/STCAssignmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1009,18 +1009,22 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'Inconvertible types: cannot cast java.lang.String[] to java.util.Set[]'
}

// GROOVY-5535
// GROOVY-5535, GROOVY-10623
void testAssignToNullInsideIf() {
assertScript '''
Date test() {
Date x = new Date()
if (true) {
x = null
['Date', 'def', 'var'].each {
assertScript """
Date test() {
$it x = new Date()
if (true) {
x = null
Date y = x
}
Date z = x
return x
}
x
}
assert test() == null
'''
assert test() == null
"""
}
}

// GROOVY-10294
Expand Down Expand Up @@ -1050,6 +1054,17 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-10623
void testAssignToNullAfterInit() {
assertScript '''
class C {
}
def x = new C()
x = null
C c = x
'''
}

// GROOVY-5798
void testShouldNotThrowConversionError() {
assertScript '''
Expand Down

0 comments on commit 53f48ee

Please sign in to comment.