Skip to content

Commit 95e649a

Browse files
squirlyRyanCavanaugh
authored andcommitted
Cherry-pick bug fix #30758 for 3.4 (#30881)
<!-- Thank you for submitting a pull request! Here's a checklist you might find useful. * [ ] There is an associated issue that is labeled 'Bug' or 'help wanted' * [ ] Code is up-to-date with the `master` branch * [ ] You've successfully run `gulp runtests` locally * [ ] There are new or updated unit tests validating the change Refer to CONTRIBUTING.MD for more details. https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md --> Fixes #30647 Upstream bugfix cherry-picked for patch release on version 3.4. See original bug for discussion: #30647 (comment)
1 parent 1871048 commit 95e649a

File tree

6 files changed

+68
-32
lines changed

6 files changed

+68
-32
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20586,7 +20586,7 @@ namespace ts {
2058620586
spanArray = createNodeArray(args);
2058720587
if (hasSpreadArgument && argCount) {
2058820588
const nextArg = elementAt(args, getSpreadArgumentIndex(args) + 1) || undefined;
20589-
spanArray = createNodeArray(args.slice(max > argCount && nextArg ? args.indexOf(nextArg) : max));
20589+
spanArray = createNodeArray(args.slice(max > argCount && nextArg ? args.indexOf(nextArg) : Math.min(max, args.length - 1)));
2059020590
}
2059120591
}
2059220592
else {

tests/baselines/reference/callWithSpread2.errors.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(22,13): error TS2556: Expected 1 arguments, but got 2 or more.
2-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(23,7): error TS2556: Expected 0 arguments, but got 1 or more.
3-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(26,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
1+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(23,13): error TS2556: Expected 1 arguments, but got 2 or more.
2+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(24,7): error TS2556: Expected 0 arguments, but got 1 or more.
3+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(27,5): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
44
Type 'string' is not assignable to type 'number'.
5-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(27,5): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
6-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(28,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
5+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(28,5): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
6+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(29,13): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
77
Type 'string' is not assignable to type 'number'.
8-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(29,13): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
9-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(30,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
8+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(30,13): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
9+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(31,11): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
1010
Type 'string' is not assignable to type 'number'.
11-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(31,11): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
12-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(32,8): error TS2556: Expected 1-3 arguments, but got 0 or more.
11+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(32,11): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
1312
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(33,8): error TS2556: Expected 1-3 arguments, but got 0 or more.
14-
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,8): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
13+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,8): error TS2556: Expected 1-3 arguments, but got 0 or more.
14+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(35,8): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
15+
tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(36,14): error TS2556: Expected 2-4 arguments, but got 1 or more.
1516

1617

17-
==== tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts (11 errors) ====
18+
==== tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts (12 errors) ====
1819
declare function all(a?: number, b?: number): void;
1920
declare function weird(a?: number | string, b?: number | string): void;
2021
declare function prefix(s: string, a?: number, b?: number): void;
2122
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
2223
declare function normal(s: string): void;
2324
declare function thunk(): string;
25+
declare function prefix2(s: string, n: number, a?: number, b?: number): void;
2426

2527
declare var ns: number[];
2628
declare var mixed: (number | string)[];
@@ -76,4 +78,8 @@ tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,8): erro
7678
prefix(...tuple)
7779
~~~~~~~~
7880
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
81+
prefix2("g", ...ns);
82+
~~~~~
83+
!!! error TS2556: Expected 2-4 arguments, but got 1 or more.
84+
!!! related TS6210 tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts:7:37: An argument for 'n' was not provided.
7985

tests/baselines/reference/callWithSpread2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare function prefix(s: string, a?: number, b?: number): void;
55
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
66
declare function normal(s: string): void;
77
declare function thunk(): string;
8+
declare function prefix2(s: string, n: number, a?: number, b?: number): void;
89

910
declare var ns: number[];
1011
declare var mixed: (number | string)[];
@@ -33,6 +34,7 @@ rest("f", ...tuple)
3334
prefix(...ns) // required parameters are required
3435
prefix(...mixed)
3536
prefix(...tuple)
37+
prefix2("g", ...ns);
3638

3739

3840
//// [callWithSpread2.js]
@@ -56,3 +58,4 @@ rest.apply(void 0, ["f"].concat(tuple));
5658
prefix.apply(void 0, ns); // required parameters are required
5759
prefix.apply(void 0, mixed);
5860
prefix.apply(void 0, tuple);
61+
prefix2.apply(void 0, ["g"].concat(ns));

tests/baselines/reference/callWithSpread2.symbols

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,84 +29,95 @@ declare function normal(s: string): void;
2929
declare function thunk(): string;
3030
>thunk : Symbol(thunk, Decl(callWithSpread2.ts, 4, 41))
3131

32+
declare function prefix2(s: string, n: number, a?: number, b?: number): void;
33+
>prefix2 : Symbol(prefix2, Decl(callWithSpread2.ts, 5, 33))
34+
>s : Symbol(s, Decl(callWithSpread2.ts, 6, 25))
35+
>n : Symbol(n, Decl(callWithSpread2.ts, 6, 35))
36+
>a : Symbol(a, Decl(callWithSpread2.ts, 6, 46))
37+
>b : Symbol(b, Decl(callWithSpread2.ts, 6, 58))
38+
3239
declare var ns: number[];
33-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
40+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
3441

3542
declare var mixed: (number | string)[];
36-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
43+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
3744

3845
declare var tuple: [number, string];
39-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
46+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
4047

4148
// good
4249
all(...ns)
4350
>all : Symbol(all, Decl(callWithSpread2.ts, 0, 0))
44-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
51+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
4552

4653
weird(...ns)
4754
>weird : Symbol(weird, Decl(callWithSpread2.ts, 0, 51))
48-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
55+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
4956

5057
weird(...mixed)
5158
>weird : Symbol(weird, Decl(callWithSpread2.ts, 0, 51))
52-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
59+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
5360

5461
weird(...tuple)
5562
>weird : Symbol(weird, Decl(callWithSpread2.ts, 0, 51))
56-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
63+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
5764

5865
prefix("a", ...ns)
5966
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
60-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
67+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
6168

6269
rest("d", ...ns)
6370
>rest : Symbol(rest, Decl(callWithSpread2.ts, 2, 65))
64-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
71+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
6572

6673

6774
// extra arguments
6875
normal("g", ...ns)
6976
>normal : Symbol(normal, Decl(callWithSpread2.ts, 3, 83))
70-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
77+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
7178

7279
thunk(...ns)
7380
>thunk : Symbol(thunk, Decl(callWithSpread2.ts, 4, 41))
74-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
81+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
7582

7683
// bad
7784
all(...mixed)
7885
>all : Symbol(all, Decl(callWithSpread2.ts, 0, 0))
79-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
86+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
8087

8188
all(...tuple)
8289
>all : Symbol(all, Decl(callWithSpread2.ts, 0, 0))
83-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
90+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
8491

8592
prefix("b", ...mixed)
8693
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
87-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
94+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
8895

8996
prefix("c", ...tuple)
9097
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
91-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
98+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
9299

93100
rest("e", ...mixed)
94101
>rest : Symbol(rest, Decl(callWithSpread2.ts, 2, 65))
95-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
102+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
96103

97104
rest("f", ...tuple)
98105
>rest : Symbol(rest, Decl(callWithSpread2.ts, 2, 65))
99-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
106+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
100107

101108
prefix(...ns) // required parameters are required
102109
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
103-
>ns : Symbol(ns, Decl(callWithSpread2.ts, 7, 11))
110+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
104111

105112
prefix(...mixed)
106113
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
107-
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 8, 11))
114+
>mixed : Symbol(mixed, Decl(callWithSpread2.ts, 9, 11))
108115

