Skip to content

Fix crash related to tuple rest arity#2928

Merged
ahejlsberg merged 4 commits into
mainfrom
copilot/fix-compilation-crash
May 17, 2026
Merged

Fix crash related to tuple rest arity#2928
ahejlsberg merged 4 commits into
mainfrom
copilot/fix-compilation-crash

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

  • Investigate crash with infer + rest/variadic tuple elements
  • Identify root cause: nil pointer dereference in inferFromObjectTypes when getElementTypeOfSliceOfTupleType returns nil
  • Add nil guards at both affected locations in internal/checker/inference.go
  • Add compiler test case inferFromTupleRestAndVariadic.ts with baselines
  • Verify the fix resolves the crash
  • Run related inference+tuple tests to confirm no regressions
  • Code review and security scan — no issues found

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits February 27, 2026 17:53
When inferring from tuple types with a mix of rest and variadic elements
(e.g. [...rest, ...T] or [...T, ...rest]), getElementTypeOfSliceOfTupleType
can return nil when the variadic element's implied arity consumes all
source tuple elements. The nil return value was passed directly to
inferFromTypes, causing a nil pointer dereference at inference.go:79.

Added nil guards consistent with the existing pattern used elsewhere in
the same function (line ~736). When getElementTypeOfSliceOfTupleType
returns nil, we skip the inference call since there are no remaining
source elements to infer from.

This bug also exists in the TypeScript compiler (tsc crashes with
TypeError on the same input), so this fix diverges from the TS source
to prevent the crash in Go.
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
@RyanCavanaugh RyanCavanaugh marked this pull request as ready for review February 27, 2026 18:05
Copilot AI review requested due to automatic review settings February 27, 2026 18:05
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

This PR fixes a crash during compilation when inferring from tuple types with rest and variadic elements. The crash occurred when a variadic element's constraint consumed all source tuple elements, causing getElementTypeOfSliceOfTupleType to return nil, leading to a nil pointer dereference.

Changes:

  • Added nil guards at two locations in inferFromObjectTypes where getElementTypeOfSliceOfTupleType could return nil
  • Added comprehensive compiler test case inferFromTupleRestAndVariadic.ts covering both [...rest, ...T] and [...T, ...rest] patterns
  • Accepted baselines showing correct type inference behavior

Reviewed changes

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

Show a summary per file
File Description
testdata/tests/cases/compiler/inferFromTupleRestAndVariadic.ts New compiler test with edge cases for tuple inference with rest and variadic elements
testdata/baselines/reference/compiler/inferFromTupleRestAndVariadic.types Type inference baseline showing correct results for all test cases
testdata/baselines/reference/compiler/inferFromTupleRestAndVariadic.symbols Symbol resolution baseline for the test cases
testdata/baselines/reference/compiler/inferFromTupleRestAndVariadic.js JavaScript emit baseline for the test cases
internal/checker/inference.go Added nil guards before calling inferFromTypes with result from getElementTypeOfSliceOfTupleType

Comment on lines +710 to +712
if restType := c.getElementTypeOfSliceOfTupleType(source, startLength+impliedArity, endLength, false, false); restType != nil {
c.inferFromTypes(n, restType, elementTypes[startLength+1])
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Strada had:

// Middle of target is [...T, ...rest] and source is tuple type
// if T is constrained by a fixed-size tuple we might be able to use its arity to infer T
const param = getInferenceInfoForType(elementTypes[startLength])?.typeParameter;
const constraint = param && getBaseConstraintOfType(param);
if (constraint && isTupleType(constraint) && !(constraint.target.combinedFlags & ElementFlags.Variable)) {
    const impliedArity = constraint.target.fixedLength;
    inferFromTypes(sliceTupleType(source, startLength, sourceArity - (startLength + impliedArity)), elementTypes[startLength]);
    inferFromTypes(getElementTypeOfSliceOfTupleType(source, startLength + impliedArity, endLength)!, elementTypes[startLength + 1]);
}

Why is this change correct? The old code didn't nil check. Is this also a crash in Strada?

(Not sure in what context this PR was created.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a Strada bug too microsoft/TypeScript#63199

@RyanCavanaugh RyanCavanaugh changed the title [WIP] Fix crash during compilation of SubTup type Fix crash related to tuple rest arity Apr 17, 2026
@RyanCavanaugh
Copy link
Copy Markdown
Member

Hoisting comment: This is a fix for microsoft/TypeScript#63199

@RyanCavanaugh RyanCavanaugh modified the milestone: TypeScript 7.0 RC Apr 17, 2026
@jakebailey
Copy link
Copy Markdown
Member

Also #3917 and #3883, might need to consolidate tests and/or fixes

@ahejlsberg ahejlsberg added this pull request to the merge queue May 17, 2026
Merged via the queue into main with commit d846405 May 17, 2026
21 checks passed
@ahejlsberg ahejlsberg deleted the copilot/fix-compilation-crash branch May 17, 2026 12:09
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.

5 participants