Github user melix commented on a diff in the pull request: https://github.com/apache/groovy/pull/635#discussion_r150800145 --- Diff: src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java --- @@ -3461,6 +3465,36 @@ public void visitTernaryExpression(final TernaryExpression expression) { popAssignmentTracking(oldTracker); } + // currently just for empty literals, not for e.g. Collections.emptyList() at present + /// it seems attractive to want to do this for more cases but perhaps not all cases + private ClassNode checkForTargetType(final Expression expr, final ClassNode type) { + if (typeCheckingContext.getEnclosingBinaryExpression() != null && isEmptyCollection(expr)) { + int op = typeCheckingContext.getEnclosingBinaryExpression().getOperation().getType(); + if (isAssignment(op)) { + VariableExpression target = (VariableExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression(); + return adjustForTargetType(target.getType(), type); + } + } + return type; + } + + private ClassNode adjustForTargetType(final ClassNode targetType, final ClassNode resultType) { + if (targetType.isUsingGenerics() && missesGenericsTypes(resultType)) { + // unchecked assignment within ternary/elvis + // examples: + // List<A> list = existingAs ?: [] + // in that case, the inferred type of the RHS is the type of the RHS + // "completed" with generics type information available in the LHS + return GenericsUtils.parameterizeType(targetType, resultType.getPlainNodeReference()); + } + return resultType; + } + + private boolean isEmptyCollection(Expression expr) { --- End diff -- Could be static.
---