-
Notifications
You must be signed in to change notification settings - Fork 1.9k
GROOVY-11022: StackOverflowError when having parameterized function w… #2496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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); | ||
|
|
||
| static GenericsType[] applyGenericsContext(final Map<GenericsTypeName, GenericsType> spec, final GenericsType[] gts) { | ||
| if (gts == null || spec == null || spec.isEmpty()) return gts; | ||
|
|
||
|
|
@@ -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(); | ||
|
paulk-asert marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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