Skip to content

Disallow instantiation expressions on the right side of instanceof #53323

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

Merged

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Mar 17, 2023

In reviewing #49863, we noticed that we don't emit any sort of error when you use an instantiation expression on the right side of an instanceof check. E.g. in the below code, <number> does nothing; you'll still get any.

declare class Box<T> {
    value: T;
}

declare const maybeBox: unknown;

if (maybeBox instanceof Box<number>) {
    maybeBox;                     // Still Box<any>!
    const value = maybeBox.value; // Still any!
}

Playground Link

This PR disallows this syntactically.

We could try and figure out how to do:

const BoxOfNumber = Box<number>;
if (maybeBox instanceof BoxOfNumber) {
    // ...
}

And that fix would look different.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 17, 2023
@jakebailey
Copy link
Member Author

@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 17, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 17, 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/149831/artifacts?artifactName=tgz&fileId=40D5A8B8FC621449725D3AD2D13A3D623F46F5FB7CFE443F9B419D60A259FEBB02&fileName=/typescript-5.1.0-insiders.20230317.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-53323-7".;

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

~~~~~~~~~~~
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.

Box<number> instanceof Object; // OK
Copy link
Member Author

Choose a reason for hiding this comment

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

I am checking only the right side here, but, should these even be legal? They seem useless in another way.

Copy link
Member

Choose a reason for hiding this comment

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

Is the instantiation expression pointless? Yes. Is it harmful? No. Is it misleading? Probably not really on the LHS, since the only reason I can think of why you'd want one on the LHS is to "satisfy" the RHS, but the RHS forbids them now, soooo...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I don't really care either way. I guess this PR is fine as-is.

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..53323

Metric main 53323 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 360,905k (± 0.01%) 360,935k (± 0.01%) ~ 360,886k 360,966k p=0.108 n=6
Parse Time 3.53s (± 0.85%) 3.51s (± 0.69%) ~ 3.48s 3.54s p=0.517 n=6
Bind Time 1.19s (± 0.75%) 1.19s (± 0.69%) ~ 1.18s 1.20s p=0.550 n=6
Check Time 9.46s (± 0.73%) 9.49s (± 0.34%) ~ 9.45s 9.54s p=0.630 n=6
Emit Time 7.95s (± 1.06%) 7.90s (± 0.53%) ~ 7.86s 7.97s p=0.296 n=6
Total Time 22.13s (± 0.76%) 22.09s (± 0.24%) ~ 21.99s 22.14s p=0.873 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 193,116k (± 0.69%) 193,130k (± 0.70%) ~ 192,437k 195,882k p=0.810 n=6
Parse Time 1.58s (± 1.37%) 1.57s (± 1.36%) ~ 1.55s 1.60s p=0.219 n=6
Bind Time 0.82s (± 0.50%) 0.82s (± 0.50%) ~ 0.82s 0.83s p=1.000 n=6
Check Time 10.13s (± 0.53%) 10.06s (± 0.39%) ~ 10.01s 10.10s p=0.108 n=6
Emit Time 2.99s (± 0.40%) 3.00s (± 0.68%) ~ 2.98s 3.02s p=0.738 n=6
Total Time 15.53s (± 0.45%) 15.45s (± 0.39%) ~ 15.37s 15.53s p=0.065 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,438k (± 0.01%) 345,444k (± 0.01%) ~ 345,414k 345,462k p=0.378 n=6
Parse Time 2.73s (± 0.64%) 2.73s (± 0.36%) ~ 2.72s 2.74s p=0.677 n=6
Bind Time 1.09s (± 0.47%) 1.09s (± 0.69%) ~ 1.08s 1.10s p=0.784 n=6
Check Time 7.68s (± 0.55%) 7.68s (± 0.37%) ~ 7.63s 7.71s p=1.000 n=6
Emit Time 4.46s (± 0.49%) 4.44s (± 0.68%) ~ 4.41s 4.49s p=0.326 n=6
Total Time 15.95s (± 0.48%) 15.95s (± 0.32%) ~ 15.89s 16.01s p=1.000 n=6
TFS - node (v16.17.1, x64)
Memory used 299,800k (± 0.01%) 299,795k (± 0.01%) ~ 299,764k 299,835k p=0.810 n=6
Parse Time 2.17s (± 0.35%) 2.17s (± 0.71%) ~ 2.15s 2.19s p=0.934 n=6
Bind Time 1.24s (± 1.11%) 1.24s (± 1.21%) ~ 1.22s 1.26s p=0.315 n=6
Check Time 7.15s (± 0.37%) 7.14s (± 0.40%) ~ 7.10s 7.18s p=0.686 n=6
Emit Time 4.34s (± 0.91%) 4.33s (± 0.90%) ~ 4.29s 4.40s p=0.374 n=6
Total Time 14.90s (± 0.16%) 14.88s (± 0.45%) ~ 14.78s 14.96s p=0.630 n=6
material-ui - node (v16.17.1, x64)
Memory used 477,442k (± 0.01%) 477,455k (± 0.02%) ~ 477,353k 477,582k p=1.000 n=6
Parse Time 3.22s (± 0.80%) 3.22s (± 0.48%) ~ 3.19s 3.23s p=0.808 n=6
Bind Time 0.96s (± 1.43%) 0.95s (± 0.88%) ~ 0.95s 0.97s p=0.931 n=6
Check Time 17.97s (± 0.33%) 18.01s (± 0.50%) ~ 17.92s 18.13s p=0.628 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.15s (± 0.17%) 22.19s (± 0.41%) ~ 22.11s 22.32s p=0.809 n=6
xstate - node (v16.17.1, x64)
Memory used 550,757k (± 0.02%) 550,794k (± 0.03%) ~ 550,647k 551,028k p=0.521 n=6
Parse Time 3.94s (± 0.26%) 3.94s (± 0.30%) ~ 3.92s 3.95s p=0.806 n=6
Bind Time 1.80s (± 0.97%) 1.81s (± 0.46%) ~ 1.79s 1.81s p=0.402 n=6
Check Time 3.04s (± 0.80%) 3.04s (± 0.54%) ~ 3.02s 3.06s 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.87s (± 0.35%) 8.88s (± 0.20%) ~ 8.86s 8.90s p=0.685 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 53323 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

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

@jakebailey jakebailey merged commit b7b0b52 into microsoft:main Mar 17, 2023
@jakebailey jakebailey deleted the instanceof-instantiation-expression branch March 17, 2023 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants