Skip to content

Preserve original type parameter names more often when shadowed - #55820

Merged
Jake Bailey (jakebailey) merged 6 commits into
microsoft:mainfrom
jakebailey:fix-55653
Nov 6, 2023
Merged

Preserve original type parameter names more often when shadowed#55820
Jake Bailey (jakebailey) merged 6 commits into
microsoft:mainfrom
jakebailey:fix-55653

Conversation

@jakebailey

@jakebaileyJake Bailey (jakebailey) commented Sep 21, 2023

Copy link
Copy Markdown
Member

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.

@jakebailey

Copy link
Copy Markdown
MemberAuthor

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@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

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@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

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@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

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@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

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

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

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@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

TypeScript Bot (typescript-bot) commented Sep 21, 2023

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@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
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Compiler

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Angular - node (v18.15.0, x64)
Memory used294,968k (± 0.00%)294,935k (± 0.00%)-33k (- 0.01%)294,926k294,947kp=0.005 n=6
Parse Time2.62s (± 0.52%)2.63s (± 0.32%)~2.62s2.64sp=0.666 n=6
Bind Time0.84s (± 1.23%)0.84s (± 1.30%)~0.83s0.85sp=0.640 n=6
Check Time8.01s (± 0.21%)8.00s (± 0.37%)~7.97s8.04sp=0.566 n=6
Emit Time7.02s (± 0.18%)7.04s (± 0.23%)~7.02s7.06sp=0.070 n=6
Total Time18.49s (± 0.13%)18.50s (± 0.09%)~18.47s18.52sp=0.871 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used191,606k (± 1.19%)192,551k (± 1.56%)~190,602k196,550kp=1.000 n=6
Parse Time1.35s (± 0.47%)1.35s (± 0.47%)~1.34s1.36sp=1.000 n=6
Bind Time0.73s (± 0.00%)0.73s (± 0.00%)~0.73s0.73sp=1.000 n=6
Check Time9.11s (± 0.70%)9.07s (± 0.29%)~9.05s9.12sp=0.326 n=6
Emit Time2.60s (± 0.60%)2.60s (± 0.68%)~2.58s2.63sp=0.685 n=6
Total Time13.79s (± 0.44%)13.75s (± 0.26%)~13.70s13.81sp=0.244 n=6
Monaco - node (v18.15.0, x64)
Memory used347,206k (± 0.01%)347,204k (± 0.00%)~347,194k347,213kp=0.936 n=6
Parse Time2.46s (± 0.55%)2.46s (± 0.55%)~2.44s2.48sp=0.934 n=6
Bind Time0.94s (± 0.00%)0.94s (± 0.86%)~0.94s0.96sp=0.405 n=6
Check Time6.84s (± 0.39%)6.86s (± 0.44%)~6.82s6.90sp=0.195 n=6
Emit Time4.04s (± 0.71%)4.04s (± 0.50%)~4.01s4.06sp=0.871 n=6
Total Time14.28s (± 0.32%)14.30s (± 0.21%)~14.26s14.34sp=0.261 n=6
TFS - node (v18.15.0, x64)
Memory used302,465k (± 0.00%)302,474k (± 0.01%)~302,454k302,494kp=0.336 n=6
Parse Time2.01s (± 0.94%)2.00s (± 0.93%)~1.99s2.04sp=0.249 n=6
Bind Time1.00s (± 0.63%)1.00s (± 0.75%)~0.99s1.01sp=0.718 n=6
Check Time6.23s (± 0.33%)6.25s (± 0.22%)~6.23s6.26sp=0.363 n=6
Emit Time3.52s (± 1.00%)3.52s (± 0.69%)~3.49s3.55sp=0.936 n=6
Total Time12.76s (± 0.25%)12.76s (± 0.29%)~12.71s12.81sp=0.872 n=6
material-ui - node (v18.15.0, x64)
Memory used470,438k (± 0.00%)470,450k (± 0.00%)~470,414k470,476kp=0.173 n=6
Parse Time2.58s (± 0.41%)2.58s (± 0.76%)~2.55s2.60sp=1.000 n=6
Bind Time0.99s (± 1.22%)0.99s (± 0.76%)~0.98s1.00sp=0.503 n=6
Check Time16.59s (± 0.41%)16.57s (± 0.23%)~16.52s16.63sp=0.630 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time20.16s (± 0.30%)20.14s (± 0.16%)~20.11s20.20sp=0.334 n=6
xstate - node (v18.15.0, x64)
Memory used512,489k (± 0.01%)512,494k (± 0.02%)~512,373k512,691kp=0.936 n=6
Parse Time3.26s (± 0.37%)3.27s (± 0.46%)~3.24s3.28sp=0.218 n=6
Bind Time1.55s (± 0.33%)1.55s (± 0.41%)~1.54s1.56sp=0.386 n=6
Check Time2.81s (± 1.09%)2.78s (± 0.23%)~2.77s2.79sp=0.059 n=6
Emit Time0.08s (± 4.99%)0.08s (± 4.99%)~0.08s0.09sp=1.000 n=6
Total Time7.69s (± 0.45%)7.68s (± 0.27%)~7.66s7.71sp=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)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@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!

