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

Narrow by comparisons to boolean literals #53714

Merged
merged 7 commits into from
Sep 19, 2023

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Apr 9, 2023

fixes #31105
closes #44366

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 9, 2023
@Andarist Andarist force-pushed the narrow-by-boolean-comparison branch from e9a78f2 to 2540cc4 Compare April 9, 2023 21:57
@Andarist Andarist marked this pull request as draft April 9, 2023 21:59
@Andarist Andarist marked this pull request as ready for review April 10, 2023 07:24
@jakebailey
Copy link
Member

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

typescript-bot commented Apr 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot
Copy link
Collaborator

Heya @jakebailey, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here.

@jakebailey
Copy link
Member

@typescript-bot pack this

Need to minimize an RWC change.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 2023

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

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..53714

Metric main 53714 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 365,132k (± 0.01%) 365,184k (± 0.01%) +52k (+ 0.01%) 365,144k 365,237k p=0.013 n=6
Parse Time 3.53s (± 0.55%) 3.55s (± 0.65%) ~ 3.53s 3.59s p=0.183 n=6
Bind Time 1.18s (± 0.44%) 1.18s (± 0.44%) ~ 1.17s 1.18s p=1.000 n=6
Check Time 9.51s (± 0.53%) 9.53s (± 0.44%) ~ 9.46s 9.58s p=0.521 n=6
Emit Time 7.89s (± 0.59%) 7.96s (± 0.45%) +0.06s (+ 0.78%) 7.90s 8.00s p=0.045 n=6
Total Time 22.10s (± 0.28%) 22.21s (± 0.30%) +0.11s (+ 0.51%) 22.14s 22.32s p=0.030 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 193,202k (± 0.75%) 192,640k (± 0.04%) ~ 192,563k 192,717k p=0.748 n=6
Parse Time 1.58s (± 1.24%) 1.59s (± 1.37%) ~ 1.55s 1.61s p=0.935 n=6
Bind Time 0.82s (± 0.50%) 0.82s (± 0.77%) ~ 0.81s 0.83s p=0.673 n=6
Check Time 10.17s (± 0.50%) 10.23s (± 0.32%) +0.06s (+ 0.62%) 10.20s 10.27s p=0.043 n=6
Emit Time 2.97s (± 0.58%) 2.98s (± 0.93%) ~ 2.95s 3.03s p=1.000 n=6
Total Time 15.54s (± 0.27%) 15.62s (± 0.26%) +0.07s (+ 0.47%) 15.56s 15.67s p=0.030 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,768k (± 0.00%) 345,797k (± 0.02%) ~ 345,741k 345,883k p=0.936 n=6
Parse Time 2.72s (± 0.38%) 2.74s (± 1.12%) ~ 2.71s 2.79s p=0.167 n=6
Bind Time 1.09s (± 0.82%) 1.08s (± 1.31%) ~ 1.06s 1.10s p=0.215 n=6
Check Time 7.81s (± 0.45%) 7.85s (± 0.74%) ~ 7.79s 7.93s p=0.296 n=6
Emit Time 4.44s (± 0.66%) 4.44s (± 0.42%) ~ 4.42s 4.46s p=0.805 n=6
Total Time 16.05s (± 0.38%) 16.11s (± 0.44%) ~ 16.03s 16.21s p=0.172 n=6
TFS - node (v16.17.1, x64)
Memory used 300,054k (± 0.01%) 300,065k (± 0.01%) ~ 300,030k 300,106k p=0.689 n=6
Parse Time 2.16s (± 0.74%) 2.16s (± 0.70%) ~ 2.14s 2.18s p=0.801 n=6
Bind Time 1.25s (± 1.31%) 1.24s (± 0.79%) ~ 1.23s 1.25s p=0.315 n=6
Check Time 7.21s (± 0.68%) 7.20s (± 0.83%) ~ 7.14s 7.30s p=0.810 n=6
Emit Time 4.36s (± 1.46%) 4.37s (± 0.43%) ~ 4.34s 4.39s p=1.000 n=6
Total Time 14.97s (± 0.77%) 14.97s (± 0.46%) ~ 14.90s 15.09s p=0.873 n=6
material-ui - node (v16.17.1, x64)
Memory used 481,550k (± 0.01%) 481,587k (± 0.01%) ~ 481,526k 481,672k p=0.298 n=6
Parse Time 3.26s (± 0.61%) 3.27s (± 0.32%) ~ 3.25s 3.28s p=0.217 n=6
Bind Time 0.95s (± 0.67%) 0.95s (± 0.43%) ~ 0.95s 0.96s p=0.673 n=6
Check Time 17.97s (± 0.68%) 17.95s (± 0.96%) ~ 17.73s 18.13s p=1.000 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.17s (± 0.63%) 22.17s (± 0.81%) ~ 21.94s 22.37s p=1.000 n=6
xstate - node (v16.17.1, x64)
Memory used 561,294k (± 0.02%) 561,267k (± 0.01%) ~ 561,170k 561,375k p=0.873 n=6
Parse Time 4.04s (± 0.41%) 4.04s (± 0.29%) ~ 4.03s 4.06s p=0.505 n=6
Bind Time 1.77s (± 0.43%) 1.77s (± 0.83%) ~ 1.75s 1.79s p=0.620 n=6
Check Time 3.03s (± 0.54%) 3.04s (± 0.80%) ~ 3.01s 3.08s p=1.000 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.94s (± 0.35%) 8.94s (± 0.40%) ~ 8.89s 8.99s p=1.000 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 53714 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 18, 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/152856/artifacts?artifactName=tgz&fileId=0CA0874DAC414F5BD2CB543A581DEBCA7868D6A661330E372365F1ADAE5E16CC02&fileName=/typescript-5.1.0-insiders.20230418.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-53714-11".;

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top-repos suite comparing main and refs/pull/53714/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.

