Skip to content

Fixed array expression spreads into variadic const calls #52845

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
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
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29665,9 +29665,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const elementFlags: ElementFlags[] = [];
pushCachedContextualType(node);
const inDestructuringPattern = isAssignmentTarget(node);
const inConstContext = isConstContext(node);
const isSpreadIntoCallOrNew = isSpreadElement(node.parent) && isCallOrNewExpression(node.parent.parent);
const inConstContext = isSpreadIntoCallOrNew || isConstContext(node);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

initially, I wanted to adjust isConstContext - but spread arguments never have contextual types + asking for a contextual type of an argument calls getEffectiveCallArguments and we are within this call already, so it was creating an infinite loop

I was looking into other ways to check for the const context for such spread expressions but I couldn't figure out anything since I can't even check the signature (it's still resolvingSignature at this point).

const contextualType = getApparentTypeOfContextualType(node, /*contextFlags*/ undefined);
const inTupleContext = !!contextualType && someType(contextualType, isTupleLikeType);
const inTupleContext = isSpreadIntoCallOrNew || !!contextualType && someType(contextualType, isTupleLikeType);
let hasOmittedExpression = false;
for (let i = 0; i < elementCount; i++) {
const e = elements[i];
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/arraySpreadInCall.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
tests/cases/conformance/es6/spread/arraySpreadInCall.ts(21,12): error TS2554: Expected 0-1 arguments, but got 2.


==== tests/cases/conformance/es6/spread/arraySpreadInCall.ts (1 errors) ====
declare function f1(a: number, b: number, c: number, d: number, e: number, f: number): void;
f1(1, 2, 3, 4, ...[5, 6]);
f1(...[1], 2, 3, 4, 5, 6);
f1(1, 2, ...[3, 4], 5, 6);
f1(1, 2, ...[3], 4, ...[5, 6]);
f1(...[1, 2], ...[3, 4], ...[5, 6]);

declare function f2<T extends unknown[]>(...args: T): T;
const x21 = f2(...[1, 'foo'])
const x22 = f2(true, ...[1, 'foo'])

declare function f3<T extends readonly unknown[]>(...args: T): T;
const x31 = f3(...[1, 'foo'])
const x32 = f3(true, ...[1, 'foo'])

// dicovered in #52845#issuecomment-1459132562
interface IAction {
run(event?: unknown): unknown;
}
declare const action: IAction
action.run(...[100, 'foo']) // error
~~~~~~~~~~~~~~~
!!! error TS2554: Expected 0-1 arguments, but got 2.


73 changes: 73 additions & 0 deletions tests/baselines/reference/arraySpreadInCall.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
=== tests/cases/conformance/es6/spread/arraySpreadInCall.ts ===
declare function f1(a: number, b: number, c: number, d: number, e: number, f: number): void;
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))
>a : Symbol(a, Decl(arraySpreadInCall.ts, 0, 20))
>b : Symbol(b, Decl(arraySpreadInCall.ts, 0, 30))
>c : Symbol(c, Decl(arraySpreadInCall.ts, 0, 41))
>d : Symbol(d, Decl(arraySpreadInCall.ts, 0, 52))
>e : Symbol(e, Decl(arraySpreadInCall.ts, 0, 63))
>f : Symbol(f, Decl(arraySpreadInCall.ts, 0, 74))

f1(1, 2, 3, 4, ...[5, 6]);
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))

f1(...[1], 2, 3, 4, 5, 6);
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))

f1(1, 2, ...[3, 4], 5, 6);
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))

f1(1, 2, ...[3], 4, ...[5, 6]);
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))

f1(...[1, 2], ...[3, 4], ...[5, 6]);
>f1 : Symbol(f1, Decl(arraySpreadInCall.ts, 0, 0))

declare function f2<T extends unknown[]>(...args: T): T;
>f2 : Symbol(f2, Decl(arraySpreadInCall.ts, 5, 36))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 7, 20))
>args : Symbol(args, Decl(arraySpreadInCall.ts, 7, 41))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 7, 20))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 7, 20))

const x21 = f2(...[1, 'foo'])
>x21 : Symbol(x21, Decl(arraySpreadInCall.ts, 8, 5))
>f2 : Symbol(f2, Decl(arraySpreadInCall.ts, 5, 36))

const x22 = f2(true, ...[1, 'foo'])
>x22 : Symbol(x22, Decl(arraySpreadInCall.ts, 9, 5))
>f2 : Symbol(f2, Decl(arraySpreadInCall.ts, 5, 36))

