Skip to content

Avoid inferring return and yield types from unreachable statements #55601

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Sep 1, 2023

closes #55437

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 1, 2023
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #55437. If you can get it accepted, this PR will have a better chance of being reviewed.

@tannerlinsley
Copy link

I needed this just the other day.

@jakebailey
Copy link
Member

@typescript-bot test top200
@typescript-bot user test this
@typescript-bot run dt
@typescript-bot perf test this
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 2, 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/157276/artifacts?artifactName=tgz&fileId=1C95677BE2D01E9CA74F671EC87A746584ABE854757C1BB2E29AC7DFBD75EF7202&fileName=/typescript-5.3.0-insiders.20230902.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.3.0-pr-55601-8".;

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user test suite comparing main and refs/pull/55601/merge:

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Unknown failure"
  • 2 instances of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 300,277k (± 0.01%) 300,284k (± 0.01%) ~ 300,263k 300,312k p=0.574 n=6
Parse Time 3.00s (± 0.14%) 3.01s (± 0.25%) ~ 3.00s 3.02s p=0.100 n=6
Bind Time 0.93s (± 0.00%) 0.93s (± 0.44%) ~ 0.92s 0.93s p=0.405 n=6
Check Time 9.31s (± 0.18%) 9.33s (± 0.52%) ~ 9.29s 9.41s p=0.869 n=6
Emit Time 7.63s (± 0.15%) 7.63s (± 0.28%) ~ 7.60s 7.66s p=0.560 n=6
Total Time 20.87s (± 0.11%) 20.89s (± 0.33%) ~ 20.83s 21.01s p=1.000 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 193,961k (± 0.01%) 193,925k (± 0.03%) ~ 193,856k 194,027k p=0.128 n=6
Parse Time 1.58s (± 0.26%) 1.58s (± 0.26%) ~ 1.57s 1.58s p=0.218 n=6
Bind Time 0.79s (± 0.69%) 0.80s (± 0.65%) ~ 0.79s 0.80s p=0.640 n=6
Check Time 9.94s (± 0.37%) 9.92s (± 0.39%) ~ 9.87s 9.96s p=0.512 n=6
Emit Time 2.74s (± 0.30%) 2.75s (± 0.38%) ~ 2.74s 2.76s p=0.101 n=6
Total Time 15.05s (± 0.26%) 15.04s (± 0.30%) ~ 14.97s 15.09s p=0.624 n=6
Monaco - node (v16.17.1, x64)
Memory used 347,181k (± 0.01%) 347,196k (± 0.00%) ~ 347,182k 347,209k p=0.568 n=6
Parse Time 2.69s (± 0.31%) 2.68s (± 0.38%) ~ 2.67s 2.70s p=0.788 n=6
Bind Time 0.99s (± 0.00%) 0.99s (± 0.00%) ~ 0.99s 0.99s p=1.000 n=6
Check Time 7.93s (± 0.42%) 7.93s (± 0.46%) ~ 7.88s 7.98s p=0.935 n=6
Emit Time 4.27s (± 0.24%) 4.27s (± 0.28%) ~ 4.25s 4.28s p=0.408 n=6
Total Time 15.87s (± 0.25%) 15.87s (± 0.23%) ~ 15.81s 15.90s p=0.871 n=6
TFS - node (v16.17.1, x64)
Memory used 301,177k (± 0.00%) 301,174k (± 0.01%) ~ 301,141k 301,190k p=0.810 n=6
Parse Time 2.18s (± 0.93%) 2.17s (± 0.79%) ~ 2.15s 2.19s p=0.285 n=6
Bind Time 1.11s (± 0.37%) 1.11s (± 0.00%) ~ 1.11s 1.11s p=0.405 n=6
Check Time 7.23s (± 0.32%) 7.22s (± 0.36%) ~ 7.20s 7.26s p=0.415 n=6
Emit Time 3.98s (± 0.52%) 3.98s (± 0.20%) ~ 3.97s 3.99s p=0.508 n=6
Total Time 14.49s (± 0.26%) 14.49s (± 0.25%) ~ 14.44s 14.54s p=0.935 n=6
material-ui - node (v16.17.1, x64)
Memory used 479,460k (± 0.00%) 479,471k (± 0.00%) ~ 479,463k 479,483k p=0.065 n=6
Parse Time 3.15s (± 0.17%) 3.15s (± 0.24%) ~ 3.14s 3.16s p=0.476 n=6
Bind Time 0.91s (± 0.00%) 0.91s (± 0.00%) ~ 0.91s 0.91s p=1.000 n=6
Check Time 17.78s (± 0.27%) 17.75s (± 0.21%) ~ 17.70s 17.79s p=0.293 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 21.84s (± 0.23%) 21.81s (± 0.18%) ~ 21.75s 21.85s p=0.373 n=6
xstate - node (v16.17.1, x64)
Memory used 542,846k (± 0.01%) 542,833k (± 0.01%) ~ 542,781k 542,925k p=0.471 n=6
Parse Time 3.69s (± 0.15%) 3.69s (± 0.14%) +0.01s (+ 0.23%) 3.69s 3.70s p=0.038 n=6
Bind Time 1.47s (± 3.58%) 1.45s (± 0.00%) ~ 1.45s 1.45s p=0.293 n=6
Check Time 3.17s (± 0.88%) 3.18s (± 0.32%) ~ 3.16s 3.19s p=0.720 n=6
Emit Time 0.08s (± 6.19%) 0.08s (± 9.79%) ~ 0.08s 0.10s p=0.752 n=6
Total Time 8.41s (± 0.74%) 8.40s (± 0.12%) ~ 8.39s 8.41s p=0.801 n=6
System info unknown
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 pr 6
Baseline baseline 6