@Andarist
Copy link
Contributor Author

@jakebailey I'd consider this an improvement because we get an error today here:

 export function isComputed(value: any, property?: string): boolean {
     if (value === null || value === undefined) return false
     if (property !== undefined) {
-        if (isObservableObject(value) === false) return false
+        if (!isObservableObject(value)) return false
         if (!value.$mobx.values[property]) return false
         return true
     }
     return true
 }

@@ -26992,6 +26994,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return type;
}

function narrowTypeByBooleanComparison(type: Type, expr: Expression, bool: BooleanLiteral, operator: BinaryOperator, assumeTrue: boolean): Type {
assumeTrue = (assumeTrue !== (bool.kind === SyntaxKind.TrueKeyword)) !== (operator !== SyntaxKind.ExclamationEqualsEqualsToken && operator !== SyntaxKind.ExclamationEqualsToken);
return narrowType(type, expr, assumeTrue);
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 probably should take another look at this and conditionally use narrowTypeByEquality or something like that. Gonna investigate this 🔜

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 don't have to do that since this is already handled by the branch that comes earlier in the code.

@typescript-bot
Copy link
Collaborator

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

@Andarist
Copy link
Contributor Author

I came back to this after a while and I think that this PR is in good shape and can be reviewed.

There are potentially some issues here related to coercion but this isn't a new problem:

function test(a: string | number | boolean) {
  if (a == false) {
    a; // actual: false, expected: "" | 0 | false
  }
}

So I don't think that those should block this PR.

@@ -27610,6 +27612,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return type;
}

