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

Preserve original type parameter names more often when shadowed #55820

Merged
merged 6 commits into from
Nov 6, 2023

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Sep 21, 2023

Fixes #55653
Fixes #55575

This turned out to be extremely related to #49627. Our original mechanism to solve the problem for type parameters was to be very conservative and rename when we think there might be a problem.

In this PR, I extend the mechanism from #49627 to the type parameters, introducing a second symbol table and fake scope to hold them, as we can't merge the symbols that share the same name without modifying them.

This turns out to have a positive impact on some existing baselines; see 83275d2 for the actual baseline diff.

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

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 21, 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/157853/artifacts?artifactName=tgz&fileId=C5FE361F795082FBDD84BBEDFDAF0A2FD34FDA502A39BDFA74E19E895FF4773102&fileName=/typescript-5.3.0-insiders.20230921.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-55820-7".;

@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,968k (± 0.00%) 294,935k (± 0.00%) -33k (- 0.01%) 294,926k 294,947k p=0.005 n=6
Parse Time 2.62s (± 0.52%) 2.63s (± 0.32%) ~ 2.62s 2.64s p=0.666 n=6
Bind Time 0.84s (± 1.23%) 0.84s (± 1.30%) ~ 0.83s 0.85s p=0.640 n=6
Check Time 8.01s (± 0.21%) 8.00s (± 0.37%) ~ 7.97s 8.04s p=0.566 n=6
Emit Time 7.02s (± 0.18%) 7.04s (± 0.23%) ~ 7.02s 7.06s p=0.070 n=6
Total Time 18.49s (± 0.13%) 18.50s (± 0.09%) ~ 18.47s 18.52s p=0.871 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 191,606k (± 1.19%) 192,551k (± 1.56%) ~ 190,602k 196,550k p=1.000 n=6
Parse Time 1.35s (± 0.47%) 1.35s (± 0.47%) ~ 1.34s 1.36s p=1.000 n=6
Bind Time 0.73s (± 0.00%) 0.73s (± 0.00%) ~ 0.73s 0.73s p=1.000 n=6
Check Time 9.11s (± 0.70%) 9.07s (± 0.29%) ~ 9.05s 9.12s p=0.326 n=6
Emit Time 2.60s (± 0.60%) 2.60s (± 0.68%) ~ 2.58s 2.63s p=0.685 n=6
Total Time 13.79s (± 0.44%) 13.75s (± 0.26%) ~ 13.70s 13.81s p=0.244 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,206k (± 0.01%) 347,204k (± 0.00%) ~ 347,194k 347,213k p=0.936 n=6
Parse Time 2.46s (± 0.55%) 2.46s (± 0.55%) ~ 2.44s 2.48s p=0.934 n=6
Bind Time 0.94s (± 0.00%) 0.94s (± 0.86%) ~ 0.94s 0.96s p=0.405 n=6
Check Time 6.84s (± 0.39%) 6.86s (± 0.44%) ~ 6.82s 6.90s p=0.195 n=6
Emit Time 4.04s (± 0.71%) 4.04s (± 0.50%) ~ 4.01s 4.06s p=0.871 n=6
Total Time 14.28s (± 0.32%) 14.30s (± 0.21%) ~ 14.26s 14.34s p=0.261 n=6
TFS - node (v18.15.0, x64)
Memory used 302,465k (± 0.00%) 302,474k (± 0.01%) ~ 302,454k 302,494k p=0.336 n=6
Parse Time 2.01s (± 0.94%) 2.00s (± 0.93%) ~ 1.99s 2.04s p=0.249 n=6
Bind Time 1.00s (± 0.63%) 1.00s (± 0.75%) ~ 0.99s 1.01s p=0.718 n=6
Check Time 6.23s (± 0.33%) 6.25s (± 0.22%) ~ 6.23s 6.26s p=0.363 n=6
Emit Time 3.52s (± 1.00%) 3.52s (± 0.69%) ~ 3.49s 3.55s p=0.936 n=6
Total Time 12.76s (± 0.25%) 12.76s (± 0.29%) ~ 12.71s 12.81s p=0.872 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,438k (± 0.00%) 470,450k (± 0.00%) ~ 470,414k 470,476k p=0.173 n=6
Parse Time 2.58s (± 0.41%) 2.58s (± 0.76%) ~ 2.55s 2.60s p=1.000 n=6
Bind Time 0.99s (± 1.22%) 0.99s (± 0.76%) ~ 0.98s 1.00s p=0.503 n=6
Check Time 16.59s (± 0.41%) 16.57s (± 0.23%) ~ 16.52s 16.63s p=0.630 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.16s (± 0.30%) 20.14s (± 0.16%) ~ 20.11s 20.20s p=0.334 n=6
xstate - node (v18.15.0, x64)
Memory used 512,489k (± 0.01%) 512,494k (± 0.02%) ~ 512,373k 512,691k p=0.936 n=6
Parse Time 3.26s (± 0.37%) 3.27s (± 0.46%) ~ 3.24s 3.28s p=0.218 n=6
Bind Time 1.55s (± 0.33%) 1.55s (± 0.41%) ~ 1.54s 1.56s p=0.386 n=6
Check Time 2.81s (± 1.09%) 2.78s (± 0.23%) ~ 2.77s 2.79s p=0.059 n=6
Emit Time 0.08s (± 4.99%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=1.000 n=6
Total Time 7.69s (± 0.45%) 7.68s (± 0.27%) ~ 7.66s 7.71s p=0.628 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/55820/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!

@@ -5987,7 +5987,7 @@ export interface NodeLinks {
decoratorSignature?: Signature; // Signature for decorator as if invoked by the runtime.
spreadIndices?: { first: number | undefined, last: number | undefined }; // Indices of first and last spread elements in array literal
parameterInitializerContainsUndefined?: boolean; // True if this is a parameter declaration whose type annotation contains "undefined".
fakeScopeForSignatureDeclaration?: boolean; // True if this is a fake scope injected into an enclosing declaration chain.
fakeScopeForSignatureDeclaration?: "params" | "typeParams"; // If present, this is a fake scope injected into an enclosing declaration chain.
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if this should be an enum, I just used a string here as the least annoying way to get two truthy values.

@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 - bun (v1.0.2, x64)
Memory used 317,145k (± 0.26%) 316,439k (± 0.24%) ~ 314,201k 318,067k p=0.126 n=12
Parse Time 2.29s (± 0.48%) 2.29s (± 0.47%) ~ 2.27s 2.33s p=1.000 n=12
Bind Time 0.90s (± 1.69%) 0.89s (± 1.01%) ~ 0.87s 0.92s p=0.745 n=12
Check Time 7.98s (± 0.45%) 7.99s (± 0.25%) ~ 7.94s 8.04s p=0.259 n=12
Emit Time 6.43s (± 0.45%) 6.41s (± 0.30%) ~ 6.35s 6.47s p=0.307 n=12
Total Time 17.59s (± 0.30%) 17.58s (± 0.16%) ~ 17.53s 17.67s p=0.885 n=12
Compiler-Unions - bun (v1.0.2, x64)
Memory used 243,470k (± 1.02%) 245,379k (± 0.59%) ~ 239,833k 247,542k p=0.371 n=12
Parse Time 1.13s (± 1.66%) 1.13s (± 1.41%) ~ 1.11s 1.18s p=1.000 n=12
Bind Time 0.84s (± 0.67%) 0.84s (± 2.16%) ~ 0.81s 0.90s p=0.498 n=12
Check Time 8.82s (± 0.53%) 8.78s (± 0.18%) ~ 8.74s 8.82s p=0.352 n=12
Emit Time 2.78s (± 0.61%) 2.79s (± 0.35%) ~ 2.76s 2.81s p=0.320 n=12
Total Time 13.57s (± 0.37%) 13.55s (± 0.20%) ~ 13.48s 13.62s p=0.369 n=12
Monaco - bun (v1.0.2, x64)
Memory used 377,339k (± 0.21%) 377,447k (± 0.18%) ~ 375,870k 379,065k p=0.795 n=12
Parse Time 1.97s (± 0.64%) 1.97s (± 0.67%) ~ 1.95s 2.00s p=0.930 n=12
Bind Time 0.96s (± 1.48%) 0.97s (± 1.81%) ~ 0.93s 1.02s p=0.268 n=12
Check Time 7.38s (± 0.30%) 7.37s (± 0.27%) ~ 7.31s 7.41s p=0.543 n=12
Emit Time 3.78s (± 0.47%) 3.77s (± 0.70%) ~ 3.69s 3.86s p=0.211 n=12
Total Time 14.10s (± 0.18%) 14.08s (± 0.23%) ~ 14.01s 14.19s p=0.434 n=12
TFS - bun (v1.0.2, x64)
Memory used 317,938k (± 0.20%) 317,329k (± 0.17%) ~ 315,501k 318,466k p=0.157 n=12
Parse Time 1.74s (± 0.33%) 1.75s (± 0.73%) ~ 1.73s 1.80s p=0.567 n=12
Bind Time 0.99s (± 1.41%) 0.98s (± 1.88%) ~ 0.92s 1.01s p=0.930 n=12
Check Time 6.68s (± 0.42%) 6.68s (± 0.36%) ~ 6.61s 6.73s p=0.908 n=12
Emit Time 3.47s (± 0.71%) 3.47s (± 0.70%) ~ 3.42s 3.55s p=0.908 n=12
Total Time 12.88s (± 0.36%) 12.89s (± 0.31%) ~ 12.79s 12.98s p=0.622 n=12
material-ui - bun (v1.0.2, x64)
Memory used 494,317k (± 1.91%) 484,418k (± 1.60%) ~ 475,405k 510,266k p=0.078 n=12
Parse Time 2.23s (± 0.33%) 2.22s (± 0.28%) ~ 2.21s 2.24s p=0.952 n=12
Bind Time 0.74s (± 3.03%) 0.76s (± 1.96%) ~ 0.71s 0.78s p=0.201 n=12
Check Time 15.80s (± 0.40%) 15.87s (± 0.47%) ~ 15.70s 16.05s p=0.119 n=12
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=12
Total Time 18.76s (± 0.29%) 18.86s (± 0.41%) +0.10s (+ 0.52%) 18.68s 19.01s p=0.049 n=12
xstate - bun (v1.0.2, x64)
Memory used 410,202k (± 5.77%) 403,990k (± 5.55%) ~ 381,218k 463,907k p=0.707 n=12
Parse Time 3.22s (± 0.39%) 3.23s (± 0.53%) ~ 3.20s 3.28s p=0.205 n=12
Bind Time 1.26s (± 1.03%) 1.26s (± 0.63%) ~ 1.24s 1.28s p=0.814 n=12
Check Time 3.59s (± 0.28%) 3.59s (± 0.35%) ~ 3.56s 3.62s p=0.347 n=12
Emit Time 0.21s (± 1.57%) 0.21s (± 2.93%) ~ 0.20s 0.23s p=0.974 n=12
Total Time 8.28s (± 0.29%) 8.29s (± 0.20%) ~ 8.25s 8.35s p=0.224 n=12
System info unknown
Hosts
  • bun (v1.0.2, x64)
Scenarios
  • Angular - bun (v1.0.2, x64)
  • Compiler-Unions - bun (v1.0.2, x64)
  • Monaco - bun (v1.0.2, x64)
  • TFS - bun (v1.0.2, x64)
  • material-ui - bun (v1.0.2, x64)
  • xstate - bun (v1.0.2, x64)
Benchmark Name Iterations
Current pr 12
Baseline baseline 12

Startup

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
tsc-startup - bun (v1.0.2, x64)
Execution time 425.46ms (± 0.08%) 425.43ms (± 0.10%) ~ 424.08ms 433.95ms p=0.051 n=600
tsserverlibrary-startup - bun (v1.0.2, x64)
Execution time 660.84ms (± 0.12%) 662.09ms (± 0.13%) +1.25ms (+ 0.19%) 660.21ms 685.95ms p=0.000 n=600
typescript-startup - bun (v1.0.2, x64)
Execution time 659.87ms (± 0.12%) 661.14ms (± 0.13%) +1.27ms (+ 0.19%) 659.39ms 676.29ms p=0.000 n=600
System info unknown
Hosts
  • bun (v1.0.2, x64)
Scenarios
  • tsc-startup - bun (v1.0.2, x64)
  • tsserverlibrary-startup - bun (v1.0.2, x64)
  • typescript-startup - bun (v1.0.2, 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/55820/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.

@fatcerberus
Copy link

fatcerberus commented Sep 22, 2023

Preserve shadowed type parameter names

Looking at #55653, technically it's the other type that's being shadowed by the type parameter, and that one already is preserved (the type parameter is renamed). If it were the other way around then the current behavior might actually be correct 😉

@fatcerberus
Copy link

fatcerberus commented Sep 22, 2023

Here's a test, in case this isn't already covered:

class A { }
const a = new A();
const b = <A,>(x: A) => a

The type parameter still needs to be renamed here.

Also here's a fun bit of chicanery:
image

Looks like an identity function, but isn't!

@jakebailey
Copy link
Member Author

jakebailey commented Sep 22, 2023

This PR makes that work, check the declaration tab here: https://www.staging-typescript.org/play?ts=5.3.0-pr-55820-7#code/MYGwhgzhAECC0G9oF8BQwD2A7CAXaY0AvNFgKYDucAFAJQDc62e0ARsdADywA0AfNQAeALji1ifAkA

That's actually the test case I added first.

Unfortunately hover doesn't set the flag from: #55821 I don't think. I've been wondering if we should just drop this flag, so we are consistent...

@fatcerberus
Copy link

declare const b: <A>(x: A) => globalThis.A;

This is a legal declaration? I didn't know globalThis existed in type-space.

Also if the file is a module the output is... questionable. I'd rather just have the type parameter be renamed here.

export declare class A {
}
export declare const a: A;
export declare const b: <A>(x: A) => import("./input").A;

@jakebailey
Copy link
Member Author

Write:

class A { }
const a = new A();
function b<A,>(x: A) { return a; }

And you'll see that we already emit globalThis.A; the only reason we don't now is because declaration emit and signature to node emit are different.

@fatcerberus
Copy link

Hmm, you're right, thanks. That self-import in the case of a module still bugs me a lot, though.

@jakebailey jakebailey changed the title Preserve shadowed type parameter names in more cases Preserve original type parameter names more often when shadowed Sep 22, 2023


//// [declarationEmitTypeParameterNameShadowedInternally.d.ts]
export declare const foo: <T>(x: T) => <T_1>(y: T_1) => readonly [T, T_1];
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I thought this test had failed, but actually, I don't think it did fail; this matches main, I think...

>y : T

return inner;
>inner : <T>(y: T) => readonly [T, T]
Copy link
Member Author

@jakebailey jakebailey Oct 9, 2023

Choose a reason for hiding this comment

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

Here you can see that the tooltip is very wrong because we don't enable renaming in tooltips.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing new, though:
image

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

I know I've said it elsewhere - Not a huge fan of the double-scope thing (or basically rebuilding the signature's .locals table by hand in two parts), but this isn't much worse than what we're already doing, and fixing that is... another issue (ungh, the reading through this, my own PR, and the original PR adding this codepath made me aware of some problems in caching caused by this approach that we really aughta fix - we're caching results between unrelated queries). This is a meaningful improvement for now (and technically will hide the problem a bit - two cache nodes means you're slightly less likely to hit the incorrect cache key issue), so we should probably take it. Once it's in, I'll make a followup with how I'd prefer it to be structured, ignoring any perf issues that causes, and once the perf of that is made acceptable via other changes I've been looking into, we can try that out for size. This all just feels.... too complicated and bespoke, for what little it does.

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 6, 2023
@jakebailey
Copy link
Member Author

Dunno why CI isn't running...

@jakebailey jakebailey merged commit 1ed8ed6 into microsoft:main Nov 6, 2023
19 checks passed
@jakebailey jakebailey deleted the fix-55653 branch November 6, 2023 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
4 participants