[WIP] Fix crash during compilation of SubTup type#2928
Open
Conversation
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>
Contributor
There was a problem hiding this comment.
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
inferFromObjectTypeswheregetElementTypeOfSliceOfTupleTypecould return nil - Added comprehensive compiler test case
inferFromTupleRestAndVariadic.tscovering 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 |
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.
infer+ rest/variadic tuple elementsinferFromObjectTypeswhengetElementTypeOfSliceOfTupleTypereturns nilinternal/checker/inference.goinferFromTupleRestAndVariadic.tswith baselines🔒 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.