Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@
import org.codehaus.groovy.transform.trait.Traits;
import org.objectweb.asm.Opcodes;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -1769,6 +1771,9 @@ static GenericsType[] getGenericsWithoutArray(final ClassNode type) {
return type.getGenericsTypes();
}

private static final ThreadLocal<Deque<GenericsTypeName>> EXPANDING_BOUNDS =
ThreadLocal.withInitial(ArrayDeque::new);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very critical to using a ThreadLocal. At the very least there should be a description of why this has to be ThreadLocal


static GenericsType[] applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final GenericsType[] gts) {
if (gts == null || spec == null || spec.isEmpty()) return gts;

Expand All @@ -1789,9 +1794,22 @@ private static GenericsType applyGenericsContext(final Map<GenericsTypeName, Gen
GenericsType specType = spec.get(name);
if (specType != null) return specType;
if (hasNonTrivialBounds(gt)) {
GenericsType newGT = new GenericsType(type, applyGenericsContext(spec, gt.getUpperBounds()), applyGenericsContext(spec, gt.getLowerBound()));
newGT.setPlaceholder(true);
return newGT;
// GROOVY-11022: avoid infinite recursion on F-bounded type
// parameters whose self-reference appears inside a wildcard
// bound (e.g. `<K extends Comparable<? super K>>`)
Deque<GenericsTypeName> expanding = EXPANDING_BOUNDS.get();
if (expanding.contains(name)) return gt;
expanding.push(name);
try {
GenericsType newGT = new GenericsType(type, applyGenericsContext(spec, gt.getUpperBounds()), applyGenericsContext(spec, gt.getLowerBound()));
newGT.setPlaceholder(true);
return newGT;
} finally {
expanding.pop();
Comment thread
paulk-asert marked this conversation as resolved.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we push to expanding and then pop from expanding. That means we remove the entry we just added, even in the case of line 1806, with the return. Why is it then not always empty again? Ah, because of the recursive call applyGenericsContext(spec, gt.getLowerBound()). But why does it have to be a ThreadLocal then? I would prefere going for a breaking change here nstead of using ThreadLocal

if (expanding.isEmpty()) {
EXPANDING_BOUNDS.remove();
}
}
}
return gt;
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/groovy/groovy/transform/stc/GenericsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4616,6 +4616,43 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
}
}

// GROOVY-11022
@Test
void testNoStackOverflow3() {
// F-bounded type parameter with self-reference inside a `? super` wildcard
assertScript '''
class C<K, V> {
public static <K extends Comparable<? super K>, V> C<K, V> m(K k, V v) {
new C<K, V>()
}
}
class Main {
@groovy.transform.TypeChecked
static test() {
C.<Integer, Integer>m(null, 1)
}
}
Main.test()
'''

// self-reference inside a `? extends` wildcard
assertScript '''
class Self implements Iterable<Self> {
Iterator<Self> iterator() { Collections.emptyIterator() }
}
class C<K> {
public static <K extends Iterable<? extends K>> C<K> m(K k) {
new C<K>()
}
}
@groovy.transform.TypeChecked
void test() {
C.<Self>m(null)
}
Comment thread
paulk-asert marked this conversation as resolved.
test()
'''
}

@Test
void testRegressionInConstructorCheck() {
assertScript '''
Expand Down
Loading