-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Infer from annotated parameters of context sensitive functions in the first inference pass #56460
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
Open
Andarist
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
Andarist:infer-early-from-annotated-params-of-context-sensitive-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+492
−15
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc49efd
Infer from annotated parameters of context sensitive functions in the…
Andarist 1655479
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist 8cf8770
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist 7d2278d
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist ac20ac6
add extra tests
Andarist 1334a37
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist e34897d
add extra completion test
Andarist 6bee359
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist e20c997
Merge remote-tracking branch 'origin/main' into infer-early-from-anno…
Andarist 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
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.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/contextSensitiveAnnotatedParametersInference1.ts] //// | ||
|
|
||
| === contextSensitiveAnnotatedParametersInference1.ts === | ||
| declare function test<T, A, B>(obj: { | ||
| >test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) | ||
| >A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) | ||
| >B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 27)) | ||
| >obj : Symbol(obj, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 31)) | ||
|
|
||
| ctx: T; | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 37)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) | ||
|
|
||
| a: (a: A, ctx: T) => void; | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 1, 9)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 6)) | ||
| >A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 11)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) | ||
|
|
||
| b: (b: B, ctx: T, a: A) => void; | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 2, 28)) | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 6)) | ||
| >B : Symbol(B, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 27)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 11)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 22)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 3, 19)) | ||
| >A : Symbol(A, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 24)) | ||
|
|
||
| }): void; | ||
|
|
||
| test({ | ||
| >test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) | ||
|
|
||
| ctx: 'foo', | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 6, 6)) | ||
|
|
||
| a: (a: string, ctx) => {}, | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 7, 13)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 6)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 16)) | ||
|
|
||
| b: (b: string, ctx, a) => {}, | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 8, 28)) | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 6)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 16)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 9, 21)) | ||
|
|
||
| }); | ||
|
|
||
| test({ | ||
| >test : Symbol(test, Decl(contextSensitiveAnnotatedParametersInference1.ts, 0, 0)) | ||
|
|
||
| ctx: 'foo', | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 12, 6)) | ||
|
|
||
| b: (b: string, ctx, a) => {}, | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 13, 13)) | ||
| >b : Symbol(b, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 6)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 16)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 21)) | ||
|
|
||
| a: (a: string, ctx) => {}, | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 14, 31)) | ||
| >a : Symbol(a, Decl(contextSensitiveAnnotatedParametersInference1.ts, 15, 6)) | ||
| >ctx : Symbol(ctx, Decl(contextSensitiveAnnotatedParametersInference1.ts, 15, 16)) | ||
|
|
||
| }); | ||
|
|
109 changes: 109 additions & 0 deletions
109
tests/baselines/reference/contextSensitiveAnnotatedParametersInference1.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,109 @@ | ||
| //// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference1.ts] //// | ||
|
|
||
| === contextSensitiveAnnotatedParametersInference1.ts === | ||
| declare function test<T, A, B>(obj: { | ||
| >test : <T, A, B>(obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void | ||
| > : ^ ^^ ^^ ^^ ^^ ^^^^^ | ||
| >obj : { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; } | ||
| > : ^^^^^^^ ^^^^^ ^^^^^ ^^^ | ||
|
|
||
| ctx: T; | ||
| >ctx : T | ||
| > : ^ | ||
|
|
||
| a: (a: A, ctx: T) => void; | ||
| >a : (a: A, ctx: T) => void | ||
| > : ^ ^^ ^^ ^^ ^^^^^ | ||
| >a : A | ||
| > : ^ | ||
| >ctx : T | ||
| > : ^ | ||
|
|
||
| b: (b: B, ctx: T, a: A) => void; | ||
| >b : (b: B, ctx: T, a: A) => void | ||
| > : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
| >b : B | ||
| > : ^ | ||
| >ctx : T | ||
| > : ^ | ||
| >a : A | ||
| > : ^ | ||
|
|
||
| }): void; | ||
|
|
||
| test({ | ||
| >test({ ctx: 'foo', a: (a: string, ctx) => {}, b: (b: string, ctx, a) => {},}) : void | ||
| > : ^^^^ | ||
| >test : <T, A, B>(obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void | ||
| > : ^ ^^ ^^ ^^ ^^ ^^^^^ | ||
| >{ ctx: 'foo', a: (a: string, ctx) => {}, b: (b: string, ctx, a) => {},} : { ctx: string; a: (a: string, ctx: string) => void; b: (b: string, ctx: string, a: string) => void; } | ||
| > : ^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| ctx: 'foo', | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
| >'foo' : "foo" | ||
| > : ^^^^^ | ||
|
|
||
| a: (a: string, ctx) => {}, | ||
| >a : (a: string, ctx: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^ | ||
| >(a: string, ctx) => {} : (a: string, ctx: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^ | ||
| >a : string | ||
| > : ^^^^^^ | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
|
|
||
| b: (b: string, ctx, a) => {}, | ||
| >b : (b: string, ctx: string, a: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ | ||
| >(b: string, ctx, a) => {} : (b: string, ctx: string, a: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ | ||
| >b : string | ||
| > : ^^^^^^ | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
| >a : string | ||
| > : ^^^^^^ | ||
|
|
||
| }); | ||
|
|
||
| test({ | ||
| >test({ ctx: 'foo', b: (b: string, ctx, a) => {}, a: (a: string, ctx) => {},}) : void | ||
| > : ^^^^ | ||
| >test : <T, A, B>(obj: { ctx: T; a: (a: A, ctx: T) => void; b: (b: B, ctx: T, a: A) => void; }) => void | ||
| > : ^ ^^ ^^ ^^ ^^ ^^^^^ | ||
| >{ ctx: 'foo', b: (b: string, ctx, a) => {}, a: (a: string, ctx) => {},} : { ctx: string; b: (b: string, ctx: string, a: string) => void; a: (a: string, ctx: string) => void; } | ||
| > : ^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| ctx: 'foo', | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
| >'foo' : "foo" | ||
| > : ^^^^^ | ||
|
|
||
| b: (b: string, ctx, a) => {}, | ||
| >b : (b: string, ctx: string, a: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ | ||
| >(b: string, ctx, a) => {} : (b: string, ctx: string, a: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ | ||
| >b : string | ||
| > : ^^^^^^ | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
| >a : string | ||
| > : ^^^^^^ | ||
|
|
||
| a: (a: string, ctx) => {}, | ||
| >a : (a: string, ctx: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^ | ||
| >(a: string, ctx) => {} : (a: string, ctx: string) => void | ||
| > : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^ | ||
| >a : string | ||
| > : ^^^^^^ | ||
| >ctx : string | ||
| > : ^^^^^^ | ||
|
|
||
| }); | ||
|
|
91 changes: 91 additions & 0 deletions
91
tests/baselines/reference/contextSensitiveAnnotatedParametersInference2.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,91 @@ | ||
| //// [tests/cases/compiler/contextSensitiveAnnotatedParametersInference2.ts] //// | ||
|
|
||
| === contextSensitiveAnnotatedParametersInference2.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/60047 | ||
|
|
||
| type Map<T> = { | ||
| >Map : Symbol(Map, Decl(contextSensitiveAnnotatedParametersInference2.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) | ||
|
|
||
| [P in keyof T]: T[P] extends boolean ? "boolean" : "other"; | ||
| >P : Symbol(P, Decl(contextSensitiveAnnotatedParametersInference2.ts, 3, 3)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) | ||
| >T : Symbol(T, Decl(contextSensitiveAnnotatedParametersInference2.ts, 2, 9)) | ||
| >P : Symbol(P, Decl(contextSensitiveAnnotatedParametersInference2.ts, 3, 3)) | ||
|
|
||
| }; | ||
|
|
||
| export function buildCommand<F extends Record<string, unknown>>(builderArgs: { | ||
| >buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) | ||
| >F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) | ||
| >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
| >builderArgs : Symbol(builderArgs, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 64)) | ||
|
|
||
| func: (p: F) => void; | ||
| >func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 78)) | ||
| >p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 7, 9)) | ||
| >F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) | ||
|
|
||
| params: Map<NoInfer<F>>; | ||
| >params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 7, 23)) | ||
| >Map : Symbol(Map, Decl(contextSensitiveAnnotatedParametersInference2.ts, 0, 0)) | ||
| >NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --)) | ||
| >F : Symbol(F, Decl(contextSensitiveAnnotatedParametersInference2.ts, 6, 29)) | ||
|
|
||
| }) {} | ||
|
|
||
| type Foo = { foo: boolean }; | ||
| >Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) | ||
| >foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 11, 12)) | ||
|
|
||
| buildCommand({ | ||
| >buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) | ||
|
|
||
| func: function (p: Foo) {}, | ||
| >func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 13, 14)) | ||
| >p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 14, 18)) | ||
| >Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) | ||
|
|
||
| params: { | ||
| >params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 14, 29)) | ||
|
|
||
| foo: "boolean", | ||
| >foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 15, 11)) | ||
|
|
||
| }, | ||
| }); | ||
|
|
||
| buildCommand({ | ||
| >buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) | ||
|
|
||
| func(p: Foo) {}, | ||
| >func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 20, 14)) | ||
| >p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 21, 7)) | ||
| >Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) | ||
|
|
||
| params: { | ||
| >params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 21, 18)) | ||
|
|
||
| foo: "boolean", | ||
| >foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 22, 11)) | ||
|
|
||
| }, | ||
| }); | ||
|
|
||
| buildCommand({ | ||
| >buildCommand : Symbol(buildCommand, Decl(contextSensitiveAnnotatedParametersInference2.ts, 4, 2)) | ||
|
|
||
| func: (p: Foo) => {}, | ||
| >func : Symbol(func, Decl(contextSensitiveAnnotatedParametersInference2.ts, 27, 14)) | ||
| >p : Symbol(p, Decl(contextSensitiveAnnotatedParametersInference2.ts, 28, 9)) | ||
| >Foo : Symbol(Foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 9, 5)) | ||
|
|
||
| params: { | ||
| >params : Symbol(params, Decl(contextSensitiveAnnotatedParametersInference2.ts, 28, 23)) | ||
|
|
||
| foo: "boolean", | ||
| >foo : Symbol(foo, Decl(contextSensitiveAnnotatedParametersInference2.ts, 29, 11)) | ||
|
|
||
| }, | ||
| }); | ||
|
|
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.
Is it not a little weird for
getInferenceContextandinferFromAnnotatedParametersto get called here? I'm not finding other examples where we're doing this sort of thing outside of a function intended to contextually check (which this function is not,contextuallyCheckFunctionExpressionOrObjectLiteralMethodis).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.
hm, this might be the first time this is called like it but the placement of those calls here is what fixes the bug conceptually - as in, for the added test cases it's important to infer before contextually checking those functions
Contextual check of functions can fix "requested" inference type parameters and that creates an ordering issue for #56459 as the parameter type "request" can happen before
inferFromAnnotatedParameterseven has a chance to be called on the other function that comes later in the source code.And the similar problem happens when it comes to #60047 . The conditional type asks for the inferred type of the type parameter and since by that time the
inferFromAnnotatedParameterswas not called it resolves to its constraint. Then the arguments fail to be typechecked against current parameter types ingetSignatureApplicabilityErrorso the algorithm returns, never reaching the second inference pass that starts to include context-sensitive parameters.So if those issues are meant to be fixed it's absolutely necessary to call
inferFromAnnotatedParameterswithin the first inference pass and that doesn't check context-sensitive functions, that's deferred to the second inference pass. I don't see how to even attempt to fix this in any other way given the known limitations around context-sensitive expressions and their associated implementation details