RFR: 8337142: StackOverflowError in Types.containsTypeRecursive with deeply nested type hierarchy [v2]
Maurizio Cimadamore
mcimadamore at openjdk.org
Mon Nov 3 16:01:45 UTC 2025
On Mon, 3 Nov 2025 14:56:30 GMT, Vicente Romero <vromero at openjdk.org> wrote:
>> javac can crash with SOE for some deeply nested type hierarchies. See the related JBS issue for the code example. Here we have another example of infinite recursion in the compiler. This is what is happening:
>>
>> - method Types."isSubtype"::containsTypeRecursive uses a cache, which is a Set, the elements in that cache are of type Types.TypePair. Method TypePair::equals invoke Types::isSameType
>> - Types::isSameType for this test case ends up invoking Types."isSubtype"::containsTypeRecursive, closing the infinite loop
>>
>> The proposed solution is to define current anonymous class isSameTypeVisitor as a named class and derive from it a visitor that will be the one used by Types.TypePair and thus invoked by TypePair::equals every time instances of TypePair are added or removed from a Map, Set, etc.
>>
>> This new visitor will compare types looking for exactness which is something desirable in a map key.
>>
>> TIA
>
> Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:
>
> addressing review comments
src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java line 3901:
> 3899: boolean sameTypeArguments(List<Type> ts, List<Type> ss) {
> 3900: while (ts.nonEmpty() && ss.nonEmpty()
> 3901: && exactTypeVisitor.visit(ts.head, ss.head)) {
You could delegate to `sameTypeComparator` here?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/28101#discussion_r2487001792
More information about the compiler-dev
mailing list