Skip to content

Reuse single errored signature candidate as resolved signature #61787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36385,7 +36385,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (result) {
return result;
}
result = getCandidateForOverloadFailure(node, candidates, args, !!candidatesOutArray, checkMode);
result = getCandidateForOverloadFailure(node, candidates, candidatesForArgumentError, args, !!candidatesOutArray, checkMode);
// Preemptively cache the result; getResolvedSignature will do this after we return, but
// we need to ensure that the result is present for the error checks below so that if
// this signature is encountered again, we handle the circularity (rather than producing a
Expand Down Expand Up @@ -36616,6 +36616,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function getCandidateForOverloadFailure(
node: CallLikeExpression,
candidates: Signature[],
candidatesForArgumentError: Signature[] | undefined,
args: readonly Expression[],
hasCandidatesOutArray: boolean,
checkMode: CheckMode,
Expand All @@ -36626,7 +36627,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Don't do this if there is a `candidatesOutArray`,
// because then we want the chosen best candidate to be one of the overloads, not a combination.
return hasCandidatesOutArray || candidates.length === 1 || candidates.some(c => !!c.typeParameters)
? pickLongestCandidateSignature(node, candidates, args, checkMode)
? pickLongestCandidateSignature(node, candidates, candidatesForArgumentError, args, checkMode)
: createUnionOfSignaturesForOverloadFailure(candidates);
}

Expand Down Expand Up @@ -36682,7 +36683,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return createSymbolWithType(first(sources), type);
}

