-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add fallback when both co- and contra-variant inference candidates exist #54072
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 2 commits
Commits
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
66 changes: 66 additions & 0 deletions
66
tests/baselines/reference/coAndContraVariantInferences5.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,66 @@ | ||
=== tests/cases/compiler/coAndContraVariantInferences5.ts === | ||
type Thing = 'a' | 'b'; | ||
>Thing : Symbol(Thing, Decl(coAndContraVariantInferences5.ts, 0, 0)) | ||
|
||
function f( | ||
>f : Symbol(f, Decl(coAndContraVariantInferences5.ts, 0, 23)) | ||
|
||
options: SelectOptions<Thing>, | ||
>options : Symbol(options, Decl(coAndContraVariantInferences5.ts, 2, 11)) | ||
>SelectOptions : Symbol(SelectOptions, Decl(coAndContraVariantInferences5.ts, 17, 2)) | ||
>Thing : Symbol(Thing, Decl(coAndContraVariantInferences5.ts, 0, 0)) | ||
|
||
onChange: (status: Thing | null) => void, | ||
>onChange : Symbol(onChange, Decl(coAndContraVariantInferences5.ts, 3, 34)) | ||
>status : Symbol(status, Decl(coAndContraVariantInferences5.ts, 4, 15)) | ||
>Thing : Symbol(Thing, Decl(coAndContraVariantInferences5.ts, 0, 0)) | ||
|
||
): void { | ||
select({ | ||
>select : Symbol(select, Decl(coAndContraVariantInferences5.ts, 10, 1)) | ||
|
||
options, | ||
>options : Symbol(options, Decl(coAndContraVariantInferences5.ts, 6, 12)) | ||
|
||
onChange, | ||
>onChange : Symbol(onChange, Decl(coAndContraVariantInferences5.ts, 7, 16)) | ||
|
||
}); | ||
} | ||
|
||
declare function select<KeyT extends string>(props: SelectProps<KeyT>): void; | ||
>select : Symbol(select, Decl(coAndContraVariantInferences5.ts, 10, 1)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 12, 24)) | ||
>props : Symbol(props, Decl(coAndContraVariantInferences5.ts, 12, 45)) | ||
>SelectProps : Symbol(SelectProps, Decl(coAndContraVariantInferences5.ts, 12, 77)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 12, 24)) | ||
|
||
type SelectProps<KeyT extends string> = { | ||
>SelectProps : Symbol(SelectProps, Decl(coAndContraVariantInferences5.ts, 12, 77)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 14, 17)) | ||
|
||
options?: SelectOptions<KeyT>; | ||
>options : Symbol(options, Decl(coAndContraVariantInferences5.ts, 14, 41)) | ||
>SelectOptions : Symbol(SelectOptions, Decl(coAndContraVariantInferences5.ts, 17, 2)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 14, 17)) | ||
|
||
onChange: (key: KeyT) => void; | ||
>onChange : Symbol(onChange, Decl(coAndContraVariantInferences5.ts, 15, 34)) | ||
>key : Symbol(key, Decl(coAndContraVariantInferences5.ts, 16, 15)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 14, 17)) | ||
|
||
}; | ||
|
||
type SelectOptions<KeyT extends string> = | ||
>SelectOptions : Symbol(SelectOptions, Decl(coAndContraVariantInferences5.ts, 17, 2)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 19, 19)) | ||
|
||
| Array<{key: KeyT}> | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>key : Symbol(key, Decl(coAndContraVariantInferences5.ts, 20, 13)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 19, 19)) | ||
|
||
| Array<KeyT>; | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>KeyT : Symbol(KeyT, Decl(coAndContraVariantInferences5.ts, 19, 19)) | ||
|
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/coAndContraVariantInferences5.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,53 @@ | ||
=== tests/cases/compiler/coAndContraVariantInferences5.ts === | ||
type Thing = 'a' | 'b'; | ||
>Thing : "a" | "b" | ||
|
||
function f( | ||
>f : (options: SelectOptions<Thing>, onChange: (status: Thing | null) => void) => void | ||
|
||
options: SelectOptions<Thing>, | ||
>options : SelectOptions<Thing> | ||
|
||
onChange: (status: Thing | null) => void, | ||
>onChange : (status: Thing | null) => void | ||
>status : Thing | null | ||
|
||
): void { | ||
select({ | ||
>select({ options, onChange, }) : void | ||
>select : <KeyT extends string>(props: SelectProps<KeyT>) => void | ||
>{ options, onChange, } : { options: SelectOptions<Thing>; onChange: (status: Thing | null) => void; } | ||
|
||
options, | ||
>options : SelectOptions<Thing> | ||
|
||
onChange, | ||
>onChange : (status: Thing | null) => void | ||
|
||
}); | ||
} | ||
|
||
declare function select<KeyT extends string>(props: SelectProps<KeyT>): void; | ||
>select : <KeyT extends string>(props: SelectProps<KeyT>) => void | ||
>props : SelectProps<KeyT> | ||
|
||
type SelectProps<KeyT extends string> = { | ||
>SelectProps : SelectProps<KeyT> | ||
|
||
options?: SelectOptions<KeyT>; | ||
>options : SelectOptions<KeyT> | undefined | ||
|
||
onChange: (key: KeyT) => void; | ||
>onChange : (key: KeyT) => void | ||
>key : KeyT | ||
|
||
}; | ||
|
||
type SelectOptions<KeyT extends string> = | ||
>SelectOptions : SelectOptions<KeyT> | ||
|
||
| Array<{key: KeyT}> | ||
>key : KeyT | ||
|
||
| Array<KeyT>; | ||
|
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,25 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
type Thing = 'a' | 'b'; | ||
|
||
function f( | ||
options: SelectOptions<Thing>, | ||
onChange: (status: Thing | null) => void, | ||
): void { | ||
select({ | ||
options, | ||
onChange, | ||
}); | ||
} | ||
|
||
declare function select<KeyT extends string>(props: SelectProps<KeyT>): void; | ||
|
||
type SelectProps<KeyT extends string> = { | ||
options?: SelectOptions<KeyT>; | ||
onChange: (key: KeyT) => void; | ||
}; | ||
|
||
type SelectOptions<KeyT extends string> = | ||
| Array<{key: KeyT}> | ||
| Array<KeyT>; |
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.
Part of #52180 was motivated by noticing that when we treat the "current" inference as special, we incur some order-dependent behavior. Where this came up (if my memory was correct) was when I was editing the
strictFunctionTypes
PR, where I could edit the file and see errors change around non-deterministically.I tested restoring the change from #52180 and this PR still passes (i.e. putting back
other !== inference && getConstraintOfTypeParameter(other.typeParameter) !== inference.typeParameter
), which makes me feel like reverting it back to the state in #52123 that skips overother === inference
will bring back that oddity (and we just don't have a test for it).I'll try and see if I can make it break again like I did the first time.
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.
It turns out we deleted the code where I was able to reproduce this, so, I guess I don't really know anymore.
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.
Looking carefully at the code you had in #52180 and the original code plus the line I added here, I've convinced myself they're just two ways of saying the same thing. So I'll restore the change from #52180.
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.
Oh! You're right, I missed that new line.
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.
In that case, I'm happy with whichever is more performant or clearer. They're definitely equivalent now that I think closely.