[ https://issues.apache.org/jira/browse/GROOVY-8255?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16251237#comment-16251237 ]
ASF GitHub Bot commented on GROOVY-8255: ---------------------------------------- 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. > Odd problems with flow typing and generics in Groovy 2.4.12+ > ------------------------------------------------------------ > > Key: GROOVY-8255 > URL: https://issues.apache.org/jira/browse/GROOVY-8255 > Project: Groovy > Issue Type: Bug > Components: Static Type Checker > Affects Versions: 2.4.12 > Reporter: Graeme Rocher > Assignee: Paul King > > In order to get the GORM codebase to compile I had to make this change: > https://github.com/grails/grails-data-mapping/commit/1ef850c496d13d8ca915b27e76b6bfdb4e27377e > The code in question is: > {code} > /** > * Sets multipart values within the request body > * > * @param name The name of the multipart > * @param value The value of the multipart > */ > void setProperty(String name, value) { > if (value instanceof File) { > value = new FileSystemResource(value) > } > else if (value instanceof URL) { > value = new UrlResource(value) > } > else if (value instanceof InputStream) { > value = new InputStreamResource(value) > } > else if (value instanceof GString) { > value = value.toString() > } > if( mvm[name] ) { > mvm[name].add value > } > else { > mvm.put(name, [value]) // <--- FAILS COMPILATION HERE > } > } > {code} > No matter what I tried I could not get it into to compile. The method accepts > `put(String, List<Object>)` but fails compilation with: > {code} > RequestCustomizer.groovy: 392: [Static type checking] - Cannot call > org.springframework.util.MultiValueMap <String, Object>#put(java.lang.String, > java.lang.Object) with arguments [java.lang.String, java.util.List > <java.lang.String>] > @ line 392, column 13. > mvm.put(name, [value]) > ^ > {code} > Altering the code to: > {code} > List<Object> values = [value] > mvm.put(name, values) > {code} > Fails with: > {code} > RequestCustomizer.groovy: 392: [Static type checking] - Incompatible generic > argument types. Cannot assign java.util.List <java.lang.String> to: > java.util.List <Object> > @ line 392, column 35. > List<Object> values = [value] > ^ > RequestCustomizer.groovy: 393: [Static type checking] - Cannot call > org.springframework.util.MultiValueMap <String, Object>#put(java.lang.String, > java.lang.Object) with arguments [java.lang.String, java.util.List > <java.lang.String>] > @ line 393, column 13. > mvm.put(name, values) > ^ > 2 errors > {code} -- This message was sent by Atlassian JIRA (v6.4.14#64029)