Fix panic when serializing empty tuple array literal types - #4803
Open
artem1458 wants to merge 1 commit into
Open
Fix panic when serializing empty tuple array literal types#4803artem1458 wants to merge 1 commit into
artem1458 wants to merge 1 commit into
Conversation
`newTypeResponse` cast a type with `ObjectFlagsTuple` set straight to
`*checker.TupleType`. That holds for a synthesized tuple target, but not for
a clone of one: `createArrayLiteralType` clones the type through
`cloneTypeReference`, which allocates `*checker.TypeReference` data and then
copies the source's object flags over it, `ObjectFlagsTuple` included.
Only empty tuples reach that path, because `createTupleTypeEx` returns the
tuple target itself at arity zero while every other arity returns a plain
type reference without `ObjectFlagsTuple`. So asking the API for the type of
an array literal contextually typed by an empty tuple killed the server:
declare function f(t: readonly []): void;
const g = () => f([]); // getTypeAtLocation on `[]`
panic: interface conversion: checker.TypeData is *checker.TypeReference,
not *checker.TupleType
Read the tuple shape off the target instead, which works for both shapes
since a tuple target's target is itself.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes type serialization crashes for empty tuple array literals by reading tuple metadata from the tuple target.
Changes:
- Serialize cloned tuple references without unsafe tuple casts.
- Add coverage for empty tuples, non-empty tuples, and arrays.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
internal/api/proto.go |
Reads tuple shape from the reference target. |
internal/api/session_typeatlocation_test.go |
Tests tuple type serialization and regression scenarios. |
Author
|
@microsoft-github-policy-service agree |
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.
Fixes #4804
Asking the API for the type of an array literal contextually typed by an empty tuple kills the tsgo server:
tsc --noEmiton the same file is clean, and the TypeScript 6 API returns[]for it, so this is specific to how the type is serialized.Cause
newTypeResponsetreatsObjectFlagsTupleas proof that the type holds*checker.TupleTypedata:That holds for a synthesized tuple target, but not for a clone of one.
createArrayLiteralTypeclones the array literal's type throughcloneTypeReference, which allocates*checker.TypeReferencedata and then copies the source's object flags over it,ObjectFlagsTupleincluded — so the flags claim a tuple while the data is a type reference, contradicting the invariant documented aboveObjectTypeininternal/checker/types.go.Only empty tuples get there:
createTupleTypeExreturns the tuple target itself at arity zero, while every other arity returns a plain type reference that never hadObjectFlagsTupleset. That is why[1]againstreadonly [number]is fine and[]againstreadonly []is not, and why the same empty tuple type reached through a type node or an annotated identifier serializes fine — those are the target, not a clone.The checker itself is unaffected, since
isTupleTypeand friends testTarget().objectFlagsrather than the type's own flags.Fix
Read the tuple shape off the target rather than casting the type itself. A tuple target's target is itself, so this works for both the target and a clone of it.
proto.go:760(t.AsInterfaceType(), guarded byObjectFlagsClassOrInterface) makes the same assumption and would break the same way if a class or interface target were ever cloned; both currentcloneTypeReferencecall sites happen to be tuple paths, so I left it alone.Tests
TestGetTypeAtLocationTupleTypesininternal/api/session_typeatlocation_test.gocovers thirteen cases — the six array literal shapes that panicked, empty tuple targets reached through a type node and through an identifier, single element tuples, and arrays — asserting the printed type and the tuple metadata on the response. It panics without this change.go test ./...is green, including an uncachedgo test -count=1 ./internal/testrunner/...(117111 subtests).