Comment threadsrc/compiler/types.ts
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
Copy Markdown
MemberAuthor

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
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Compiler

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Angular - bun (v1.0.2, x64)
Memory used317,145k (± 0.26%)316,439k (± 0.24%)~314,201k318,067kp=0.126 n=12
Parse Time2.29s (± 0.48%)2.29s (± 0.47%)~2.27s2.33sp=1.000 n=12
Bind Time0.90s (± 1.69%)0.89s (± 1.01%)~0.87s0.92sp=0.745 n=12
Check Time7.98s (± 0.45%)7.99s (± 0.25%)~7.94s8.04sp=0.259 n=12
Emit Time6.43s (± 0.45%)6.41s (± 0.30%)~6.35s6.47sp=0.307 n=12
Total Time17.59s (± 0.30%)17.58s (± 0.16%)~17.53s17.67sp=0.885 n=12
Compiler-Unions - bun (v1.0.2, x64)
Memory used243,470k (± 1.02%)245,379k (± 0.59%)~239,833k247,542kp=0.371 n=12
Parse Time1.13s (± 1.66%)1.13s (± 1.41%)~1.11s1.18sp=1.000 n=12
Bind Time0.84s (± 0.67%)0.84s (± 2.16%)~0.81s0.90sp=0.498 n=12
Check Time8.82s (± 0.53%)8.78s (± 0.18%)~8.74s8.82sp=0.352 n=12
Emit Time2.78s (± 0.61%)2.79s (± 0.35%)~2.76s2.81sp=0.320 n=12
Total Time13.57s (± 0.37%)13.55s (± 0.20%)~13.48s13.62sp=0.369 n=12
Monaco - bun (v1.0.2, x64)
Memory used377,339k (± 0.21%)377,447k (± 0.18%)~375,870k379,065kp=0.795 n=12
Parse Time1.97s (± 0.64%)1.97s (± 0.67%)~1.95s2.00sp=0.930 n=12
Bind Time0.96s (± 1.48%)0.97s (± 1.81%)~0.93s1.02sp=0.268 n=12
Check Time7.38s (± 0.30%)7.37s (± 0.27%)~7.31s7.41sp=0.543 n=12
Emit Time3.78s (± 0.47%)3.77s (± 0.70%)~3.69s3.86sp=0.211 n=12
Total Time14.10s (± 0.18%)14.08s (± 0.23%)~14.01s14.19sp=0.434 n=12
TFS - bun (v1.0.2, x64)
Memory used317,938k (± 0.20%)317,329k (± 0.17%)~315,501k318,466kp=0.157 n=12
Parse Time1.74s (± 0.33%)1.75s (± 0.73%)~1.73s1.80sp=0.567 n=12
Bind Time0.99s (± 1.41%)0.98s (± 1.88%)~0.92s1.01sp=0.930 n=12
Check Time6.68s (± 0.42%)6.68s (± 0.36%)~6.61s6.73sp=0.908 n=12
Emit Time3.47s (± 0.71%)3.47s (± 0.70%)~3.42s3.55sp=0.908 n=12
Total Time12.88s (± 0.36%)12.89s (± 0.31%)~12.79s12.98sp=0.622 n=12
material-ui - bun (v1.0.2, x64)
Memory used494,317k (± 1.91%)484,418k (± 1.60%)~475,405k510,266kp=0.078 n=12
Parse Time2.23s (± 0.33%)2.22s (± 0.28%)~2.21s2.24sp=0.952 n=12
Bind Time0.74s (± 3.03%)0.76s (± 1.96%)~0.71s0.78sp=0.201 n=12
Check Time15.80s (± 0.40%)15.87s (± 0.47%)~15.70s16.05sp=0.119 n=12
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=12
Total Time18.76s (± 0.29%)18.86s (± 0.41%)+0.10s (+ 0.52%)18.68s19.01sp=0.049 n=12
xstate - bun (v1.0.2, x64)
Memory used410,202k (± 5.77%)403,990k (± 5.55%)~381,218k463,907kp=0.707 n=12
Parse Time3.22s (± 0.39%)3.23s (± 0.53%)~3.20s3.28sp=0.205 n=12
Bind Time1.26s (± 1.03%)1.26s (± 0.63%)~1.24s1.28sp=0.814 n=12
Check Time3.59s (± 0.28%)3.59s (± 0.35%)~3.56s3.62sp=0.347 n=12
Emit Time0.21s (± 1.57%)0.21s (± 2.93%)~0.20s0.23sp=0.974 n=12
Total Time8.28s (± 0.29%)8.29s (± 0.20%)~8.25s8.35sp=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)
BenchmarkNameIterations
Currentpr12
Baselinebaseline12