declare function f3<T extends readonly unknown[]>(...args: T): T;
>f3 : Symbol(f3, Decl(arraySpreadInCall.ts, 9, 35))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 11, 20))
>args : Symbol(args, Decl(arraySpreadInCall.ts, 11, 50))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 11, 20))
>T : Symbol(T, Decl(arraySpreadInCall.ts, 11, 20))

const x31 = f3(...[1, 'foo'])
>x31 : Symbol(x31, Decl(arraySpreadInCall.ts, 12, 5))
>f3 : Symbol(f3, Decl(arraySpreadInCall.ts, 9, 35))

const x32 = f3(true, ...[1, 'foo'])
>x32 : Symbol(x32, Decl(arraySpreadInCall.ts, 13, 5))
>f3 : Symbol(f3, Decl(arraySpreadInCall.ts, 9, 35))

// dicovered in #52845#issuecomment-1459132562
interface IAction {
>IAction : Symbol(IAction, Decl(arraySpreadInCall.ts, 13, 35))

run(event?: unknown): unknown;
>run : Symbol(IAction.run, Decl(arraySpreadInCall.ts, 16, 19))
>event : Symbol(event, Decl(arraySpreadInCall.ts, 17, 8))
}
declare const action: IAction
>action : Symbol(action, Decl(arraySpreadInCall.ts, 19, 13))
>IAction : Symbol(IAction, Decl(arraySpreadInCall.ts, 13, 35))

action.run(...[100, 'foo']) // error
>action.run : Symbol(IAction.run, Decl(arraySpreadInCall.ts, 16, 19))
>action : Symbol(action, Decl(arraySpreadInCall.ts, 19, 13))
>run : Symbol(IAction.run, Decl(arraySpreadInCall.ts, 16, 19))


142 changes: 142 additions & 0 deletions tests/baselines/reference/arraySpreadInCall.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
=== tests/cases/conformance/es6/spread/arraySpreadInCall.ts ===
declare function f1(a: number, b: number, c: number, d: number, e: number, f: number): void;
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>a : number
>b : number
>c : number
>d : number
>e : number
>f : number

f1(1, 2, 3, 4, ...[5, 6]);
>f1(1, 2, 3, 4, ...[5, 6]) : void
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>1 : 1
>2 : 2
>3 : 3
>4 : 4
>...[5, 6] : number
>[5, 6] : readonly [number, number]
>5 : 5
>6 : 6

f1(...[1], 2, 3, 4, 5, 6);
>f1(...[1], 2, 3, 4, 5, 6) : void
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>...[1] : number
>[1] : readonly [number]
>1 : 1
>2 : 2
>3 : 3
>4 : 4
>5 : 5
>6 : 6

f1(1, 2, ...[3, 4], 5, 6);
>f1(1, 2, ...[3, 4], 5, 6) : void
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>1 : 1
>2 : 2
>...[3, 4] : number
>[3, 4] : readonly [number, number]
>3 : 3
>4 : 4
>5 : 5
>6 : 6

f1(1, 2, ...[3], 4, ...[5, 6]);
>f1(1, 2, ...[3], 4, ...[5, 6]) : void
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>1 : 1
>2 : 2
>...[3] : number
>[3] : readonly [number]
>3 : 3
>4 : 4
>...[5, 6] : number
>[5, 6] : readonly [number, number]
>5 : 5
>6 : 6

f1(...[1, 2], ...[3, 4], ...[5, 6]);
>f1(...[1, 2], ...[3, 4], ...[5, 6]) : void
>f1 : (a: number, b: number, c: number, d: number, e: number, f: number) => void
>...[1, 2] : number
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2
>...[3, 4] : number
>[3, 4] : readonly [number, number]
>3 : 3
>4 : 4
>...[5, 6] : number
>[5, 6] : readonly [number, number]
>5 : 5
>6 : 6

declare function f2<T extends unknown[]>(...args: T): T;
>f2 : <T extends unknown[]>(...args: T) => T
>args : T

const x21 = f2(...[1, 'foo'])
>x21 : [number, string]
>f2(...[1, 'foo']) : [number, string]
>f2 : <T extends unknown[]>(...args: T) => T
>...[1, 'foo'] : string | number
>[1, 'foo'] : readonly [number, string]
>1 : 1
>'foo' : "foo"

const x22 = f2(true, ...[1, 'foo'])
>x22 : [boolean, number, string]
>f2(true, ...[1, 'foo']) : [boolean, number, string]
>f2 : <T extends unknown[]>(...args: T) => T
>true : true
>...[1, 'foo'] : string | number
>[1, 'foo'] : readonly [number, string]
>1 : 1
>'foo' : "foo"

declare function f3<T extends readonly unknown[]>(...args: T): T;
>f3 : <T extends readonly unknown[]>(...args: T) => T
>args : T

