-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixed spreading iterables into arguments list directly into rest-only signatures #52838
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/baselines/reference/argumentsSpreadRestIterables(target=es5).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
tests/cases/compiler/argumentsSpreadRestIterables.tsx(1,22): error TS2304: Cannot find name 'Iterable'. | ||
tests/cases/compiler/argumentsSpreadRestIterables.tsx(8,21): error TS2461: Type '"hello"' is not an array type. | ||
tests/cases/compiler/argumentsSpreadRestIterables.tsx(10,27): error TS2461: Type '"hello"' is not an array type. | ||
tests/cases/compiler/argumentsSpreadRestIterables.tsx(15,19): error TS2461: Type '"hello"' is not an array type. | ||
tests/cases/compiler/argumentsSpreadRestIterables.tsx(17,25): error TS2461: Type '"hello"' is not an array type. | ||
|
||
|
||
==== tests/cases/compiler/argumentsSpreadRestIterables.tsx (5 errors) ==== | ||
declare const itNum: Iterable<number> | ||
~~~~~~~~ | ||
!!! error TS2304: Cannot find name 'Iterable'. | ||
|
||
;(function(...rest) {})(...itNum) | ||
;(function(a, ...rest) {})('', ...itNum) | ||
;(function(a, ...rest) {})('', true, ...itNum) | ||
|
||
declare function fn1<const T extends readonly unknown[]>(...args: T): T; | ||
const res1 = fn1(..."hello"); | ||
~~~~~~~ | ||
!!! error TS2461: Type '"hello"' is not an array type. | ||
const res2 = fn1(...itNum); | ||
const res3 = fn1(true, ..."hello"); | ||
~~~~~~~ | ||
!!! error TS2461: Type '"hello"' is not an array type. | ||
const res4 = fn1(true, ...itNum); | ||
|
||
// repro from #52781 | ||
declare function foo<T extends unknown[]>(...args: T): T; | ||
const p1 = foo(..."hello"); | ||
~~~~~~~ | ||
!!! error TS2461: Type '"hello"' is not an array type. | ||
const p2 = foo(...itNum); | ||
const p3 = foo(true, ..."hello"); | ||
~~~~~~~ | ||
!!! error TS2461: Type '"hello"' is not an array type. | ||
const p4 = foo(true, ...itNum); | ||
|
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/argumentsSpreadRestIterables(target=es5).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
=== tests/cases/compiler/argumentsSpreadRestIterables.tsx === | ||
declare const itNum: Iterable<number> | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
>Iterable : Symbol(Iterable) | ||
|
||
;(function(...rest) {})(...itNum) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 2, 11)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
;(function(a, ...rest) {})('', ...itNum) | ||
>a : Symbol(a, Decl(argumentsSpreadRestIterables.tsx, 3, 11)) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 3, 13)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
;(function(a, ...rest) {})('', true, ...itNum) | ||
>a : Symbol(a, Decl(argumentsSpreadRestIterables.tsx, 4, 11)) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 4, 13)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
declare function fn1<const T extends readonly unknown[]>(...args: T): T; | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
>args : Symbol(args, Decl(argumentsSpreadRestIterables.tsx, 6, 57)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
|
||
const res1 = fn1(..."hello"); | ||
>res1 : Symbol(res1, Decl(argumentsSpreadRestIterables.tsx, 7, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
|
||
const res2 = fn1(...itNum); | ||
>res2 : Symbol(res2, Decl(argumentsSpreadRestIterables.tsx, 8, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
const res3 = fn1(true, ..."hello"); | ||
>res3 : Symbol(res3, Decl(argumentsSpreadRestIterables.tsx, 9, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
|
||
const res4 = fn1(true, ...itNum); | ||
>res4 : Symbol(res4, Decl(argumentsSpreadRestIterables.tsx, 10, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
// repro from #52781 | ||
declare function foo<T extends unknown[]>(...args: T): T; | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
>args : Symbol(args, Decl(argumentsSpreadRestIterables.tsx, 13, 42)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
|
||
const p1 = foo(..."hello"); | ||
>p1 : Symbol(p1, Decl(argumentsSpreadRestIterables.tsx, 14, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
|
||
const p2 = foo(...itNum); | ||
>p2 : Symbol(p2, Decl(argumentsSpreadRestIterables.tsx, 15, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
const p3 = foo(true, ..."hello"); | ||
>p3 : Symbol(p3, Decl(argumentsSpreadRestIterables.tsx, 16, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
|
||
const p4 = foo(true, ...itNum); | ||
>p4 : Symbol(p4, Decl(argumentsSpreadRestIterables.tsx, 17, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
102 changes: 102 additions & 0 deletions
102
tests/baselines/reference/argumentsSpreadRestIterables(target=es5).types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
=== tests/cases/compiler/argumentsSpreadRestIterables.tsx === | ||
declare const itNum: Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
;(function(...rest) {})(...itNum) | ||
>(function(...rest) {})(...itNum) : void | ||
>(function(...rest) {}) : (...rest: Iterable<number>) => void | ||
>function(...rest) {} : (...rest: Iterable<number>) => void | ||
>rest : Iterable<number> | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
;(function(a, ...rest) {})('', ...itNum) | ||
>(function(a, ...rest) {})('', ...itNum) : void | ||
>(function(a, ...rest) {}) : (a: string, ...rest: Iterable<number>) => void | ||
>function(a, ...rest) {} : (a: string, ...rest: Iterable<number>) => void | ||
>a : string | ||
>rest : Iterable<number> | ||
>'' : "" | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
;(function(a, ...rest) {})('', true, ...itNum) | ||
>(function(a, ...rest) {})('', true, ...itNum) : void | ||
>(function(a, ...rest) {}) : (a: string, rest_0: boolean, ...rest_1: any[]) => void | ||
>function(a, ...rest) {} : (a: string, rest_0: boolean, ...rest_1: any[]) => void | ||
>a : string | ||
>rest : [boolean, ...any[]] | ||
>'' : "" | ||
>true : true | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
declare function fn1<const T extends readonly unknown[]>(...args: T): T; | ||
>fn1 : <const T extends readonly unknown[]>(...args: T) => T | ||
>args : T | ||
|
||
const res1 = fn1(..."hello"); | ||
>res1 : readonly any[] | ||
>fn1(..."hello") : readonly any[] | ||
>fn1 : <const T extends readonly unknown[]>(...args: T) => T | ||
>..."hello" : any | ||
>"hello" : "hello" | ||
|
||
const res2 = fn1(...itNum); | ||
>res2 : Iterable<number> | ||
>fn1(...itNum) : Iterable<number> | ||
>fn1 : <const T extends readonly unknown[]>(...args: T) => T | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
const res3 = fn1(true, ..."hello"); | ||
>res3 : readonly [true, ...any[]] | ||
>fn1(true, ..."hello") : readonly [true, ...any[]] | ||
>fn1 : <const T extends readonly unknown[]>(...args: T) => T | ||
>true : true | ||
>..."hello" : any | ||
>"hello" : "hello" | ||
|
||
const res4 = fn1(true, ...itNum); | ||
>res4 : readonly [true, ...any[]] | ||
>fn1(true, ...itNum) : readonly [true, ...any[]] | ||
>fn1 : <const T extends readonly unknown[]>(...args: T) => T | ||
>true : true | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
// repro from #52781 | ||
declare function foo<T extends unknown[]>(...args: T): T; | ||
>foo : <T extends unknown[]>(...args: T) => T | ||
>args : T | ||
|
||
const p1 = foo(..."hello"); | ||
>p1 : any[] | ||
>foo(..."hello") : any[] | ||
>foo : <T extends unknown[]>(...args: T) => T | ||
>..."hello" : any | ||
>"hello" : "hello" | ||
|
||
const p2 = foo(...itNum); | ||
>p2 : Iterable<number> | ||
>foo(...itNum) : Iterable<number> | ||
>foo : <T extends unknown[]>(...args: T) => T | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
||
const p3 = foo(true, ..."hello"); | ||
>p3 : [boolean, ...any[]] | ||
>foo(true, ..."hello") : [boolean, ...any[]] | ||
>foo : <T extends unknown[]>(...args: T) => T | ||
>true : true | ||
>..."hello" : any | ||
>"hello" : "hello" | ||
|
||
const p4 = foo(true, ...itNum); | ||
>p4 : [boolean, ...any[]] | ||
>foo(true, ...itNum) : [boolean, ...any[]] | ||
>foo : <T extends unknown[]>(...args: T) => T | ||
>true : true | ||
>...itNum : Iterable<number> | ||
>itNum : Iterable<number> | ||
|
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/argumentsSpreadRestIterables(target=esnext).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
=== tests/cases/compiler/argumentsSpreadRestIterables.tsx === | ||
declare const itNum: Iterable<number> | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) | ||
|
||
;(function(...rest) {})(...itNum) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 2, 11)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
;(function(a, ...rest) {})('', ...itNum) | ||
>a : Symbol(a, Decl(argumentsSpreadRestIterables.tsx, 3, 11)) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 3, 13)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
;(function(a, ...rest) {})('', true, ...itNum) | ||
>a : Symbol(a, Decl(argumentsSpreadRestIterables.tsx, 4, 11)) | ||
>rest : Symbol(rest, Decl(argumentsSpreadRestIterables.tsx, 4, 13)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
declare function fn1<const T extends readonly unknown[]>(...args: T): T; | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
>args : Symbol(args, Decl(argumentsSpreadRestIterables.tsx, 6, 57)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 6, 21)) | ||
|
||
const res1 = fn1(..."hello"); | ||
>res1 : Symbol(res1, Decl(argumentsSpreadRestIterables.tsx, 7, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
|
||
const res2 = fn1(...itNum); | ||
>res2 : Symbol(res2, Decl(argumentsSpreadRestIterables.tsx, 8, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
const res3 = fn1(true, ..."hello"); | ||
>res3 : Symbol(res3, Decl(argumentsSpreadRestIterables.tsx, 9, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
|
||
const res4 = fn1(true, ...itNum); | ||
>res4 : Symbol(res4, Decl(argumentsSpreadRestIterables.tsx, 10, 5)) | ||
>fn1 : Symbol(fn1, Decl(argumentsSpreadRestIterables.tsx, 4, 46)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
// repro from #52781 | ||
declare function foo<T extends unknown[]>(...args: T): T; | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
>args : Symbol(args, Decl(argumentsSpreadRestIterables.tsx, 13, 42)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
>T : Symbol(T, Decl(argumentsSpreadRestIterables.tsx, 13, 21)) | ||
|
||
const p1 = foo(..."hello"); | ||
>p1 : Symbol(p1, Decl(argumentsSpreadRestIterables.tsx, 14, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
|
||
const p2 = foo(...itNum); | ||
>p2 : Symbol(p2, Decl(argumentsSpreadRestIterables.tsx, 15, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
||
const p3 = foo(true, ..."hello"); | ||
>p3 : Symbol(p3, Decl(argumentsSpreadRestIterables.tsx, 16, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
|
||
const p4 = foo(true, ...itNum); | ||
>p4 : Symbol(p4, Decl(argumentsSpreadRestIterables.tsx, 17, 5)) | ||
>foo : Symbol(foo, Decl(argumentsSpreadRestIterables.tsx, 10, 33)) | ||
>itNum : Symbol(itNum, Decl(argumentsSpreadRestIterables.tsx, 0, 13)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
likely this shouldn't always produce mutable types but there are other bugs here, like this one:
I would prefer to fix this in a followup PR as the fix for this largely lies elsewhere - then I could revisit this piece of code to produce mutable/readonly array/tuple types based on
inConstContext
.Sideways q - should
T
in the broken case be inferred asreadonly (number | string)[]
or should it bereadonly [1, 'bar']
? In other words - should the spread array expression inherit the const context?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the latter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The justification in my mind being that it should match what you get if you write
foo(1, 'bar')
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think that the latter is better but currently it's complicated to implement it. IIRC the easiest way to achieve this would be through
getEffectiveCallArguments
but it creates a cycle for spreads.getContextualTypeForArgument
/getContextualTypeForArgumentAtIndex
call back intogetEffectiveCallArguments
. I think that I've even seen a comment somewhere about spread arguments never needing the contextual type - so this would be the first time when that would actually have to become supported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm missing something, but is it possible to yank the required code out of
getEffectiveCallArguments
into a helper so that it's not directly used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe. I can't say that I explored this in full yet. I might have also missed some additional things from my initial investigation, it's a little bit stale in my memory ;p
I could try to give this a shot in a followup PR unless you strongly think that it should go in together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm mistaken, your code sample behaves the same between
main
and this PR, so, I feel like it's fine to leave as-is and then report a bug for further refinement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(But do tell me if I'm right or wrong here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ye, the behavior is the same.
It seems that I already started to work on further improvements related to this here (who could have guessed that? 😱 ) and I even mentioned some more details around that issue with
getEffectiveCallArguments
in the comment hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see your memory is as stale as mine is on a regular basis