Fix panic in checker.GetTypeArguments for non-reference types (#4338)#4416
Open
UditDewan wants to merge 2 commits into
Open
Fix panic in checker.GetTypeArguments for non-reference types (#4338)#4416UditDewan wants to merge 2 commits into
UditDewan wants to merge 2 commits into
Conversation
…oft#4338) The public API `Checker.GetTypeArguments` forwarded straight to the internal `getTypeArguments`, which assumes a type reference and immediately dereferences `t.AsTypeReference()`. Passing a non-reference type (e.g. the intrinsic `string`) yielded a nil `*TypeReference` and crashed with a nil pointer dereference. Guard the exported method so non-reference types return no type arguments, matching tsc and the native-preview `getTypeArguments(type): readonly Type[]` contract. Internal callers already gate on `ObjectFlagsReference` before calling `getTypeArguments`, so the hot path is unaffected. Adds sync and async API regression tests that reproduce the original panic. Claude (Opus 4.8) helped debug this issue.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a server panic in the exported checker API by making Checker.GetTypeArguments safely handle non-reference types (e.g. intrinsic string) and return “no type arguments” instead of dereferencing a nil TypeReference. This aligns the native-preview API’s getTypeArguments(type: Type): readonly Type[] contract with tsc behavior.
Changes:
- Add an
ObjectFlagsReferenceguard inChecker.GetTypeArgumentsto avoid nil-deref for non-reference types. - Add sync + async native-preview regression tests asserting
getTypeArguments(stringType)returns[](issue #4338).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/checker/exports.go | Adds an exported-API guard so non-reference Types return nil/empty instead of panicking. |
| _packages/native-preview/test/sync/api.test.ts | Adds sync regression test for getTypeArguments on string. |
| _packages/native-preview/test/async/api.test.ts | Adds async regression test for getTypeArguments on string. |
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 #4338
Problem
The public API method
Checker.GetTypeArgumentsforwarded straight to the internalgetTypeArguments, which assumes its argument is a type reference and immediately doest.AsTypeReference(). For a non-reference type (e.g. the intrinsicstring, produced bytype Alias = string) that cast returnsnil, so the next field access (d.resolvedTypeArguments) is a nil pointer dereference and the server panics:The native-preview API types this method as
getTypeArguments(type: Type): readonly Type[], i.e. it accepts anyType, so callers can legitimately reach this path with a non-reference type.Fix
Guard the exported
GetTypeArgumentsso non-reference types return no type arguments instead of panicking. This matchestsc's behavior (non-references have no type arguments) and thereadonly Type[]contract. The check usesObjectFlagsReference— the same gate internal callers already apply before callinggetTypeArguments— so the internal hot path is untouched.This mirrors the approach taken for the sibling crash in #4335 / PR #4345 (
getBaseTypeson non-matching types returnsnil).Tests
Added regression tests to both the sync and async API suites (
Checker - getTypeArguments) that callgetTypeArgumentson the intrinsicstringtype and assert it returns[]. Verified that:getTypeArgumentstest pass.gofmtanddprint checkare clean.Claude (Opus 4.8) helped debug this issue.