tsserver

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-UnionsTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 2,489ms (± 0.11%) 2,487ms (± 0.18%) ~ 2,482ms 2,494ms p=0.422 n=6
Req 2 - geterr 5,948ms (± 0.35%) 5,939ms (± 0.59%) ~ 5,911ms 6,003ms p=0.378 n=6
Req 3 - references 343ms (± 0.43%) 345ms (± 1.51%) ~ 341ms 355ms p=1.000 n=6
Req 4 - navto 278ms (± 0.73%) 279ms (± 0.99%) ~ 277ms 283ms p=0.591 n=6
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) ~ 1,356 1,356 p=1.000 n=6
Req 5 - completionInfo 81ms (± 8.47%) 80ms (± 8.01%) ~ 76ms 92ms p=0.739 n=6
CompilerTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 2,634ms (± 0.68%) 2,629ms (± 0.81%) ~ 2,603ms 2,661ms p=0.521 n=6
Req 2 - geterr 4,771ms (± 0.14%) 4,777ms (± 0.24%) ~ 4,767ms 4,793ms p=0.422 n=6
Req 3 - references 351ms (± 0.23%) 351ms (± 0.16%) ~ 350ms 351ms p=0.859 n=6
Req 4 - navto 270ms (± 0.20%) 271ms (± 0.98%) ~ 269ms 276ms p=0.476 n=6
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) ~ 1,518 1,518 p=1.000 n=6
Req 5 - completionInfo 78ms (± 3.27%) 79ms (± 0.52%) ~ 79ms 80ms p=0.673 n=6
xstateTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 2,712ms (± 0.19%) 2,706ms (± 0.24%) ~ 2,699ms 2,718ms p=0.109 n=6
Req 2 - geterr 1,937ms (± 2.44%) 1,960ms (± 1.85%) ~ 1,888ms 1,986ms p=0.471 n=6
Req 3 - references 138ms (± 2.44%) 140ms (± 1.46%) ~ 136ms 142ms p=0.412 n=6
Req 4 - navto 353ms (± 0.34%) 352ms (± 0.29%) ~ 351ms 354ms p=0.180 n=6
Req 5 - completionInfo count 2,071 (± 0.00%) 2,071 (± 0.00%) ~ 2,071 2,071 p=1.000 n=6
Req 5 - completionInfo 321ms (± 1.75%) 323ms (± 1.51%) ~ 313ms 326ms p=0.870 n=6
System info unknown
Hosts
  • node (v16.17.1, x64)
Scenarios
  • CompilerTSServer - node (v16.17.1, x64)
  • Compiler-UnionsTSServer - node (v16.17.1, x64)
  • xstateTSServer - node (v16.17.1, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Startup

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
tsc-startup - node (v16.17.1, x64)
Execution time 156.27ms (± 0.17%) 156.26ms (± 0.17%) ~ 154.34ms 159.97ms p=0.579 n=600
tsserver-startup - node (v16.17.1, x64)
Execution time 230.31ms (± 0.14%) 230.97ms (± 0.14%) +0.65ms (+ 0.28%) 229.37ms 237.98ms p=0.000 n=600
tsserverlibrary-startup - node (v16.17.1, x64)
Execution time 235.15ms (± 0.14%) 236.12ms (± 0.17%) +0.97ms (+ 0.41%) 234.53ms 244.92ms p=0.000 n=600
typescript-startup - node (v16.17.1, x64)
Execution time 236.52ms (± 0.13%) 236.53ms (± 0.16%) ~ 234.86ms 247.37ms p=0.702 n=600
System info unknown
Hosts
  • node (v16.17.1, x64)
Scenarios
  • tsc-startup - node (v16.17.1, x64)
  • tsserver-startup - node (v16.17.1, x64)
  • tsserverlibrary-startup - node (v16.17.1, x64)
  • typescript-startup - node (v16.17.1, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top-repos suite comparing main and refs/pull/55601/merge:

Everything looks good!

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

@DanielRosenwasser
Copy link
Member

This doesn't really work for assertion functions right?

Can you add a few tests around that?

function throws(): never {
    throw new Error();
}

export function foo() {
    throws();
    return 42;
}

export function bar() {
    var x;
    x = 1;
    if (Math.random()) {
        throws();
        return x;
    }
    x = 2;
    return x;
}

@Andarist
Copy link
Contributor Author

Andarist commented Sep 7, 2023

Should I just codify the current results in the test suite or would you expect those cases to work differently? It would be possible to achieve different results for them, right? I'm not sure if that's totally desirable from your PoV though.

@@ -36286,6 +36286,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const nextTypes: Type[] = [];
const isAsync = (getFunctionFlags(func) & FunctionFlags.Async) !== 0;
forEachYieldExpression(func.body as Block, yieldExpression => {
const statement = findAncestor(yieldExpression, isStatement)!;
if (canHaveFlowNode(statement) && !statement.flowNode && !compilerOptions.allowUnreachableCode) {
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiousity, why flag this on allowUnreachableCode? My impression is that allowUnreachableCode is more like a lint rule that errors on the unreachable code, and not something that should impact type analysis.

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 honestly have no idea cause I've written it half a year ago 😅

If the tests would come roughly unchanged without this condition (which is likely) then maybe I just have tried to be conservative when it comes to the impact of this change. Taking a look at how allowUnreachableCode is defined in the docs now - you are right that this flag should mostly just affect error reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Waiting on reviewers
Development

Successfully merging this pull request may close these issues.

Unreachable returns badly break return type inference
6 participants