Startup

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
tsc-startup - bun (v1.0.2, x64)
Execution time425.46ms (± 0.08%)425.43ms (± 0.10%)~424.08ms433.95msp=0.051 n=600
tsserverlibrary-startup - bun (v1.0.2, x64)
Execution time660.84ms (± 0.12%)662.09ms (± 0.13%)+1.25ms (+ 0.19%)660.21ms685.95msp=0.000 n=600
typescript-startup - bun (v1.0.2, x64)
Execution time659.87ms (± 0.12%)661.14ms (± 0.13%)+1.27ms (+ 0.19%)659.39ms676.29msp=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)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@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
Copy Markdown
Contributor

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

@fatcerberus

Bruce Pascoe (fatcerberus) commented Sep 22, 2023

Copy link
Copy Markdown

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

Bruce Pascoe (fatcerberus) commented Sep 22, 2023

Copy link
Copy Markdown

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

classA{}consta=newA();constb=<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

Jake Bailey (jakebailey) commented Sep 22, 2023

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown

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.

exportdeclareclassA{}exportdeclareconsta: A;exportdeclareconstb: <A>(x: A)=>import("./input").A;

@jakebailey

Copy link
Copy Markdown
MemberAuthor

Write:

classA{}consta=newA();functionb<A,>(x: A){returna;}

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
Copy Markdown

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

@jakebaileyJake Bailey (jakebailey) changed the title Preserve shadowed type parameter names in more casesPreserve original type parameter names more often when shadowedSep 22, 2023


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

Copy link
Copy Markdown
MemberAuthor

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]

@jakebaileyJake Bailey (jakebailey)Oct 9, 2023

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
MemberAuthor

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
Copy Markdown
Member

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-botTypeScript 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
Copy Markdown
MemberAuthor

Dunno why CI isn't running...

@jakebailey
Jake Bailey (jakebailey) merged commit 1ed8ed6 into microsoft:mainNov 6, 2023
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Milestone BugPRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type parameters are renamed in declarations Arrow function parameters using an alias and intersection are expanded inappropriately in .d.ts

4 participants

@jakebailey@typescript-bot@fatcerberus@weswigham