Skip to content

Make overload resolution more elaborate #951

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

Merged
merged 14 commits into from
Oct 27, 2014
Merged
Show file tree
Hide file tree
Changes from 13 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
305 changes: 239 additions & 66 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ module ts {
};
}

export function concatenateDiagnosticMessageChains(headChain: DiagnosticMessageChain, tailChain: DiagnosticMessageChain): DiagnosticMessageChain {
Debug.assert(!headChain.next);
headChain.next = tailChain;
return headChain;
}

export function flattenDiagnosticChain(file: SourceFile, start: number, length: number, diagnosticChain: DiagnosticMessageChain, newLine: string): Diagnostic {
Debug.assert(start >= 0, "start must be non-negative, is " + start);
Debug.assert(length >= 0, "length must be non-negative, is " + length);
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ module ts {
Left_hand_side_of_assignment_expression_cannot_be_a_constant: { code: 2450, category: DiagnosticCategory.Error, key: "Left-hand side of assignment expression cannot be a constant.", isEarly: true },
Cannot_redeclare_block_scoped_variable_0: { code: 2451, category: DiagnosticCategory.Error, key: "Cannot redeclare block-scoped variable '{0}'.", isEarly: true },
An_enum_member_cannot_have_a_numeric_name: { code: 2452, category: DiagnosticCategory.Error, key: "An enum member cannot have a numeric name." },
The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly_Colon: { code: 2453, category: DiagnosticCategory.Error, key: "The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly:" },
Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0_Colon: { code: 2454, category: DiagnosticCategory.Error, key: "Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}':" },
Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0: { code: 2455, category: DiagnosticCategory.Error, key: "Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}'." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
Type_parameter_0_of_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 4001, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using name '{1}' from private module '{2}'." },
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,18 @@
"category": "Error",
"code": 2452
},
"The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly:": {
"category": "Error",
"code": 2453
},
"Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}':": {
"category": "Error",
"code": 2454
},
"Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}'.": {
"category": "Error",
"code": 2455
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ module ts {
inferenceCount: number; // Incremented for every inference made (whether new or not)
inferences: Type[][]; // Inferences made for each type parameter
inferredTypes: Type[]; // Inferred type for each type parameter
failureIndex?: number; // Index of type parameter for which inference failed
// It is optional because in contextual signature instantiation, nothing fails
}

export interface DiagnosticMessage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(2,22): error TS2339: Property 'foo' does not exist on type 'string'.
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,10): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.


==== tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts (2 errors) ====
Expand All @@ -8,5 +9,6 @@ tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,10): error TS
~~~
!!! error TS2339: Property 'foo' does not exist on type 'string'.
var r9 = f10('', () => (a => a.foo), 1); // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(2,6): error TS2339: Property 'length' does not exist on type '{}'.
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(5,6): error TS2339: Property 'length' does not exist on type 'Object'.
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,14): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,14): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.


==== tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts (3 errors) ====
Expand All @@ -16,8 +17,9 @@ tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,14): error TS2346: Sup

function concat<T>(x: T, y: T): T { return null; }
var result = concat(1, ""); // error
~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
var elementCount = result.length;

function concat2<T, U>(x: T, y: U) { return null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts(2,1): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts(2,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.


==== tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts (1 errors) ====
function bar<T>(item1: T, item2: T) { }
bar(1, ""); // Should be ok
~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(26,18): error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(30,15): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(33,15): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(34,16): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(35,23): error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(26,10): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(30,15): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(33,15): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(34,16): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(35,15): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.


==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts (5 errors) ====
Expand Down Expand Up @@ -34,25 +37,28 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
var r7 = foo3(1, <Z>(a: Z) => '', ''); // string

var r8 = foo3(1, function (a) { return '' }, 1); // error
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
var r9 = foo3<number, string>(1, (a) => '', ''); // string

function other<T, U>(t: T, u: U) {
var r10 = foo2(1, (x: T) => ''); // error
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
var r10 = foo2(1, (x) => ''); // string

var r11 = foo3(1, (x: T) => '', ''); // error
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
var r11b = foo3(1, (x: T) => '', 1); // error
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'T'.
var r12 = foo3(1, function (a) { return '' }, 1); // error
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => number'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments2.ts(29,10): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments2.ts(40,10): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments2.ts(29,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments2.ts(40,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.


==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments2.ts (2 errors) ====
Expand Down Expand Up @@ -32,8 +34,9 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
}

var r4 = foo2(1, i2); // error
~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
var r4b = foo2(1, a); // any
var r5 = foo2(1, i); // any
var r6 = foo2<string, string>('', i2); // string
Expand All @@ -45,6 +48,7 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
var r7 = foo3(null, i, ''); // any
var r7b = foo3(null, a, ''); // any
var r8 = foo3(1, i2, 1); // error
~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
var r9 = foo3<string, string>('', i2, ''); // string
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGenericSignatureArguments.ts(18,10): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGenericSignatureArguments.ts(19,10): error TS2346: Supplied parameters do not match any signature of call target.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGenericSignatureArguments.ts(18,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate '{ x: number; y?: number; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; z?: number; }'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGenericSignatureArguments.ts(19,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
Type argument candidate '{ x: number; z?: number; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y?: number; }'.


==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGenericSignatureArguments.ts (2 errors) ====
Expand All @@ -21,11 +23,13 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithGen
var b: { x: number; z?: number; };

var r4 = foo((x: typeof a) => a, (x: typeof b) => b); // typeof a => typeof a
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate '{ x: number; y?: number; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; z?: number; }'.
var r5 = foo((x: typeof b) => b, (x: typeof a) => a); // typeof b => typeof b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly:
!!! error TS2453: Type argument candidate '{ x: number; z?: number; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y?: number; }'.

function other<T>(x: T) {
var r6 = foo((a: T) => a, (b: T) => b); // T => T
Expand Down
Loading