Skip to content

Filter possible contextual return types from unions for async functions and generators - #51196

Merged
Ron Buckton (rbuckton) merged 13 commits into
microsoft:mainfrom
Andarist:fix/contextual-return-type-asyncs-and-generators
Oct 26, 2023
Merged

Filter possible contextual return types from unions for async functions and generators#51196
Ron Buckton (rbuckton) merged 13 commits into
microsoft:mainfrom
Andarist:fix/contextual-return-type-asyncs-and-generators

Conversation

@Andarist

@AndaristMateusz Burzyński (Andarist) commented Oct 17, 2022

Copy link
Copy Markdown
Contributor

fixes#51187
fixes#47682
fixes#41428
supersedes #47683
fixes#42439

Comment threadsrc/compiler/checker.ts Outdated
Comment on lines +27190 to +27192
if (functionFlags & FunctionFlags.Async) {
return !!getAwaitedTypeOfPromise(returnType);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This block supersedes my other attempt from a couple of months back at fixing #47682 . I think that the fix here is better but perhaps that older one might be still interesting for reviewers

Comment threadsrc/compiler/checker.ts Outdated
return checkGeneratorInstantiationAssignabilityToReturnType(returnType, functionFlags, /*errorNode*/ undefined);
}
if (functionFlags & FunctionFlags.Async) {
return !!getAwaitedTypeOfPromise(returnType);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

const g: OrGen = function* () {
>g : OrGen
>function* () { return (num) => console.log(num);} : () => Generator<never, (num: number) => void, unknown>
>function* () { return (num) => console.log(num);} : () => Generator<never, (num: number) => void, undefined>

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

All 3 affected baselines are directly related to the change and they are improvements/fixes

@Andarist

Copy link
Copy Markdown
ContributorAuthor

Jake Bailey (@jakebailey) what would you say about being nerd-sniped into reviewing this one? 😅

@jakebailey

Copy link
Copy Markdown
Member

I can at least be nerd sniped into running the tests.

TypeScript Bot (@typescript-bot) test this
TypeScript Bot (@typescript-bot) test top100
TypeScript Bot (@typescript-bot) user test this
TypeScript Bot (@typescript-bot) run dt
TypeScript Bot (@typescript-bot) perf test this faster
TypeScript Bot (@typescript-bot) pack this

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based user code test suite on this PR at dfe955a. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite on this PR at dfe955a. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the tarball bundle task on this PR at dfe955a. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the extended test suite on this PR at dfe955a. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the parallelized Definitely Typed test suite on this PR at dfe955a. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the abridged perf test suite on this PR at dfe955a. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
"devDependencies": {
"typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/149519/artifacts?artifactName=tgz&fileId=45CA82B3CD054E8D781CD80B5EB60780F605B752B13B032ECE34A133C4A9682902&fileName=/typescript-5.1.0-insiders.20230315.tgz"
}
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.1.0-pr-51196-8".;

@Andarist

Copy link
Copy Markdown
ContributorAuthor

I can at least be nerd sniped into running the tests.

I'll take it, thanks!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user test suite comparing main and refs/pull/51196/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..51196

Metricmain51196DeltaBestWorstp-value
Angular - node (v16.17.1, x64)
Memory used361,458k (± 0.01%)361,441k (± 0.01%)~361,393k361,472kp=0.298 n=6
Parse Time3.51s (± 0.58%)3.53s (± 0.47%)~3.51s3.55sp=0.084 n=6
Bind Time1.18s (± 0.69%)1.18s (± 0.35%)~1.18s1.19sp=0.206 n=6
Check Time9.46s (± 0.54%)9.45s (± 0.40%)~9.40s9.49sp=0.870 n=6
Emit Time7.91s (± 0.80%)7.91s (± 0.41%)~7.86s7.94sp=0.517 n=6
Total Time22.05s (± 0.28%)22.07s (± 0.34%)~21.96s22.15sp=0.376 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used193,215k (± 0.76%)193,124k (± 0.70%)~192,510k195,888kp=0.230 n=6
Parse Time1.57s (± 1.25%)1.58s (± 1.25%)~1.54s1.60sp=0.514 n=6
Bind Time0.82s (± 1.25%)0.82s (± 0.50%)~0.81s0.82sp=0.390 n=6
Check Time10.07s (± 0.39%)10.02s (± 0.39%)~9.96s10.07sp=0.063 n=6
Emit Time2.99s (± 0.52%)2.99s (± 1.08%)~2.94s3.02sp=0.461 n=6
Total Time15.44s (± 0.26%)15.40s (± 0.36%)~15.32s15.46sp=0.172 n=6
Monaco - node (v16.17.1, x64)
Memory used346,709k (± 0.00%)346,704k (± 0.00%)~346,691k346,719kp=0.810 n=6
Parse Time2.72s (± 0.49%)2.73s (± 0.44%)~2.72s2.75sp=0.101 n=6
Bind Time1.09s (± 0.47%)1.09s (± 0.37%)~1.09s1.10sp=0.114 n=6
Check Time7.74s (± 0.28%)7.71s (± 0.29%)~7.69s7.75sp=0.107 n=6
Emit Time4.46s (± 0.52%)4.47s (± 0.59%)~4.43s4.51sp=0.405 n=6
Total Time15.99s (± 0.31%)16.00s (± 0.17%)~15.98s16.05sp=0.257 n=6
TFS - node (v16.17.1, x64)
Memory used300,126k (± 0.01%)300,131k (± 0.01%)~300,097k300,192kp=0.688 n=6
Parse Time2.17s (± 0.69%)2.17s (± 0.38%)~2.16s2.18sp=0.738 n=6
Bind Time1.23s (± 0.80%)1.24s (± 0.68%)~1.22s1.24sp=0.256 n=6
Check Time7.16s (± 0.29%)7.17s (± 0.39%)~7.12s7.20sp=0.572 n=6
Emit Time4.34s (± 0.74%)4.33s (± 0.54%)~4.30s4.36sp=0.418 n=6
Total Time14.89s (± 0.45%)14.90s (± 0.30%)~14.84s14.96sp=0.810 n=6
material-ui - node (v16.17.1, x64)
Memory used477,697k (± 0.01%)477,743k (± 0.01%)~477,697k477,783kp=0.128 n=6
Parse Time3.22s (± 0.32%)3.22s (± 0.72%)~3.19s3.25sp=0.624 n=6
Bind Time0.96s (± 0.57%)0.96s (± 1.22%)~0.95s0.98sp=0.859 n=6
Check Time18.08s (± 0.46%)18.13s (± 0.49%)~18.03s18.25sp=0.296 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time22.25s (± 0.38%)22.31s (± 0.43%)~22.21s22.47sp=0.296 n=6
xstate - node (v16.17.1, x64)
Memory used550,691k (± 0.02%)550,682k (± 0.01%)~550,640k550,764kp=0.936 n=6
Parse Time3.94s (± 0.41%)3.94s (± 0.41%)~3.91s3.95sp=0.627 n=6
Bind Time1.80s (± 0.57%)1.79s (± 0.68%)~1.77s1.80sp=0.210 n=6
Check Time3.00s (± 0.49%)3.02s (± 0.90%)~2.99s3.07sp=0.081 n=6
Emit Time0.09s (± 0.00%)0.09s (± 0.00%)~0.09s0.09sp=1.000 n=6
Total Time8.83s (± 0.14%)8.84s (± 0.41%)~8.78s8.89sp=0.360 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
BenchmarkNameIterations
Current511966
Baselinemain6

Developer Information:

Download Benchmark

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), it looks like the DT test run failed. Please check the log for more details.
You can check the log here.

@jakebailey

Copy link
Copy Markdown
Member

Yay npm timeouts

TypeScript Bot (@typescript-bot) run dt

@typescript-bot

TypeScript Bot (typescript-bot) commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the parallelized Definitely Typed test suite on this PR at dfe955a. You can monitor the build here.

Update: The results are in!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/51196/merge:

Something interesting changed - please have a look.

Details

microsoft/vscode

4 of 53 projects failed to build with the old tsc and were ignored

extensions/references-view/tsconfig.json

src/tsconfig.tsec.json

  • error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?
  • error TS2322: Type '(ILocalExtension | Partial<IGalleryMetadata & { isApplicationScoped: boolean; isMachineScoped: boolean; isBuiltin: boolean; isSystem: boolean; updated: boolean; preRelease: boolean; installedTimestamp: number; pinned: boolean; }> | undefined)[][]' is not assignable to type '[ILocalExtension, Partial<IGalleryMetadata & { isApplicationScoped: boolean; isMachineScoped: boolean; isBuiltin: boolean; isSystem: boolean; updated: boolean; preRelease: boolean; installedTimestamp: number; pinned: boolean; }> | undefined][]'.

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@Andarist

Copy link
Copy Markdown
ContributorAuthor

Thanks for that test run! It seems that all of the reported failures might be related to promise-likes/thenables, I will dig into this. I prepared minimal repro cases for all 3: TS playground

Comment threadsrc/compiler/checker.ts Outdated
Comment on lines +36971 to +36973
if (returnType && returnType.flags & TypeFlags.Union) {
returnType = filterType(returnType, t => checkGeneratorInstantiationAssignabilityToReturnType(t, functionFlags, /*errorNode*/ undefined));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this necessary? I think it makes sense to be more forgiving for contextual types, but if you write:

function*f(): Generator<A,B,C>|string{}

then you'll probably have other issues with assignability. I'm not sure we should filter the return type of an explicit type annotation.

Is this to support #41428? If so, I'm not sure we should try to handle a case of

function*f(): Generator<A>|AsyncGenerator<A>{}

since a normal generator is incapable of producing an async generator.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Is this to support #41428? If so, I'm not sure we should try to handle a case of

Yes, it was added in this commit: "Filter annotated return type within checkYieldExpression"

since a normal generator is incapable of producing an async generator.

That is true, I just assumed that this might be wanted to handle mixed unions. The function* is just sugar and with regular functions, we are free to assign wider return types than what is required to be covered. So, from my PoV, this was meant as a convenience - it's more about type reusability than about actually wanting to write a union like this directly as the return type annotation, usually it would come from an alias.

@Andarist
Mateusz Burzyński (Andarist)force-pushed the fix/contextual-return-type-asyncs-and-generators branch from 1b98fac to 3f1ee89CompareMay 29, 2023 11:55
thisArg?: any
): void;

registerCommand("_references-view.showHistoryItem", async (item) => { // Error, contextual return type of Promise<unknown>

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

When I created this PR, this code wasn't erroring. My initial changes created a regression with this code and it started to error (problem caught by the extended test suite). However, right now it is supposed to error as per the change here. I decided to add this as an extra regression test.

const functionFlags = getFunctionFlags(functionDecl);
if (functionFlags & FunctionFlags.Generator) {
return filterType(returnType, t => {
return !!(t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void | TypeFlags.InstantiableNonPrimitive)) || checkGeneratorInstantiationAssignabilityToReturnType(t, functionFlags, /*errorNode*/ undefined);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I'm not super fond of this check here (talking mainly about TypeFlags.InstantiableNonPrimitive check, the rest is OK).

I analyzed all of the core logic behind this change and I think it's sound. This particular check might be slightly off though. In addition to that, I think that this logic could be improved further for instantiable types but I don't know how to do it right now. I think that this PR is an improvement already though and that it could be merged without fixing it right now.

I only have a single idea to fix this: create copies of instantiable types here with the same~ filtering logic applied to their constraints (perhaps the filtering could stay deferred so we wouldn't have to compute it until requested). I'm not sure how feasible this is and if it's the right approach so I'm not even taking a stab at this right now.

The overall idea behind this part of the fix is that we want to reject contextual types that are guaranteed to not match the given function's criteria (non-promises/non-generators). We also need to keep the types that poison the type (like any/unknown/void).

We need to do this here, for example, because getContextualTypeForReturnExpression uses the result of this function (getContextualReturnType). Let's consider this example:

declarefunctionload(): Promise<boolean>;typeLoadCallback=()=>Promise<boolean>|string;constcb1: LoadCallback=async()=>load().then(m=>m);

In a case like this, we shouldn't allow string to be used as the contextual type for this return expression (of the inner arrow function). If we don't reject it soon enough then we end up with Promise<string | boolean> as the result of getContextualTypeForReturnExpression and thus we end up with an assignability problem as that's not assignable to Promise<boolean>.

Initially, I was trying to fix this by filtering in the getContextualTypeForReturnExpressionhere but I found out that filtering in getContextualReturnType gives better results for some of the other added test cases in this PR here.

…-type-asyncs-and-generators
# Conflicts:
#	tests/baselines/reference/noImplicitReturnsExclusions.errors.txt
…-type-asyncs-and-generators
# Conflicts:
#	src/compiler/checker.ts
@Andarist

Copy link
Copy Markdown
ContributorAuthor

Ron Buckton (@rbuckton) friendly 🏓

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Milestone BugPRs that fix a bug with a specific milestone

Projects

Archived in project

5 participants

@Andarist@jakebailey@typescript-bot@rbuckton@sandersn