Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types#3929
Draft
Copilot wants to merge 2 commits into
Draft
Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types#3929Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
…e template literal types Add overflow protection to prevent int64 wraparound when computing cross product union sizes. When the size would exceed 100,000, return 100,000 immediately instead of continuing to multiply, which could overflow and produce a value that bypasses the limit check. Add test case for template literal type with combinatorial explosion. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/5fcc3e3e-691e-43a4-b71c-72ae84389f8b Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix tsgo hang on large template literal types
Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types
May 16, 2026
jakebailey
reviewed
May 16, 2026
| case t.flags&TypeFlagsUnion != 0: | ||
| size *= len(t.Types()) | ||
| n := len(t.Types()) | ||
| if n > 0 && size > 100_000/n { |
Member
There was a problem hiding this comment.
@copilot 100_000 seems like a magic number, and this definitely requires a comment. Probably also best to return MaxInt?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getCrossProductUnionSizeoverflows Go's signed 64-bitintwhen computing large cross products (e.g., 4^49 for 49 unions of 4 members). At 4^32 = 2^64, the product wraps to 0, bypassing the 100,000 limit check entirely. In TypeScript's reference implementation, JS floats produceInfinitywhich correctly triggers the error.getCrossProductUnionSizewhen the next multiplication would exceed the 100k threshold, preventing overflow and correctly returning the cap valuetemplateLiteralTypeTooComplex.tscovering the exact repro from the issue — confirms TS2590 is emitted instead of hanging