Skip to content

Fix panic when inferring [...rest, ...T] from tuple shorter than fixed-arity constraint#3917

Merged
ahejlsberg merged 3 commits into
mainfrom
copilot/fix-tsgo-panics-inference
May 18, 2026
Merged

Fix panic when inferring [...rest, ...T] from tuple shorter than fixed-arity constraint#3917
ahejlsberg merged 3 commits into
mainfrom
copilot/fix-tsgo-panics-inference

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

Go slices panic on negative indices, unlike JS Array.slice which clamps to 0. In the [...rest, ...T] inference branch, startIndex = endIndex - impliedArity goes negative when the source tuple is shorter than T's constraint arity.

function f<T extends [string]>(args: [...string[], ...T]) {}
f([]) // panic: slice bounds out of range [-1:]
  • Added bounds check startIndex >= startLength before slicing source tuple type arguments in inferFromObjectTypes
  • Added compiler test case reproducing the crash

…d-arity constraint

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/7618a648-4ae3-4585-8479-774b65e1636d

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tsgo panics when inferring from tuple shorter than fixed-arity Fix panic when inferring [...rest, ...T] from tuple shorter than fixed-arity constraint May 16, 2026
Copilot AI requested a review from jakebailey May 16, 2026 19:56
@ahejlsberg
Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

…cs-inference

# Conflicts:
#	internal/checker/inference.go

Co-authored-by: ahejlsberg <4226954+ahejlsberg@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 17, 2026

@copilot resolve the merge conflicts in this pull request

Resolved the merge conflict in internal/checker/inference.go — combined the bounds check (startIndex >= startLength) with the restType != nil guard from main. All tests, build, lint, and format pass. (ba1dd4d)

Copilot AI requested a review from ahejlsberg May 17, 2026 13:11
@ahejlsberg ahejlsberg marked this pull request as ready for review May 17, 2026 13:16
Copilot AI review requested due to automatic review settings May 17, 2026 13:16
@ahejlsberg ahejlsberg enabled auto-merge May 17, 2026 13:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a Go panic in the [...rest, ...T] tuple inference branch when the source tuple is shorter than the fixed-arity constraint of T. The Go-native slice c.getTypeArguments(source)[startIndex:endIndex] panics on negative startIndex, while JavaScript's Array.slice would clamp; the fix guards the slice with startIndex >= startLength before performing inference, and adds a regression compiler test.

Changes:

  • Add startIndex >= startLength bounds check around the [...rest, ...T] trailing slice/inference block in inferFromObjectTypes.
  • Add inferRestTupleShortSource.ts compiler test plus accepted baselines (.types, .symbols, .js, .errors.txt).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/checker/inference.go Wraps trailing-slice creation and both inferFromTypes calls in a startIndex >= startLength guard to avoid negative slice bounds.
testdata/tests/cases/compiler/inferRestTupleShortSource.ts New regression test reproducing the panic with f<T extends [string]>(args: [...string[], ...T]) called as f([]).
testdata/baselines/reference/compiler/inferRestTupleShortSource.types Accepted types baseline showing f([]) resolves to void with no crash.
testdata/baselines/reference/compiler/inferRestTupleShortSource.symbols Accepted symbols baseline for the new test.
testdata/baselines/reference/compiler/inferRestTupleShortSource.js Accepted JS emit baseline for the new test.
testdata/baselines/reference/compiler/inferRestTupleShortSource.errors.txt Accepted error baseline showing TS2345 with target [...string[], string] (T defaults to its constraint).

@ahejlsberg ahejlsberg added this pull request to the merge queue May 18, 2026
Merged via the queue into main with commit 902a32b May 18, 2026
25 checks passed
@ahejlsberg ahejlsberg deleted the copilot/fix-tsgo-panics-inference branch May 18, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo panics when inferring [...rest, ...T] from a tuple shorter than fixed-arity constraint

5 participants