Skip to content

Commit 4d31416

Browse files
Copilotjakebailey
andauthored
Fix panic in instantiateAliasOrInterfaceWithDefaults for non-interface types
Add nil check for AsInterfaceType() to prevent SIGSEGV when JSX.LibraryManagedAttributes has an unexpected type like an enum. Add test case to reproduce the issue. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/b1693337-08f1-4c05-b861-3cf0317ca5d2 Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 7f484ba commit 4d31416

6 files changed

Lines changed: 74 additions & 1 deletion

internal/checker/jsx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ func (c *Checker) instantiateAliasOrInterfaceWithDefaults(managedSym *ast.Symbol
10411041
return c.getTypeAliasInstantiation(managedSym, args, nil)
10421042
}
10431043
}
1044-
if len(declaredManagedType.AsInterfaceType().TypeParameters()) >= len(typeArguments) {
1044+
if declaredManagedType.AsInterfaceType() != nil && len(declaredManagedType.AsInterfaceType().TypeParameters()) >= len(typeArguments) {
10451045
args := c.fillMissingTypeArguments(typeArguments, declaredManagedType.AsInterfaceType().TypeParameters(), len(typeArguments), inJavaScript)
10461046
return c.createTypeReference(declaredManagedType, args)
10471047
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
jsxLibraryManagedAttributesUnexpectedType.tsx(7,12): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
2+
3+
4+
==== jsxLibraryManagedAttributesUnexpectedType.tsx (1 errors) ====
5+
declare namespace JSX {
6+
enum LibraryManagedAttributes {}
7+
}
8+
9+
declare const C: () => any;
10+
11+
const x = <C />;
12+
~
13+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/jsxLibraryManagedAttributesUnexpectedType.tsx] ////
2+
3+
//// [jsxLibraryManagedAttributesUnexpectedType.tsx]
4+
declare namespace JSX {
5+
enum LibraryManagedAttributes {}
6+
}
7+
8+
declare const C: () => any;
9+
10+
const x = <C />;
11+
12+
13+
//// [jsxLibraryManagedAttributesUnexpectedType.js]
14+
"use strict";
15+
const x = React.createElement(C, null);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/jsxLibraryManagedAttributesUnexpectedType.tsx] ////
2+
3+
=== jsxLibraryManagedAttributesUnexpectedType.tsx ===
4+
declare namespace JSX {
5+
>JSX : Symbol(JSX, Decl(jsxLibraryManagedAttributesUnexpectedType.tsx, 0, 0))
6+
7+
enum LibraryManagedAttributes {}
8+
>LibraryManagedAttributes : Symbol(LibraryManagedAttributes, Decl(jsxLibraryManagedAttributesUnexpectedType.tsx, 0, 23))
9+
}
10+
11+
declare const C: () => any;
12+
>C : Symbol(C, Decl(jsxLibraryManagedAttributesUnexpectedType.tsx, 4, 13))
13+
14+
const x = <C />;
15+
>x : Symbol(x, Decl(jsxLibraryManagedAttributesUnexpectedType.tsx, 6, 5))
16+
>C : Symbol(C, Decl(jsxLibraryManagedAttributesUnexpectedType.tsx, 4, 13))
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/jsxLibraryManagedAttributesUnexpectedType.tsx] ////
2+
3+
=== jsxLibraryManagedAttributesUnexpectedType.tsx ===
4+
declare namespace JSX {
5+
>JSX : typeof JSX
6+
7+
enum LibraryManagedAttributes {}
8+
>LibraryManagedAttributes : LibraryManagedAttributes
9+
}
10+
11+
declare const C: () => any;
12+
>C : () => any
13+
14+
const x = <C />;
15+
>x : any
16+
><C /> : any
17+
>C : () => any
18+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @jsx: react
2+
3+
declare namespace JSX {
4+
enum LibraryManagedAttributes {}
5+
}
6+
7+
declare const C: () => any;
8+
9+
const x = <C />;

0 commit comments

Comments
 (0)