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

Reuse cached resolved signatures early #60208

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

Andarist
Copy link
Contributor

fixes #60202

cc @gabritto

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 12, 2024
BaseClass extends new (...args: any[]) => any,
>(Base: BaseClass): any;

declare class Item extends ClientDocumentMixin(BaseItem) {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

due to the circular nature of this example, the compiler goes into resolveCall for the same node twice in a nested manner

  1. the first getResolvedSignature ends up calling resolveAnonymousTypeMembers for typeof BaseItem
  2. in the process it preemptively sets empty signatures, members, index infos on that type here
  3. but then it also computes real construct signatures here
  4. this in turn leads to calling getResolvedSignature again for the same node and all
  5. in that inner call we see no construct signatures on typeof BaseItem (those were set to an empty array) so the error is raised because signaturesRelatedTo fails to determine that this source (typeof BaseItem) has the required target signatures
  6. this inner call errors and exits, caching the getCandidateForOverloadFailure's result as the links.resolvedSignature. That signature is (Base: new (...args: any[]) => any): any
  7. this leads to resolving (): BaseItem as getDefaultConstructSignatures and that replaces the empty construct signatures preemptively set by resolveAnonymousTypeMembers
  8. now we climb up the stack, going back to the first/outer getResolvedSignature call for this. When calling getSignatureApplicabilityError the compiler returns an error because it reuses cached RelationComparisonResult.Failed that was cached when relating the argument and parameter types in that inner call
  9. so now the compiler computes getCandidateForOverloadFailure again and tries to elaborate this error (again!).
  10. this time it succeeds when relating those 2 types because the construct signatures of typeof BaseItem changed "in-between". So the debug assert kicks finally kicks in and the compiler crashes

Choose a reason for hiding this comment

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

Thanks a ton for this PR and the explanation! It makes the crash make much more sense to me now.

I will say some of these steps still seem to have a bit of concerning logic and seem to help explain something else weird I noted on my issue, namely that there doesn't have to be a "real" loop in the base type for you to get an error like Argument of type 'typeof BaseItem' is not assignable to parameter of type 'new (...args: any[]) => any'.

Playground (make sure to make any edit to overcome the crash and view an error!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh your example is somewhat convoluted to the point that I'm not sure if it should error or not. It's definitely outside of the "regular TS" territory 😅 so all I was after here was to fix the crash. Once this gets fixed you might want to raise a new issue about the circularity issue if you thing it shouldn't be reported

@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 12, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

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

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user tests with tsc comparing main and refs/pull/60208/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 31 31 ~ ~ ~ p=1.000 n=6
Symbols 62,340 62,340 ~ ~ ~ p=1.000 n=6
Types 50,379 50,379 ~ ~ ~ p=1.000 n=6
Memory used 192,901k (± 0.09%) 192,859k (± 0.07%) ~ 192,793k 193,115k p=0.810 n=6
Parse Time 1.30s (± 1.37%) 1.31s (± 0.62%) ~ 1.30s 1.32s p=0.157 n=6
Bind Time 0.72s (± 0.57%) 0.72s ~ ~ ~ p=0.405 n=6
Check Time 9.72s (± 0.27%) 9.75s (± 0.31%) ~ 9.71s 9.80s p=0.170 n=6
Emit Time 2.72s (± 1.28%) 2.72s (± 0.55%) ~ 2.70s 2.74s p=0.332 n=6
Total Time 14.47s (± 0.30%) 14.50s (± 0.21%) ~ 14.46s 14.54s p=0.420 n=6
angular-1 - node (v18.15.0, x64)
Errors 33 33 ~ ~ ~ p=1.000 n=6
Symbols 947,886 947,886 ~ ~ ~ p=1.000 n=6
Types 410,840 410,840 ~ ~ ~ p=1.000 n=6
Memory used 1,224,591k (± 0.00%) 1,224,577k (± 0.00%) ~ 1,224,544k 1,224,596k p=0.688 n=6
Parse Time 6.66s (± 0.72%) 6.64s (± 0.87%) ~ 6.59s 6.73s p=0.573 n=6
Bind Time 1.88s (± 0.27%) 1.88s ~ ~ ~ p=0.174 n=6
Check Time 31.81s (± 0.27%) 31.82s (± 0.38%) ~ 31.69s 32.02s p=0.936 n=6
Emit Time 15.18s (± 0.38%) 15.13s (± 0.28%) ~ 15.07s 15.17s p=0.260 n=6
Total Time 55.52s (± 0.30%) 55.47s (± 0.30%) ~ 55.29s 55.67s p=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,530,075 2,530,075 ~ ~ ~ p=1.000 n=6
Types 916,374 916,374 ~ ~ ~ p=1.000 n=6
Memory used 2,341,297k (± 0.00%) 2,341,307k (± 0.00%) ~ 2,341,243k 2,341,401k p=0.936 n=6
Parse Time 9.37s (± 0.46%) 9.39s (± 0.21%) ~ 9.36s 9.41s p=0.573 n=6
Bind Time 2.17s (± 0.45%) 2.16s (± 0.45%) ~ 2.15s 2.18s p=0.211 n=6
Check Time 76.04s (± 0.35%) 75.93s (± 0.49%) ~ 75.56s 76.55s p=0.378 n=6
Emit Time 0.28s (± 2.70%) 0.27s (± 2.98%) ~ 0.27s 0.29s p=0.206 n=6
Total Time 87.86s (± 0.33%) 87.75s (± 0.43%) ~ 87.39s 88.39s p=0.471 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,258,109 1,258,110 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,266 266,266 ~ ~ ~ p=1.000 n=6
Memory used 3,146,212k (± 0.01%) 3,146,853k (± 0.05%) ~ 3,145,282k 3,148,807k p=1.000 n=6
Parse Time 6.59s (± 0.47%) 6.60s (± 0.84%) ~ 6.54s 6.68s p=0.936 n=6
Bind Time 2.32s (± 1.26%) 2.28s (± 4.13%) ~ 2.09s 2.33s p=0.746 n=6
Check Time 42.99s (± 0.36%) 42.94s (± 0.34%) ~ 42.75s 43.11s p=0.810 n=6
Emit Time 3.59s (± 1.88%) 3.56s (± 2.06%) ~ 3.49s 3.68s p=0.422 n=6
Total Time 55.48s (± 0.20%) 55.38s (± 0.38%) ~ 55.05s 55.68s p=0.575 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,258,109 1,258,110 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,266 266,266 ~ ~ ~ p=1.000 n=6
Memory used 2,739,089k (±13.56%) 2,859,044k (±13.74%) ~ 2,498,920k 3,218,158k p=0.689 n=6
Parse Time 6.65s (± 2.66%) 6.68s (± 2.36%) ~ 6.54s 6.90s p=0.873 n=6
Bind Time 2.18s (± 4.35%) 2.23s (± 4.50%) ~ 2.14s 2.37s p=0.172 n=6
Check Time 43.15s (± 0.23%) 43.18s (± 0.41%) ~ 42.91s 43.39s p=0.689 n=6
Emit Time 3.47s (± 1.77%) 3.54s (± 1.86%) ~ 3.44s 3.61s p=0.173 n=6
Total Time 55.47s (± 0.46%) 55.63s (± 0.52%) ~ 55.13s 55.93s p=0.378 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 261,786 261,787 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 106,508 106,508 ~ ~ ~ p=1.000 n=6
Memory used 438,538k (± 0.01%) 438,572k (± 0.01%) ~ 438,462k 438,626k p=0.173 n=6
Parse Time 2.90s (± 0.85%) 2.90s (± 0.65%) ~ 2.88s 2.92s p=0.935 n=6
Bind Time 1.10s (± 0.47%) 1.10s ~ ~ ~ p=0.174 n=6
Check Time 15.72s (± 0.24%) 15.72s (± 0.31%) ~ 15.66s 15.78s p=1.000 n=6
Emit Time 1.30s (± 1.58%) 1.31s (± 0.93%) ~ 1.29s 1.32s p=0.871 n=6
Total Time 21.03s (± 0.17%) 21.02s (± 0.21%) ~ 20.96s 21.08s p=0.809 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 68 68 ~ ~ ~ p=1.000 n=6
Symbols 225,919 225,919 ~ ~ ~ p=1.000 n=6
Types 94,415 94,415 ~ ~ ~ p=1.000 n=6
Memory used 371,058k (± 0.01%) 371,089k (± 0.01%) ~ 371,064k 371,116k p=0.054 n=6
Parse Time 2.89s (± 0.75%) 2.88s (± 1.58%) ~ 2.84s 2.95s p=0.520 n=6
Bind Time 1.57s (± 1.11%) 1.58s (± 0.77%) ~ 1.56s 1.59s p=0.744 n=6
Check Time 16.36s (± 0.28%) 16.42s (± 0.12%) +0.06s (+ 0.36%) 16.39s 16.44s p=0.019 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.83s (± 0.21%) 20.88s (± 0.30%) ~ 20.79s 20.95s p=0.171 n=6
vscode - node (v18.15.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 3,117,487 3,117,487 ~ ~ ~ p=1.000 n=6
Types 1,074,418 1,074,418 ~ ~ ~ p=1.000 n=6
Memory used 3,210,903k (± 0.01%) 3,210,927k (± 0.01%) ~ 3,210,173k 3,211,292k p=0.689 n=6
Parse Time 14.01s (± 0.32%) 14.05s (± 0.63%) ~ 13.90s 14.16s p=0.229 n=6
Bind Time 4.43s (± 0.56%) 4.45s (± 0.40%) ~ 4.42s 4.47s p=0.253 n=6
Check Time 86.44s (± 1.85%) 85.36s (± 1.16%) ~ 83.84s 86.77s p=0.378 n=6
Emit Time 26.52s (± 5.23%) 25.55s (± 8.17%) ~ 22.63s 27.35s p=0.173 n=6
Total Time 131.40s (± 1.90%) 129.40s (± 1.69%) ~ 126.10s 131.93s p=0.199 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 285,224 285,224 ~ ~ ~ p=1.000 n=6
Types 115,781 115,781 ~ ~ ~ p=1.000 n=6
Memory used 435,393k (± 0.04%) 435,353k (± 0.03%) ~ 435,119k 435,479k p=0.689 n=6
Parse Time 5.04s (± 0.71%) 5.04s (± 0.52%) ~ 5.00s 5.07s p=0.808 n=6
Bind Time 2.17s (± 1.78%) 2.15s (± 1.48%) ~ 2.10s 2.19s p=0.169 n=6
Check Time 22.80s (± 0.59%) 22.80s (± 0.65%) ~ 22.59s 22.98s p=0.810 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 30.01s (± 0.52%) 29.99s (± 0.58%) ~ 29.72s 30.16s p=0.936 n=6
xstate-main - node (v18.15.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 540,222 540,222 ~ ~ ~ p=1.000 n=6
Types 181,145 181,145 ~ ~ ~ p=1.000 n=6
Memory used 483,836k (± 0.01%) 483,870k (± 0.01%) ~ 483,761k 483,948k p=0.261 n=6
Parse Time 4.18s (± 0.58%) 4.17s (± 0.48%) ~ 4.14s 4.20s p=0.745 n=6
Bind Time 1.45s (± 0.83%) 1.46s (± 0.72%) ~ 1.44s 1.47s p=1.000 n=6
Check Time 23.65s (± 0.36%) 23.73s (± 0.20%) ~ 23.66s 23.78s p=0.054 n=6
Emit Time 0.00s (±154.76%) 0.00s (±154.76%) ~ 0.00s 0.01s p=1.000 n=6
Total Time 29.28s (± 0.34%) 29.36s (± 0.19%) ~ 29.29s 29.43s p=0.128 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - 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 top 400 repos with tsc comparing main and refs/pull/60208/merge:

Everything looks good!

Copy link
Member

@gabritto gabritto left a comment

Choose a reason for hiding this comment

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

Seems like a promising idea in the absence of the possibility of fixing the assignability result inconsistency, but I left a question.

src/compiler/checker.ts Show resolved Hide resolved
src/compiler/checker.ts Show resolved Hide resolved
@gabritto
Copy link
Member

Maybe related fix: #49598

@gabritto gabritto self-assigned this Oct 18, 2024
Copy link
Member

@gabritto gabritto left a comment

Choose a reason for hiding this comment

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

Left a question.

@Andarist Andarist requested a review from gabritto October 23, 2024 19:22
@gabritto gabritto merged commit 5e2e321 into microsoft:main Nov 6, 2024
32 checks passed
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: Done
Development

Successfully merging this pull request may close these issues.

"No error for last overload signature" regression in #58859
5 participants