Skip to content

Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params#3924

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-panics-contextually-typed-function
Open

Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params#3924
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-panics-contextually-typed-function

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

getRestTypeAtPosition panics with makeslice: len out of range when the source signature has fewer parameters than pos, producing a negative slice length.

const f: () => void = (a?, ...b) => {};  // panic

The context signature has 0 parameters, but assignContextualParameterTypes passes pos=1 (the non-rest param count of the function expression). The TS reference uses dynamic arrays with a for (let i = pos; i < parameterCount; i++) loop that naturally handles this—Go's pre-allocated make does not.

  • Added a bounds check in getRestTypeAtPosition: when parameterCount - pos <= 0, return an empty tuple
  • Added a minimal repro test case

When a contextually typed function has more parameters than the context
type (e.g. `const f: () => void = (a?, ...b) => {};`), pos can exceed
parameterCount, making the slice length negative and causing a panic.

Add a bounds check to return an empty tuple when parameterCount - pos <= 0,
matching the TypeScript reference implementation behavior.

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/740d6ea1-5b03-4627-9bc9-a887a0bd62d1

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tsgo panics for contextually typed function with optional and rest params Fix panic in getRestTypeAtPosition for contextually typed function with optional and rest params May 16, 2026
Copilot AI requested a review from jakebailey May 16, 2026 20:12
@jakebailey jakebailey marked this pull request as ready for review May 16, 2026 20:42
Copilot AI review requested due to automatic review settings May 16, 2026 20:42
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 panic in the checker when computing a rest type at a parameter position beyond the contextual signature’s parameter count (e.g., context signature has 0 params but pos is 1), by avoiding negative slice lengths and returning an empty tuple rest type instead.

Changes:

  • Add a guard in Checker.getRestTypeAtPosition to return an empty tuple when parameterCount - pos <= 0.
  • Add a compiler test that reproduces the crash scenario (contextually typed function expression with optional + rest parameters).
  • Add new reference baselines for the test (types/symbols/js/errors).

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/relater.go Prevents make([]T, negative) by handling out-of-range pos with an empty tuple rest type.
testdata/tests/cases/compiler/contextuallyTypedFunctionOptionalAndRest.ts Adds a minimal repro case for the panic scenario.
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.types Captures expected type output for the new test.
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.symbols Captures expected symbol output for the new test.
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.js Captures expected JS emit for the new test.
testdata/baselines/reference/compiler/contextuallyTypedFunctionOptionalAndRest.errors.txt Captures expected diagnostics for the new test.

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 for a contextually typed function with optional and rest params

3 participants