Skip to content

Commit deece85

Browse files
Add test for generic type inference failure
1 parent f4ef31d commit deece85

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

src/resolver.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,9 @@ export class Resolver extends DiagnosticEmitter {
738738
ctxFlow,
739739
reportMode,
740740
);
741-
741+
if (!resolvedTypeArguments) {
742+
return null;
743+
}
742744
return this.resolveFunction(
743745
prototype,
744746
resolvedTypeArguments,
@@ -778,16 +780,24 @@ export class Resolver extends DiagnosticEmitter {
778780
let numParameters = parameterNodes.length;
779781

780782
let argumentNodes: Expression[];
783+
let argumentsRange: Range;
781784
switch (node.kind) {
782-
case NodeKind.Call:
783-
argumentNodes = (<CallExpression>node).args;
785+
case NodeKind.Call: {
786+
const expr = node as CallExpression;
787+
argumentNodes = expr.args;
788+
argumentsRange = expr.argumentsRange;
784789
break;
785-
case NodeKind.New:
786-
argumentNodes = (<NewExpression>node).args;
790+
}
791+
case NodeKind.New: {
792+
const expr = node as NewExpression;
793+
argumentNodes = expr.args;
794+
argumentsRange = expr.argumentsRange;
787795
break;
788-
default:
796+
}
797+
default: {
789798
assert(false);
790799
return null;
800+
}
791801
}
792802

793803
let numArguments = argumentNodes.length;
@@ -802,12 +812,20 @@ export class Resolver extends DiagnosticEmitter {
802812
if (parameterNodes[i].parameterKind == ParameterKind.Optional) {
803813
continue;
804814
}
805-
// missing initializer -> too few arguments
806815
if (reportMode == ReportMode.Report) {
807-
this.error(
808-
DiagnosticCode.Expected_0_arguments_but_got_1,
809-
node.range, numParameters.toString(), numArguments.toString()
810-
);
816+
if (parameterNodes[i].parameterKind == ParameterKind.Rest) {
817+
// rest params are optional, but one element is needed for type inference
818+
this.error(
819+
DiagnosticCode.Type_argument_expected,
820+
argumentsRange.atEnd
821+
);
822+
} else {
823+
// missing initializer -> too few arguments
824+
this.error(
825+
DiagnosticCode.Expected_0_arguments_but_got_1,
826+
node.range, numParameters.toString(), numArguments.toString()
827+
);
828+
}
811829
}
812830
return null;
813831
}

tests/compiler/call-rest-err.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"TS2322: Type '~lib/string/String' is not assignable to type 'i32'.",
88
"sum('a', 'b')",
99
"TS2322: Type '~lib/string/String' is not assignable to type 'i32'.",
10-
"count(1, 'a')"
10+
"count(1, 'a')",
11+
"TS1140: Type argument expected.",
12+
"count()"
1113
]
1214
}

tests/compiler/call-rest-err.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ function count<T>(...args: T[]): i32 {
1212

1313
sum('a', 'b'); // expect a type mismatch error on each argument
1414
count(1, 'a'); // expect a type mismatch error on the second argument
15+
count(); // expect type inference error

0 commit comments

Comments
 (0)