const x31 = f3(...[1, 'foo'])
>x31 : [number, string]
>f3(...[1, 'foo']) : [number, string]
>f3 : <T extends readonly unknown[]>(...args: T) => T
>...[1, 'foo'] : string | number
>[1, 'foo'] : readonly [number, string]
>1 : 1
>'foo' : "foo"

const x32 = f3(true, ...[1, 'foo'])
>x32 : [boolean, number, string]
>f3(true, ...[1, 'foo']) : [boolean, number, string]
>f3 : <T extends readonly unknown[]>(...args: T) => T
>true : true
>...[1, 'foo'] : string | number
>[1, 'foo'] : readonly [number, string]
>1 : 1
>'foo' : "foo"

// dicovered in #52845#issuecomment-1459132562
interface IAction {
run(event?: unknown): unknown;
>run : (event?: unknown) => unknown
>event : unknown
}
declare const action: IAction
>action : IAction

action.run(...[100, 'foo']) // error
>action.run(...[100, 'foo']) : unknown
>action.run : (event?: unknown) => unknown
>action : IAction
>run : (event?: unknown) => unknown
>...[100, 'foo'] : string | number
>[100, 'foo'] : readonly [number, string]
>100 : 100
>'foo' : "foo"


24 changes: 12 additions & 12 deletions tests/baselines/reference/callChain.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ o1?.(...[1, 2]);
>o1?.(...[1, 2]) : number | undefined
>o1 : ((...args: any[]) => number) | undefined
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2

Expand All @@ -25,7 +25,7 @@ o1?.(1, ...[2, 3], 4);
>o1 : ((...args: any[]) => number) | undefined
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand Down Expand Up @@ -54,7 +54,7 @@ o2?.b(...[1, 2]);
>o2 : { b: (...args: any[]) => number; } | undefined
>b : ((...args: any[]) => number) | undefined
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2

Expand All @@ -65,7 +65,7 @@ o2?.b(1, ...[2, 3], 4);
>b : ((...args: any[]) => number) | undefined
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand All @@ -89,7 +89,7 @@ o2?.["b"](...[1, 2]);
>o2 : { b: (...args: any[]) => number; } | undefined
>"b" : "b"
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2

Expand All @@ -100,7 +100,7 @@ o2?.["b"](1, ...[2, 3], 4);
>"b" : "b"
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand Down Expand Up @@ -135,7 +135,7 @@ o3.b?.(...[1, 2]).c;
>o3 : { b: ((...args: any[]) => { c: string; }) | undefined; }
>b : ((...args: any[]) => { c: string; }) | undefined
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2
>c : string | undefined
Expand All @@ -148,7 +148,7 @@ o3.b?.(1, ...[2, 3], 4).c;
>b : ((...args: any[]) => { c: string; }) | undefined
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand Down Expand Up @@ -178,7 +178,7 @@ o3.b?.(...[1, 2])["c"];
>o3 : { b: ((...args: any[]) => { c: string; }) | undefined; }
>b : ((...args: any[]) => { c: string; }) | undefined
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2
>"c" : "c"
Expand All @@ -191,7 +191,7 @@ o3.b?.(1, ...[2, 3], 4)["c"];
>b : ((...args: any[]) => { c: string; }) | undefined
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand Down Expand Up @@ -221,7 +221,7 @@ o3["b"]?.(...[1, 2]).c;
>o3 : { b: ((...args: any[]) => { c: string; }) | undefined; }
>"b" : "b"
>...[1, 2] : number
>[1, 2] : number[]
>[1, 2] : readonly [number, number]
>1 : 1
>2 : 2
>c : string | undefined
Expand All @@ -234,7 +234,7 @@ o3["b"]?.(1, ...[2, 3], 4).c;
>"b" : "b"
>1 : 1
>...[2, 3] : number
>[2, 3] : number[]
>[2, 3] : readonly [number, number]
>2 : 2
>3 : 3
>4 : 4
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callWithSpread.types
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ xa[1].foo(1, 2, ...a, "abc");
>1 : 1
>foo : (x: number, y: number, ...z: string[]) => X
>...[1, 2, "abc"] : string | number
>[1, 2, "abc"] : (string | number)[]
>[1, 2, "abc"] : readonly [number, number, string]
>1 : 1
>2 : 2
>"abc" : "abc"
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callWithSpreadES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ xa[1].foo(1, 2, ...a, "abc");
>1 : 1
>foo : (x: number, y: number, ...z: string[]) => any
>...[1, 2, "abc"] : string | number
>[1, 2, "abc"] : (string | number)[]
>[1, 2, "abc"] : readonly [number, number, string]
>1 : 1
>2 : 2
>"abc" : "abc"
Expand Down
Loading