Skip to content
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

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

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Oct 17, 2022

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

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Oct 17, 2022
Comment on lines 27190 to 27192
if (functionFlags & FunctionFlags.Async) {
return !!getAwaitedTypeOfPromise(returnType);
}
Copy link
Contributor Author

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

return checkGeneratorInstantiationAssignabilityToReturnType(returnType, functionFlags, /*errorNode*/ undefined);
}
if (functionFlags & FunctionFlags.Async) {
return !!getAwaitedTypeOfPromise(returnType);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -5,7 +5,7 @@ type OrGen = () => (number | Generator<string, (arg: number) => void, undefined>

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
Contributor Author

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
Contributor Author

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

@jakebailey
Copy link
Member

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

@typescript-bot test this
@typescript-bot test top100
@typescript-bot user test this
@typescript-bot run dt
@typescript-bot perf test this faster
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

Heya @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
Collaborator

typescript-bot commented Mar 15, 2023

Heya @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
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

Hey @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
Contributor Author

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

I'll take it, thanks!

@typescript-bot
Copy link
Collaborator

@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
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..51196

Metric main 51196 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 361,458k (± 0.01%) 361,441k (± 0.01%) ~ 361,393k 361,472k p=0.298 n=6
Parse Time 3.51s (± 0.58%) 3.53s (± 0.47%) ~ 3.51s 3.55s p=0.084 n=6
Bind Time 1.18s (± 0.69%) 1.18s (± 0.35%) ~ 1.18s 1.19s p=0.206 n=6
Check Time 9.46s (± 0.54%) 9.45s (± 0.40%) ~ 9.40s 9.49s p=0.870 n=6
Emit Time 7.91s (± 0.80%) 7.91s (± 0.41%) ~ 7.86s 7.94s p=0.517 n=6
Total Time 22.05s (± 0.28%) 22.07s (± 0.34%) ~ 21.96s 22.15s p=0.376 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 193,215k (± 0.76%) 193,124k (± 0.70%) ~ 192,510k 195,888k p=0.230 n=6
Parse Time 1.57s (± 1.25%) 1.58s (± 1.25%) ~ 1.54s 1.60s p=0.514 n=6
Bind Time 0.82s (± 1.25%) 0.82s (± 0.50%) ~ 0.81s 0.82s p=0.390 n=6
Check Time 10.07s (± 0.39%) 10.02s (± 0.39%) ~ 9.96s 10.07s p=0.063 n=6
Emit Time 2.99s (± 0.52%) 2.99s (± 1.08%) ~ 2.94s 3.02s p=0.461 n=6
Total Time 15.44s (± 0.26%) 15.40s (± 0.36%) ~ 15.32s 15.46s p=0.172 n=6
Monaco - node (v16.17.1, x64)
Memory used 346,709k (± 0.00%) 346,704k (± 0.00%) ~ 346,691k 346,719k p=0.810 n=6
Parse Time 2.72s (± 0.49%) 2.73s (± 0.44%) ~ 2.72s 2.75s p=0.101 n=6
Bind Time 1.09s (± 0.47%) 1.09s (± 0.37%) ~ 1.09s 1.10s p=0.114 n=6
Check Time 7.74s (± 0.28%) 7.71s (± 0.29%) ~ 7.69s 7.75s p=0.107 n=6
Emit Time 4.46s (± 0.52%) 4.47s (± 0.59%) ~ 4.43s 4.51s p=0.405 n=6
Total Time 15.99s (± 0.31%) 16.00s (± 0.17%) ~ 15.98s 16.05s p=0.257 n=6
TFS - node (v16.17.1, x64)
Memory used 300,126k (± 0.01%) 300,131k (± 0.01%) ~ 300,097k 300,192k p=0.688 n=6
Parse Time 2.17s (± 0.69%) 2.17s (± 0.38%) ~ 2.16s 2.18s p=0.738 n=6
Bind Time 1.23s (± 0.80%) 1.24s (± 0.68%) ~ 1.22s 1.24s p=0.256 n=6
Check Time 7.16s (± 0.29%) 7.17s (± 0.39%) ~ 7.12s 7.20s p=0.572 n=6
Emit Time 4.34s (± 0.74%) 4.33s (± 0.54%) ~ 4.30s 4.36s p=0.418 n=6
Total Time 14.89s (± 0.45%) 14.90s (± 0.30%) ~ 14.84s 14.96s p=0.810 n=6
material-ui - node (v16.17.1, x64)
Memory used 477,697k (± 0.01%) 477,743k (± 0.01%) ~ 477,697k 477,783k p=0.128 n=6
Parse Time 3.22s (± 0.32%) 3.22s (± 0.72%) ~ 3.19s 3.25s p=0.624 n=6
Bind Time 0.96s (± 0.57%) 0.96s (± 1.22%) ~ 0.95s 0.98s p=0.859 n=6
Check Time 18.08s (± 0.46%) 18.13s (± 0.49%) ~ 18.03s 18.25s p=0.296 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.25s (± 0.38%) 22.31s (± 0.43%) ~ 22.21s 22.47s p=0.296 n=6
xstate - node (v16.17.1, x64)
Memory used 550,691k (± 0.02%) 550,682k (± 0.01%) ~ 550,640k 550,764k p=0.936 n=6
Parse Time 3.94s (± 0.41%) 3.94s (± 0.41%) ~ 3.91s 3.95s p=0.627 n=6
Bind Time 1.80s (± 0.57%) 1.79s (± 0.68%) ~ 1.77s 1.80s p=0.210 n=6
Check Time 3.00s (± 0.49%) 3.02s (± 0.90%) ~ 2.99s 3.07s p=0.081 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.83s (± 0.14%) 8.84s (± 0.41%) ~ 8.78s 8.89s p=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)
Benchmark Name Iterations
Current 51196 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

Hey @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
Member

Yay npm timeouts

@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 15, 2023

Heya @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
Collaborator

@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
Collaborator

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

@Andarist
Copy link
Contributor Author

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

src/compiler/checker.ts Outdated Show resolved Hide resolved
Comment on lines 36971 to 36973
if (returnType && returnType.flags & TypeFlags.Union) {
returnType = filterType(returnType, t => checkGeneratorInstantiationAssignabilityToReturnType(t, functionFlags, /*errorNode*/ undefined));
}
Copy link
Member

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
Contributor Author

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 Andarist force-pushed the fix/contextual-return-type-asyncs-and-generators branch from 1b98fac to 3f1ee89 Compare May 29, 2023 11:55
thisArg?: any
): void;

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

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
Contributor Author

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:

declare function load(): Promise<boolean>;
type LoadCallback = () => Promise<boolean> | string;
const cb1: 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 getContextualTypeForReturnExpression here but I found out that filtering in getContextualReturnType gives better results for some of the other added test cases in this PR here.

@Andarist Andarist requested a review from rbuckton May 29, 2023 12:14
Andarist added 2 commits June 13, 2023 13:13
…-type-asyncs-and-generators

# Conflicts:
#	tests/baselines/reference/noImplicitReturnsExclusions.errors.txt
…-type-asyncs-and-generators

# Conflicts:
#	src/compiler/checker.ts
@Andarist
Copy link
Contributor Author

@rbuckton friendly 🏓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment