Skip to content

Commit aee18e0

Browse files
authored
Merge pull request microsoft#41017 from weswigham/fix-unchecked-cast-crash
Fix crash due to unchecked cast in addImplementationSuccessElaboration
2 parents e6d525c + 39c2a09 commit aee18e0

6 files changed

+134
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27335,7 +27335,7 @@ namespace ts {
2733527335

2733627336
const declCount = length(failed.declaration?.symbol.declarations);
2733727337
const isOverload = declCount > 1;
27338-
const implDecl = isOverload ? find(failed.declaration?.symbol.declarations || emptyArray, d => nodeIsPresent((d as FunctionLikeDeclaration).body)) : undefined;
27338+
const implDecl = isOverload ? find(failed.declaration?.symbol.declarations || emptyArray, d => isFunctionLikeDeclaration(d) && nodeIsPresent(d.body)) : undefined;
2733927339
if (implDecl) {
2734027340
const candidate = getSignatureFromDeclaration(implDecl as FunctionLikeDeclaration);
2734127341
const isSingleNonGenericCandidate = !candidate.typeParameters;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
tests/cases/compiler/index.ts(3,3): error TS2769: No overload matches this call.
2+
Overload 1 of 2, '(opts?: Whatever): void', gave the following error.
3+
Argument of type 'number' is not assignable to parameter of type 'Whatever'.
4+
Overload 2 of 2, '(cb: Function, opts?: Whatever): void', gave the following error.
5+
Argument of type 'number' is not assignable to parameter of type 'Function'.
6+
7+
8+
==== tests/cases/compiler/index.ts (1 errors) ====
9+
import X = require("./file");
10+
11+
X(0); // shouldn't cause a crash
12+
~
13+
!!! error TS2769: No overload matches this call.
14+
!!! error TS2769: Overload 1 of 2, '(opts?: Whatever): void', gave the following error.
15+
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'Whatever'.
16+
!!! error TS2769: Overload 2 of 2, '(cb: Function, opts?: Whatever): void', gave the following error.
17+
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'Function'.
18+
==== tests/cases/compiler/file.d.ts (0 errors) ====
19+
declare namespace Foo {
20+
interface Whatever {
21+
prop: any;
22+
}
23+
}
24+
25+
declare function Foo(opts?: Foo.Whatever): void;
26+
declare function Foo(cb: Function, opts?: Foo.Whatever): void;
27+
28+
export = Foo;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/namespaceMergedWithFunctionWithOverloadsUsage.ts] ////
2+
3+
//// [file.d.ts]
4+
declare namespace Foo {
5+
interface Whatever {
6+
prop: any;
7+
}
8+
}
9+
10+
declare function Foo(opts?: Foo.Whatever): void;
11+
declare function Foo(cb: Function, opts?: Foo.Whatever): void;
12+
13+
export = Foo;
14+
//// [index.ts]
15+
import X = require("./file");
16+
17+
X(0); // shouldn't cause a crash
18+
19+
//// [index.js]
20+
"use strict";
21+
exports.__esModule = true;
22+
var X = require("./file");
23+
X(0); // shouldn't cause a crash
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import X = require("./file");
3+
>X : Symbol(X, Decl(index.ts, 0, 0))
4+
5+
X(0); // shouldn't cause a crash
6+
>X : Symbol(X, Decl(index.ts, 0, 0))
7+
8+
=== tests/cases/compiler/file.d.ts ===
9+
declare namespace Foo {
10+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
11+
12+
interface Whatever {
13+
>Whatever : Symbol(Whatever, Decl(file.d.ts, 0, 23))
14+
15+
prop: any;
16+
>prop : Symbol(Whatever.prop, Decl(file.d.ts, 1, 24))
17+
}
18+
}
19+
20+
declare function Foo(opts?: Foo.Whatever): void;
21+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
22+
>opts : Symbol(opts, Decl(file.d.ts, 6, 21))
23+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
24+
>Whatever : Symbol(Foo.Whatever, Decl(file.d.ts, 0, 23))
25+
26+
declare function Foo(cb: Function, opts?: Foo.Whatever): void;
27+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
28+
>cb : Symbol(cb, Decl(file.d.ts, 7, 21))
29+
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
30+
>opts : Symbol(opts, Decl(file.d.ts, 7, 34))
31+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
32+
>Whatever : Symbol(Foo.Whatever, Decl(file.d.ts, 0, 23))
33+
34+
export = Foo;
35+
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
36+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/index.ts ===
2+
import X = require("./file");
3+
>X : { (opts?: X.Whatever): void; (cb: Function, opts?: X.Whatever): void; }
4+
5+
X(0); // shouldn't cause a crash
6+
>X(0) : void
7+
>X : { (opts?: X.Whatever): void; (cb: Function, opts?: X.Whatever): void; }
8+
>0 : 0
9+
10+
=== tests/cases/compiler/file.d.ts ===
11+
declare namespace Foo {
12+
interface Whatever {
13+
prop: any;
14+
>prop : any
15+
}
16+
}
17+
18+
declare function Foo(opts?: Foo.Whatever): void;
19+
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }
20+
>opts : Foo.Whatever
21+
>Foo : any
22+
23+
declare function Foo(cb: Function, opts?: Foo.Whatever): void;
24+
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }
25+
>cb : Function
26+
>opts : Foo.Whatever
27+
>Foo : any
28+
29+
export = Foo;
30+
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }
31+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @filename: file.d.ts
2+
declare namespace Foo {
3+
interface Whatever {
4+
prop: any;
5+
}
6+
}
7+
8+
declare function Foo(opts?: Foo.Whatever): void;
9+
declare function Foo(cb: Function, opts?: Foo.Whatever): void;
10+
11+
export = Foo;
12+
// @filename: index.ts
13+
import X = require("./file");
14+
15+
X(0); // shouldn't cause a crash

0 commit comments

Comments
 (0)