function pickLongestCandidateSignature(node: CallLikeExpression, candidates: Signature[], args: readonly Expression[], checkMode: CheckMode): Signature {
function pickLongestCandidateSignature(node: CallLikeExpression, candidates: Signature[], candidatesForArgumentError: Signature[] | undefined, args: readonly Expression[], checkMode: CheckMode): Signature {
// Pick the longest signature. This way we can get a contextual type for cases like:
// declare function f(a: { xa: number; xb: number; }, b: number);
// f({ |
Expand All @@ -36699,9 +36700,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const typeArgumentNodes: readonly TypeNode[] | undefined = callLikeExpressionMayHaveTypeArguments(node) ? node.typeArguments : undefined;
const instantiated = typeArgumentNodes
? createSignatureInstantiation(candidate, getTypeArgumentsFromNodes(typeArgumentNodes, typeParameters, isInJSFile(node)))
// when there is only one candidate reuse existing *inferred* candidate for argument error (if available)
// this saves the compiler some extra work but more importantly it includes inferences made from context-sensitive arguments and generic functions
// which avoids confusing mismatches between inferred type arguments and reported argument error
//
// for the time being only do this when there is a single candidate as the origin index of the `candidatesForArgumentError` is not known
// and it could be different from the selected `bestIndex` here
Comment on lines +36707 to +36708
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider revisiting this in Corsa where CallState is already inroduced and improving upon this would likely be slightly easier thanks to that

: candidates.length === 1 && candidatesForArgumentError
? candidatesForArgumentError[0]
: inferSignatureInstantiationForOverloadFailure(node, typeParameters, candidate, args, checkMode);
candidates[bestIndex] = instantiated;
return instantiated;
// for similar reasons as above, we reuse the already instantiated *single* candidatesForArgumentError here (even when there are more input candidates)
// this is the preferred candidate for which the signature errors are reported
return candidatesForArgumentError?.length === 1 ? candidatesForArgumentError[0] : instantiated;
}

function getTypeArgumentsFromNodes(typeArgumentNodes: readonly TypeNode[], typeParameters: readonly TypeParameter[], isJs: boolean): readonly Type[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
contextualSignatureInstantiation.ts(19,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
contextualSignatureInstantiation.ts(19,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
contextualSignatureInstantiation.ts(19,13): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
Types of parameters 'y' and 'y' are incompatible.
Type 'string' is not assignable to type 'number'.
contextualSignatureInstantiation.ts(20,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
contextualSignatureInstantiation.ts(20,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
contextualSignatureInstantiation.ts(20,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
Types of parameters 'y' and 'y' are incompatible.
Type 'string' is not assignable to type 'number'.
contextualSignatureInstantiation.ts(21,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
contextualSignatureInstantiation.ts(21,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'string'.
contextualSignatureInstantiation.ts(21,23): error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: number) => string'.
Types of parameters 'y' and 'y' are incompatible.
Type 'number' is not assignable to type 'string'.
Expand All @@ -33,29 +33,31 @@ contextualSignatureInstantiation.ts(21,23): error TS2345: Argument of type '<T>(
var b: number | string;
var b = foo(g); // Error, number and string are disjoint types
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
~
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
!!! error TS2345: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
var b = bar(1, "one", g); // Error, number and string are disjoint types
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'number'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
~
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: number, y: string) => number'.
!!! error TS2345: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
var b = bar("one", 1, g); // Error, number and string are disjoint types
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'unknown'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'b' must be of type 'string | number', but here has type 'string'.
!!! related TS6203 contextualSignatureInstantiation.ts:18:5: 'b' was also declared here.
~
!!! error TS2345: Argument of type '<T>(x: T, y: T) => T' is not assignable to parameter of type '(x: string, y: number) => string'.
!!! error TS2345: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type 'string'.
var b = baz(b, b, g); // Should be number | string

var b2: number | string;
var b2 = baz(b2, b2, g); // Should be number | string

var d: number[] | string[];
var d = foo(h); // Should be number[] | string[]
Expand Down
7 changes: 5 additions & 2 deletions tests/baselines/reference/contextualSignatureInstantiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var b: number | string;
var b = foo(g); // Error, number and string are disjoint types
var b = bar(1, "one", g); // Error, number and string are disjoint types
var b = bar("one", 1, g); // Error, number and string are disjoint types
var b = baz(b, b, g); // Should be number | string

var b2: number | string;
var b2 = baz(b2, b2, g); // Should be number | string

var d: number[] | string[];
var d = foo(h); // Should be number[] | string[]
Expand All @@ -44,7 +46,8 @@ var b;
var b = foo(g); // Error, number and string are disjoint types
var b = bar(1, "one", g); // Error, number and string are disjoint types
var b = bar("one", 1, g); // Error, number and string are disjoint types
var b = baz(b, b, g); // Should be number | string
var b2;
var b2 = baz(b2, b2, g); // Should be number | string
var d;
var d = foo(h); // Should be number[] | string[]
var d = bar(1, "one", h); // Should be number[] | string[]
Expand Down
33 changes: 18 additions & 15 deletions tests/baselines/reference/contextualSignatureInstantiation.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -83,52 +83,55 @@ var a = baz(1, 1, g); // Should be number
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b: number | string;
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3))

var b = foo(g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3))
>foo : Symbol(foo, Decl(contextualSignatureInstantiation.ts, 0, 0))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = bar(1, "one", g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = bar("one", 1, g); // Error, number and string are disjoint types
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var b = baz(b, b, g); // Should be number | string
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
var b2: number | string;
>b2 : Symbol(b2, Decl(contextualSignatureInstantiation.ts, 22, 3), Decl(contextualSignatureInstantiation.ts, 23, 3))

var b2 = baz(b2, b2, g); // Should be number | string
>b2 : Symbol(b2, Decl(contextualSignatureInstantiation.ts, 22, 3), Decl(contextualSignatureInstantiation.ts, 23, 3))
>baz : Symbol(baz, Decl(contextualSignatureInstantiation.ts, 7, 68))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b : Symbol(b, Decl(contextualSignatureInstantiation.ts, 17, 3), Decl(contextualSignatureInstantiation.ts, 18, 3), Decl(contextualSignatureInstantiation.ts, 19, 3), Decl(contextualSignatureInstantiation.ts, 20, 3), Decl(contextualSignatureInstantiation.ts, 21, 3))
>b2 : Symbol(b2, Decl(contextualSignatureInstantiation.ts, 22, 3), Decl(contextualSignatureInstantiation.ts, 23, 3))
>b2 : Symbol(b2, Decl(contextualSignatureInstantiation.ts, 22, 3), Decl(contextualSignatureInstantiation.ts, 23, 3))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

var d: number[] | string[];
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))

var d = foo(h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>foo : Symbol(foo, Decl(contextualSignatureInstantiation.ts, 0, 0))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = bar(1, "one", h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = bar("one", 1, h); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>bar : Symbol(bar, Decl(contextualSignatureInstantiation.ts, 6, 60))
>h : Symbol(h, Decl(contextualSignatureInstantiation.ts, 10, 37))

var d = baz(d, d, g); // Should be number[] | string[]
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>baz : Symbol(baz, Decl(contextualSignatureInstantiation.ts, 7, 68))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 23, 3), Decl(contextualSignatureInstantiation.ts, 24, 3), Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>d : Symbol(d, Decl(contextualSignatureInstantiation.ts, 25, 3), Decl(contextualSignatureInstantiation.ts, 26, 3), Decl(contextualSignatureInstantiation.ts, 27, 3), Decl(contextualSignatureInstantiation.ts, 28, 3), Decl(contextualSignatureInstantiation.ts, 29, 3))
>g : Symbol(g, Decl(contextualSignatureInstantiation.ts, 8, 65))

Loading