109116
prefix(...tuple)
110117
>prefix : Symbol(prefix, Decl(callWithSpread2.ts, 1, 71))
111-
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 9, 11))
118+
>tuple : Symbol(tuple, Decl(callWithSpread2.ts, 10, 11))
119+
120+
prefix2("g", ...ns);
121+
>prefix2 : Symbol(prefix2, Decl(callWithSpread2.ts, 5, 33))
122+
>ns : Symbol(ns, Decl(callWithSpread2.ts, 8, 11))
112123

tests/baselines/reference/callWithSpread2.types

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ declare function normal(s: string): void;
2929
declare function thunk(): string;
3030
>thunk : () => string
3131

32+
declare function prefix2(s: string, n: number, a?: number, b?: number): void;
33+
>prefix2 : (s: string, n: number, a?: number, b?: number) => void
34+
>s : string
35+
>n : number
36+
>a : number
37+
>b : number
38+
3239
declare var ns: number[];
3340
>ns : number[]
3441

@@ -151,3 +158,10 @@ prefix(...tuple)
151158
>...tuple : string | number
152159
>tuple : [number, string]
153160

161+
prefix2("g", ...ns);
162+
>prefix2("g", ...ns) : void
163+
>prefix2 : (s: string, n: number, a?: number, b?: number) => void
164+
>"g" : "g"
165+
>...ns : number
166+
>ns : number[]
167+

tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare function prefix(s: string, a?: number, b?: number): void;
44
declare function rest(s: string, a?: number, b?: number, ...rest: number[]): void;
55
declare function normal(s: string): void;
66
declare function thunk(): string;
7+
declare function prefix2(s: string, n: number, a?: number, b?: number): void;
78

89
declare var ns: number[];
910
declare var mixed: (number | string)[];
@@ -32,3 +33,4 @@ rest("f", ...tuple)
3233
prefix(...ns) // required parameters are required
3334
prefix(...mixed)
3435
prefix(...tuple)
36+
prefix2("g", ...ns);

0 commit comments

Comments
 (0)