You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/03/15 20:58:56 UTC
[groovy] branch master updated: GROOVY-10114: STC: diamond inference: handle ternary without target type
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 4869241 GROOVY-10114: STC: diamond inference: handle ternary without target type
4869241 is described below
commit 4869241faded5ef0d75636887840608055c9f76e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Mar 15 15:33:04 2022 -0500
GROOVY-10114: STC: diamond inference: handle ternary without target type
---
.../transform/stc/StaticTypeCheckingVisitor.java | 11 ++++++++---
.../groovy/transform/stc/GenericsSTCTest.groovy | 22 +++++++++++++++++++++-
2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 6d6c4cd..79b7426 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4185,11 +4185,16 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
targetType = enclosingMethod.getReturnType();
}
+ if (expr instanceof ConstructorCallExpression) { // GROOVY-9972, GROOVY-9983
+ // GROOVY-10114: type parameter(s) could be inferred from call arguments
+ if (targetType == null) targetType = sourceType.getPlainNodeReference();
+ inferDiamondType((ConstructorCallExpression) expr, targetType);
+ return sourceType;
+ }
+
if (targetType == null) return sourceType;
- if (expr instanceof ConstructorCallExpression) {
- inferDiamondType((ConstructorCallExpression) expr, targetType);
- } else if (!isPrimitiveType(getUnwrapper(targetType))
+ if (!isPrimitiveType(getUnwrapper(targetType))
&& !isObjectType(targetType) && missesGenericsTypes(sourceType)) {
// unchecked assignment with ternary/elvis, like "List<T> list = listOfT ?: []"
// the inferred type is the RHS type "completed" with generics information from LHS
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 1caa599..c301b93 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -989,7 +989,8 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
C.m(new A<>(new B()))
C.m(new A<>(new E()))
C.m(flag ? new A<>(new B()) : new A<>(new B()))
- C.m(flag ? new A<>(new B()) : new A<>(new E())) // Cannot call m(A<B>) with arguments [A<? extends B>]
+ C.m(flag ? new A<>(new B()) : new A<>((B)null))
+ C.m(flag ? new A<>(new B()) : new A<>((B)new E())) // Cannot call m(A<B>) with arguments [A<? extends B>]
'''
shouldFailWithMessages types + '''
@@ -1005,6 +1006,25 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
'Cannot call C#m(A<B>) with arguments [A<? extends java.lang.Object>]'
}
+ // GROOVY-10114
+ void testDiamondInferrenceFromConstructor14a() {
+ assertScript '''
+ @groovy.transform.TupleConstructor(defaults=false)
+ class A<T> {
+ T p
+ }
+ class B {
+ Character m() {
+ (Character) '!'
+ }
+ }
+ (false ? new A<B>(new B()) : new A<>(new B())).p.m()
+ (false ? new A< >(new B()) : new A<>(new B())).p.m()
+ def a = (true ? new A<>(new B()) : new A<>(new B()))
+ assert a.p.m() == (char)'!'
+ '''
+ }
+
// GROOVY-9995
void testDiamondInferrenceFromConstructor15() {
[