Skip to content

Commit

Permalink
GROOVY-10698
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 24, 2022
1 parent fcc22a7 commit df1d8f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6022,6 +6022,28 @@ public void testTypeChecked10673() {
runConformTest(sources, "1");
}

@Test
public void testTypeChecked10698() {
//@formatter:off
String[] sources = {
"Main.groovy",
"class A<T> {\n" +
" A(T t, B<T> b_of_t) {\n" +
" }\n" +
"}\n" +
"class B<U> {\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" new A<>('string', new B<>())\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testTypeChecked10699() {
for (String type : new String[] {"java.util.function.Function<T,T>", "java.util.function.UnaryOperator<T>"}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2788,11 +2788,18 @@ && findMethod(receiver, "<init>", init(argumentTypes)).size() == 1) {
&& parameters.length == argumentTypes.length - 1) {
ctor = typeCheckMapConstructor(call, receiver, arguments);
} else {
/* GRECLIPSE edit -- GROOVY-10698
if (parameters.length > 0 && asBoolean(receiver.getGenericsTypes())) { // GROOVY-10283, GROOVY-10316, GROOVY-10482, GROOVY-10624, et al.
Map<GenericsTypeName, GenericsType> context = extractPlaceHolders(null, receiver, ctor.getDeclaringClass());
parameters = parameters.clone(); for (int i = 0; i < parameters.length; i += 1)
parameters[i] = new Parameter(applyGenericsContext(context, parameters[i].getType()), parameters[i].getName());
}
*/
GenericsType[] typeParameters = ctor.getDeclaringClass().getGenericsTypes(); if (typeParameters != null && typeParameters.length > 0) {
Map<GenericsTypeName, GenericsType> context = extractGenericsConnectionsFromArguments(typeParameters, parameters, argumentList, receiver.getGenericsTypes());
if (!context.isEmpty()) parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
// GRECLIPSE end
resolvePlaceholdersFromImplicitTypeHints(argumentTypes, argumentList, parameters);
typeCheckMethodsWithGenericsOrFail(receiver, argumentTypes, ctor, call);
visitMethodCallArguments(receiver, argumentList, true, ctor);
Expand Down Expand Up @@ -6203,7 +6210,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
private Map<GenericsTypeName, GenericsType> extractGenericsConnectionsFromArguments(final GenericsType[] methodGenericTypes, final Parameter[] parameters, final Expression arguments, final GenericsType[] explicitTypeHints) {
Map<GenericsTypeName, GenericsType> resolvedPlaceholders = new HashMap<>();

if (explicitTypeHints != null) { // resolve type parameters from type arguments
if (asBoolean(explicitTypeHints)) { // resolve type parameters from type arguments
int n = methodGenericTypes.length;
if (n == explicitTypeHints.length) {
for (int i = 0; i < n; i += 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2615,10 +2615,17 @@ && findMethod(receiver, "<init>", init(argumentTypes)).size() == 1) {
&& parameters.length == argumentTypes.length - 1) {
ctor = typeCheckMapConstructor(call, receiver, arguments);
} else {
/* GRECLIPSE edit -- GROOVY-10698
if (asBoolean(receiver.getGenericsTypes())) { // GROOVY-10283, GROOVY-10316, GROOVY-10482, GROOVY-10624, et al.
Map<GenericsTypeName, GenericsType> context = extractPlaceHolders(null, receiver, ctor.getDeclaringClass());
parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
*/
GenericsType[] typeParameters = ctor.getDeclaringClass().getGenericsTypes(); if (typeParameters != null && typeParameters.length > 0) {
Map<GenericsTypeName, GenericsType> context = extractGenericsConnectionsFromArguments(typeParameters, parameters, argumentList, receiver.getGenericsTypes());
if (!context.isEmpty()) parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
// GRECLIPSE end
resolvePlaceholdersFromImplicitTypeHints(argumentTypes, argumentList, parameters);
typeCheckMethodsWithGenericsOrFail(receiver, argumentTypes, ctor, call);
visitMethodCallArguments(receiver, argumentList, true, ctor);
Expand Down Expand Up @@ -5891,7 +5898,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
private Map<GenericsTypeName, GenericsType> extractGenericsConnectionsFromArguments(final GenericsType[] methodGenericTypes, final Parameter[] parameters, final Expression arguments, final GenericsType[] explicitTypeHints) {
Map<GenericsTypeName, GenericsType> resolvedPlaceholders = new HashMap<>();

if (explicitTypeHints != null) { // resolve type parameters from type arguments
if (asBoolean(explicitTypeHints)) { // resolve type parameters from type arguments
int n = methodGenericTypes.length;
if (n == explicitTypeHints.length) {
for (int i = 0; i < n; i += 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2314,10 +2314,17 @@ && findMethod(receiver, "<init>", init(argumentTypes)).size() == 1) {
&& parameters.length == argumentTypes.length - 1) {
ctor = typeCheckMapConstructor(call, receiver, arguments);
} else {
/* GRECLIPSE edit -- GROOVY-10698
if (asBoolean(receiver.getGenericsTypes())) { // GROOVY-10283, GROOVY-10316, GROOVY-10482, GROOVY-10624
Map<GenericsTypeName, GenericsType> context = extractPlaceHolders(receiver, ctor.getDeclaringClass());
parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
*/
GenericsType[] typeParameters = ctor.getDeclaringClass().getGenericsTypes(); if (typeParameters != null && typeParameters.length > 0) {
Map<GenericsTypeName, GenericsType> context = extractGenericsConnectionsFromArguments(typeParameters, parameters, argumentList, receiver.getGenericsTypes());
if (!context.isEmpty()) parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
// GRECLIPSE end
resolvePlaceholdersFromImplicitTypeHints(argumentTypes, argumentList, parameters);
typeCheckMethodsWithGenericsOrFail(receiver, argumentTypes, ctor, call);
visitMethodCallArguments(receiver, argumentList, true, ctor);
Expand Down Expand Up @@ -5384,7 +5391,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
private Map<GenericsTypeName, GenericsType> extractGenericsConnectionsFromArguments(final GenericsType[] methodGenericTypes, final Parameter[] parameters, final Expression arguments, final GenericsType[] explicitTypeHints) {
Map<GenericsTypeName, GenericsType> resolvedPlaceholders = new HashMap<>();

if (explicitTypeHints != null) { // resolve type parameters from type arguments
if (asBoolean(explicitTypeHints)) { // resolve type parameters from type arguments
int n = methodGenericTypes.length;
if (n == explicitTypeHints.length) {
for (int i = 0; i < n; i += 1) {
Expand Down

0 comments on commit df1d8f9

Please sign in to comment.