Skip to content

Commit

Permalink
GROOVY-10153
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 23, 2022
1 parent c49c190 commit 0817dcf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4271,6 +4271,36 @@ public void testTypeChecked10128() {
runConformTest(sources);
}

@Test
public void testTypeChecked10153() {
//@formatter:off
String[] sources = {
"Main.groovy",
"class A {}\n" +
"class B extends A {}\n" +
"class C extends B {}\n" +
"class Foo<T extends A> {}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" Foo<? super C> foo = new Foo<B>()\n" + // ? is not a valid substitute for <T extends A>
"}\n" +
"test()\n",
};
//@formatter:on

if (isAtLeastGroovy(30)) {
runConformTest(sources);
} else {
runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 7)\n" +
"\tFoo<? super C> foo = new Foo<B>()\n" +
"\t ^^^^^^^^^\n" +
"Groovy:The type ? is not a valid substitute for the bounded parameter <T extends A>\n" +
"----------\n");
}
}

@Test
public void testTypeChecked10166() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

Expand Down Expand Up @@ -544,7 +545,9 @@ protected boolean resolve(final ClassNode type, final boolean testModuleImports,
type.setGenericsPlaceHolder(true);
return true;
}

// GRECLIPSE add
try {
// GRECLIPSE end
if (currentClass.getNameWithoutPackage().equals(typeName)) {
type.setRedirect(currentClass);
return true;
Expand All @@ -556,6 +559,21 @@ protected boolean resolve(final ClassNode type, final boolean testModuleImports,
(testDefaultImports && !type.hasPackageName() && resolveFromDefaultImports(type, true)) ||
resolveToOuter(type) ||
(testStaticInnerClasses && type.hasPackageName() && resolveFromStaticInnerClasses(type, true));
// GRECLIPSE add -- GROOVY-10153: deal with "Foo<? super Bar> -> Foo<T extends Baz>"
} finally {
if (type.isRedirectNode()) Optional.ofNullable(type.getGenericsTypes()).ifPresent(arguments -> {
for (int i = 0, n = arguments.length; i < n; i += 1) { GenericsType argument = arguments[i];
if (!argument.isWildcard() || argument.getUpperBounds() != null) continue;
GenericsType[] parameters = type.redirect().getGenericsTypes();
if (parameters != null && i < parameters.length) {
ClassNode implicitBound = parameters[i].getType();
if (!implicitBound.equals(ClassHelper.OBJECT_TYPE))
argument.getType().setRedirect(implicitBound);
}
}
});
}
// GRECLIPSE end
}

protected boolean resolveNestedClass(final ClassNode type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

Expand Down Expand Up @@ -457,7 +458,9 @@ protected boolean resolve(final ClassNode type, final boolean testModuleImports,
type.setGenericsPlaceHolder(true);
return true;
}

// GRECLIPSE add
try {
// GRECLIPSE end
if (currentClass.getNameWithoutPackage().equals(typeName)) {
type.setRedirect(currentClass);
return true;
Expand All @@ -469,6 +472,21 @@ protected boolean resolve(final ClassNode type, final boolean testModuleImports,
(testDefaultImports && !type.hasPackageName() && resolveFromDefaultImports(type, true)) ||
resolveToOuter(type) ||
(testStaticInnerClasses && type.hasPackageName() && resolveFromStaticInnerClasses(type, true));
// GRECLIPSE add -- GROOVY-10153: deal with "Foo<? super Bar> -> Foo<T extends Baz>"
} finally {
if (type.isRedirectNode()) Optional.ofNullable(type.getGenericsTypes()).ifPresent(arguments -> {
for (int i = 0, n = arguments.length; i < n; i += 1) { GenericsType argument = arguments[i];
if (!argument.isWildcard() || argument.getUpperBounds() != null) continue;
GenericsType[] parameters = type.redirect().getGenericsTypes();
if (parameters != null && i < parameters.length) {
ClassNode implicitBound = parameters[i].getType();
if (!isObjectType(implicitBound))
argument.getType().setRedirect(implicitBound);
}
}
});
}
// GRECLIPSE end
}

protected boolean resolveNestedClass(final ClassNode type) {
Expand Down

0 comments on commit 0817dcf

Please sign in to comment.