Skip to content

Commit

Permalink
Merge pull request #41017 from weswigham/fix-unchecked-cast-crash
Browse files Browse the repository at this point in the history
Fix crash due to unchecked cast in addImplementationSuccessElaboration
  • Loading branch information
weswigham committed Oct 9, 2020
2 parents e6d525c + 39c2a09 commit aee18e0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -27335,7 +27335,7 @@ namespace ts {

const declCount = length(failed.declaration?.symbol.declarations);
const isOverload = declCount > 1;
const implDecl = isOverload ? find(failed.declaration?.symbol.declarations || emptyArray, d => nodeIsPresent((d as FunctionLikeDeclaration).body)) : undefined;
const implDecl = isOverload ? find(failed.declaration?.symbol.declarations || emptyArray, d => isFunctionLikeDeclaration(d) && nodeIsPresent(d.body)) : undefined;
if (implDecl) {
const candidate = getSignatureFromDeclaration(implDecl as FunctionLikeDeclaration);
const isSingleNonGenericCandidate = !candidate.typeParameters;
Expand Down
@@ -0,0 +1,28 @@
tests/cases/compiler/index.ts(3,3): error TS2769: No overload matches this call.
Overload 1 of 2, '(opts?: Whatever): void', gave the following error.
Argument of type 'number' is not assignable to parameter of type 'Whatever'.
Overload 2 of 2, '(cb: Function, opts?: Whatever): void', gave the following error.
Argument of type 'number' is not assignable to parameter of type 'Function'.


==== tests/cases/compiler/index.ts (1 errors) ====
import X = require("./file");

X(0); // shouldn't cause a crash
~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(opts?: Whatever): void', gave the following error.
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'Whatever'.
!!! error TS2769: Overload 2 of 2, '(cb: Function, opts?: Whatever): void', gave the following error.
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'Function'.
==== tests/cases/compiler/file.d.ts (0 errors) ====
declare namespace Foo {
interface Whatever {
prop: any;
}
}

declare function Foo(opts?: Foo.Whatever): void;
declare function Foo(cb: Function, opts?: Foo.Whatever): void;

export = Foo;
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/namespaceMergedWithFunctionWithOverloadsUsage.ts] ////

//// [file.d.ts]
declare namespace Foo {
interface Whatever {
prop: any;
}
}

declare function Foo(opts?: Foo.Whatever): void;
declare function Foo(cb: Function, opts?: Foo.Whatever): void;

export = Foo;
//// [index.ts]
import X = require("./file");

X(0); // shouldn't cause a crash

//// [index.js]
"use strict";
exports.__esModule = true;
var X = require("./file");
X(0); // shouldn't cause a crash
@@ -0,0 +1,36 @@
=== tests/cases/compiler/index.ts ===
import X = require("./file");
>X : Symbol(X, Decl(index.ts, 0, 0))

X(0); // shouldn't cause a crash
>X : Symbol(X, Decl(index.ts, 0, 0))

=== tests/cases/compiler/file.d.ts ===
declare namespace Foo {
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))

interface Whatever {
>Whatever : Symbol(Whatever, Decl(file.d.ts, 0, 23))

prop: any;
>prop : Symbol(Whatever.prop, Decl(file.d.ts, 1, 24))
}
}

declare function Foo(opts?: Foo.Whatever): void;
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
>opts : Symbol(opts, Decl(file.d.ts, 6, 21))
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
>Whatever : Symbol(Foo.Whatever, Decl(file.d.ts, 0, 23))

declare function Foo(cb: Function, opts?: Foo.Whatever): void;
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
>cb : Symbol(cb, Decl(file.d.ts, 7, 21))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>opts : Symbol(opts, Decl(file.d.ts, 7, 34))
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))
>Whatever : Symbol(Foo.Whatever, Decl(file.d.ts, 0, 23))

export = Foo;
>Foo : Symbol(Foo, Decl(file.d.ts, 4, 1), Decl(file.d.ts, 6, 48), Decl(file.d.ts, 0, 0))

@@ -0,0 +1,31 @@
=== tests/cases/compiler/index.ts ===
import X = require("./file");
>X : { (opts?: X.Whatever): void; (cb: Function, opts?: X.Whatever): void; }

X(0); // shouldn't cause a crash
>X(0) : void
>X : { (opts?: X.Whatever): void; (cb: Function, opts?: X.Whatever): void; }
>0 : 0

=== tests/cases/compiler/file.d.ts ===
declare namespace Foo {
interface Whatever {
prop: any;
>prop : any
}
}

declare function Foo(opts?: Foo.Whatever): void;
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }
>opts : Foo.Whatever
>Foo : any

declare function Foo(cb: Function, opts?: Foo.Whatever): void;
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }
>cb : Function
>opts : Foo.Whatever
>Foo : any

export = Foo;
>Foo : { (opts?: Foo.Whatever): void; (cb: Function, opts?: Foo.Whatever): void; }

@@ -0,0 +1,15 @@
// @filename: file.d.ts
declare namespace Foo {
interface Whatever {
prop: any;
}
}

declare function Foo(opts?: Foo.Whatever): void;
declare function Foo(cb: Function, opts?: Foo.Whatever): void;

export = Foo;
// @filename: index.ts
import X = require("./file");

X(0); // shouldn't cause a crash

0 comments on commit aee18e0

Please sign in to comment.