Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@
* type information from node metadata generated by the static type checker.
*/
public class StaticTypesTypeChooser extends StatementMetaTypeChooser {

@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
ASTNode target = getTarget(exp); // see GROOVY-9344, GROOVY-9607

ClassNode inferredType = target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
ClassNode inferredType = exp.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
if (inferredType == null) {
inferredType = exp.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
}
if (inferredType == null) {
inferredType = target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
ASTNode node = getTarget(exp); // GROOVY-9344, GROOVY-9607
inferredType = node.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
}
if (inferredType != null && !isPrimitiveVoid(inferredType)) {
return inferredType;
}

// AsmClassGenerator may create "this" expressions that the type checker knows nothing about
if (target instanceof VariableExpression && ((VariableExpression) target).isThisExpression()) {
// AsmClassGenerator creates "this" expressions that the type checker knows nothing about
if (exp instanceof VariableExpression && ((VariableExpression) exp).isThisExpression()) {
return current;
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,21 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'''
}

void testCastObjectToInterface() {
assertScript '''
List<Object> m(object) {
if (object == null) return new ArrayList<>()
if (object instanceof Collection) return new ArrayList<Object>((Collection) object)
return [object]
}
def item = 0
def list = [1,2]
assert m(null).size() == 0
assert m(item).size() == 1
assert m(list).size() == 2
'''
}

void testIfElseBranch() {
shouldFailWithMessages '''
def x
Expand Down
20 changes: 20 additions & 0 deletions src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ class STCnAryExpressionTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-11375
void testShiftOnPrimitivesVariableFlowType() {
assertScript '''
def x = "--"
x = x.size()
def y = x << x
assert y === 8
'''
}

// GROOVY-11375
void testPowerOnPrimitivesVariableFlowType() {
assertScript '''
def x = "--"
x = x.size()
def y = x ** x
assert y === 4
'''
}

// GROOVY-5644
void testSpaceshipOperatorNoAmbiguousError() {
assertScript '''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ final class AssignmentsStaticCompileTest extends STCAssignmentTest implements St
String bytecode = astTrees['C'][1]
assert !bytecode.contains('ScriptBytecodeAdapter.setGroovyObjectProperty') : '"c.i += 10" should use setter, not dynamic property'
}

@Override // GROOVY-11769
void testCastObjectToInterface() {
super.testCastObjectToInterface()
String bytecode = astTrees.values()[0][1]
bytecode = bytecode.substring(bytecode.indexOf('m(Ljava/lang/Object;)'))
assert bytecode.count('CHECKCAST') == 1 // guarded typecast isn't groovy
}
}
Loading