function narrowTypeByBooleanComparison(type: Type, expr: Expression, bool: BooleanLiteral, operator: BinaryOperator, assumeTrue: boolean): Type {
assumeTrue = (assumeTrue !== (bool.kind === SyntaxKind.TrueKeyword)) !== (operator !== SyntaxKind.ExclamationEqualsEqualsToken && operator !== SyntaxKind.ExclamationEqualsToken);
Copy link
Member

Choose a reason for hiding this comment

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

I think my only comment on this bit at this point is just how unreadable this expression is; I honestly have no idea what I'm looking at here. Is there a more obvious way to represent this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Iirc this is a double xor with booleans

Copy link
Member

Choose a reason for hiding this comment

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

That makes me feel slightly better, but this is still like 3 nested equalities...

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 don't disagree 😅 at first I started with some nested if statements - and believe me... it didn't look good either. Then I realized that what I writing there was really just a xor so I managed to fold it all into this one line. It didn't look that great to me either. I think it's a very nice way of doing this but the readability suffers here if one doesnt know what this is. I tried to give it some names but I don't have any good ones so I gave up. The best alternative that I can offer would be smth like:

const a = assumeTrue;
const b = bool.kind === SyntaxKind.TrueKeyword;
const c = operator !== SyntaxKind.ExclamationEqualsEqualsToken && operator !== SyntaxKind.ExclamationEqualsToken

// boolean xors
assumeTrue = (a !== b) !== c

@jakebailey
Copy link
Member

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 2023

Heya @jakebailey, I've started to run the tsc-only perf test suite on this PR at 9568e9e. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 2023

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

Update: The results are in!

@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 (v18.15.0, x64)
Memory used 294,934k (± 0.02%) 294,925k (± 0.01%) ~ 294,896k 294,965k p=0.936 n=6
Parse Time 2.63s (± 0.32%) 2.64s (± 0.32%) ~ 2.62s 2.64s p=0.796 n=6
Bind Time 0.84s (± 1.43%) 0.84s (± 0.97%) ~ 0.83s 0.85s p=1.000 n=6
Check Time 8.00s (± 0.38%) 8.01s (± 0.29%) ~ 7.97s 8.04s p=0.627 n=6
Emit Time 7.00s (± 0.21%) 7.02s (± 0.17%) ~ 7.00s 7.03s p=0.102 n=6
Total Time 18.48s (± 0.22%) 18.50s (± 0.16%) ~ 18.45s 18.53s p=0.259 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 192,562k (± 1.53%) 192,008k (± 1.22%) ~ 190,603k 196,366k p=0.748 n=6
Parse Time 1.36s (± 0.80%) 1.35s (± 0.56%) ~ 1.34s 1.36s p=0.195 n=6
Bind Time 0.74s (± 0.00%) 0.73s (± 0.00%) -0.01s (- 1.35%) 0.73s 0.73s p=0.001 n=6
Check Time 9.07s (± 0.74%) 9.09s (± 0.31%) ~ 9.05s 9.13s p=0.936 n=6
Emit Time 2.60s (± 0.72%) 2.60s (± 0.29%) ~ 2.59s 2.61s p=0.565 n=6
Total Time 13.77s (± 0.59%) 13.77s (± 0.24%) ~ 13.73s 13.82s p=0.422 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,178k (± 0.01%) 347,163k (± 0.00%) ~ 347,137k 347,176k p=0.575 n=6
Parse Time 2.47s (± 0.51%) 2.46s (± 0.48%) ~ 2.44s 2.47s p=0.140 n=6
Bind Time 0.93s (± 0.59%) 0.93s (± 0.59%) ~ 0.93s 0.94s p=1.000 n=6
Check Time 6.88s (± 0.41%) 6.88s (± 0.62%) ~ 6.83s 6.96s p=1.000 n=6
Emit Time 4.03s (± 0.30%) 4.05s (± 0.56%) ~ 4.02s 4.09s p=0.155 n=6
Total Time 14.32s (± 0.22%) 14.32s (± 0.48%) ~ 14.24s 14.45s p=0.872 n=6
TFS - node (v18.15.0, x64)
Memory used 302,448k (± 0.00%) 302,471k (± 0.00%) +23k (+ 0.01%) 302,454k 302,484k p=0.030 n=6
Parse Time 2.00s (± 0.93%) 1.98s (± 1.13%) ~ 1.96s 2.01s p=0.122 n=6
Bind Time 1.00s (± 0.98%) 1.00s (± 0.89%) ~ 0.99s 1.01s p=0.798 n=6
Check Time 6.23s (± 0.26%) 6.25s (± 0.39%) ~ 6.22s 6.28s p=0.089 n=6
Emit Time 3.52s (± 0.63%) 3.53s (± 1.00%) ~ 3.49s 3.58s p=0.622 n=6
Total Time 12.74s (± 0.14%) 12.77s (± 0.38%) ~ 12.72s 12.83s p=0.746 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,437k (± 0.01%) 470,425k (± 0.01%) ~ 470,387k 470,453k p=0.298 n=6
Parse Time 2.58s (± 0.29%) 2.58s (± 0.53%) ~ 2.56s 2.60s p=0.672 n=6
Bind Time 1.00s (± 1.21%) 1.00s (± 0.63%) ~ 0.99s 1.01s p=0.673 n=6
Check Time 16.56s (± 0.69%) 16.62s (± 0.56%) ~ 16.50s 16.76s p=0.521 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.14s (± 0.55%) 20.20s (± 0.46%) ~ 20.07s 20.34s p=0.470 n=6
xstate - node (v18.15.0, x64)
Memory used 512,231k (± 0.01%) 512,242k (± 0.01%) ~ 512,174k 512,345k p=0.936 n=6
Parse Time 3.26s (± 0.17%) 3.25s (± 0.41%) ~ 3.23s 3.26s p=0.490 n=6
Bind Time 1.55s (± 0.53%) 1.55s (± 0.48%) ~ 1.54s 1.56s p=0.306 n=6
Check Time 2.79s (± 0.37%) 2.76s (± 0.90%) -0.03s (- 1.13%) 2.73s 2.79s p=0.029 n=6
Emit Time 0.08s (± 0.00%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=0.405 n=6
Total Time 7.67s (± 0.27%) 7.65s (± 0.40%) ~ 7.60s 7.68s p=0.086 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, 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 user test suite comparing main and refs/pull/53714/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

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

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@jakebailey
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 18, 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/157794/artifacts?artifactName=tgz&fileId=96CA87E3A6470055357EFF85F9421F680D00B55523461C94BC42B59A13C4D52E02&fileName=/typescript-5.3.0-insiders.20230918.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-53714-29".;

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

Inspecting Playground Link (as it's a little easier to read than the baselines), it seems like things are correct.

@RyanCavanaugh @DanielRosenwasser is this something we want for sure?

@Andarist
Copy link
Contributor Author

I think that Ryan confirmed in March that this is smth that the team would accept PR for (see here)

@jakebailey
Copy link
Member

Ah, yeah, you're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

typeguards only narrow types when used with unary not Strict equals false type guard doesn't work
4 participants