@geastack/compiler 1.0.20 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (534) hide show
  1. package/dist/cli-emit.js +11 -1
  2. package/dist/cli-package-source.d.ts +0 -4
  3. package/dist/cli-package-source.js +8 -112
  4. package/dist/cli-refusal-location.d.ts +6 -0
  5. package/dist/cli-refusal-location.js +5 -0
  6. package/dist/cli.js +2 -1
  7. package/dist/compiler.d.ts +4 -0
  8. package/dist/compiler.js +118 -28
  9. package/dist/conversion/algebra.d.ts +16 -1
  10. package/dist/conversion/algebra.js +2 -0
  11. package/dist/conversion/build.js +16 -6
  12. package/dist/conversion/constructor-identity-family.d.ts +49 -0
  13. package/dist/conversion/constructor-identity-family.js +67 -0
  14. package/dist/conversion/derive.js +39 -6
  15. package/dist/conversion/iterable-object-view.d.ts +44 -0
  16. package/dist/conversion/iterable-object-view.js +55 -0
  17. package/dist/conversion/iterator-object-view.d.ts +77 -0
  18. package/dist/conversion/iterator-object-view.js +112 -0
  19. package/dist/conversion/nodes.d.ts +93 -0
  20. package/dist/conversion/nodes.js +262 -5
  21. package/dist/conversion/operand-view.js +41 -0
  22. package/dist/conversion/protocol-iterator.d.ts +84 -0
  23. package/dist/conversion/protocol-iterator.js +88 -0
  24. package/dist/conversion/record-view.d.ts +152 -8
  25. package/dist/conversion/record-view.js +518 -37
  26. package/dist/conversion/registry.d.ts +32 -0
  27. package/dist/ir/borrowed-call-arguments.d.ts +22 -1
  28. package/dist/ir/borrowed-call-arguments.js +102 -1
  29. package/dist/ir/borrowed-callable-uses.d.ts +62 -0
  30. package/dist/ir/borrowed-callable-uses.js +135 -0
  31. package/dist/ir/boxed-bind-assumptions.d.ts +24 -0
  32. package/dist/ir/boxed-bind-assumptions.js +93 -0
  33. package/dist/ir/build.d.ts +18 -7
  34. package/dist/ir/build.js +71 -13
  35. package/dist/ir/call-dispatch.js +474 -10
  36. package/dist/ir/call-entry.d.ts +24 -1
  37. package/dist/ir/call-entry.js +106 -1
  38. package/dist/ir/callable-class-flow.js +15 -5
  39. package/dist/ir/callable-identity-demand.d.ts +20 -1
  40. package/dist/ir/callable-identity-demand.js +157 -18
  41. package/dist/ir/callable-member-candidates.d.ts +11 -1
  42. package/dist/ir/callable-member-candidates.js +82 -3
  43. package/dist/ir/callable-records.js +2 -2
  44. package/dist/ir/callable-site-escape.d.ts +4 -0
  45. package/dist/ir/callable-site-escape.js +228 -0
  46. package/dist/ir/captures.d.ts +10 -1
  47. package/dist/ir/captures.js +219 -4
  48. package/dist/ir/certify/carrier-keys.d.ts +58 -1
  49. package/dist/ir/certify/carrier-keys.js +158 -6
  50. package/dist/ir/certify/property-access-keys.d.ts +17 -0
  51. package/dist/ir/certify/property-access-keys.js +36 -0
  52. package/dist/ir/certify/property-access.js +49 -4
  53. package/dist/ir/certify/runtime-helper.d.ts +4 -0
  54. package/dist/ir/certify/runtime-helper.js +17 -5
  55. package/dist/ir/certify.js +132 -79
  56. package/dist/ir/class-static-fields.d.ts +11 -0
  57. package/dist/ir/class-static-fields.js +28 -0
  58. package/dist/ir/construct-entry.d.ts +2 -1
  59. package/dist/ir/construct-entry.js +3 -2
  60. package/dist/ir/construction-only-fields.d.ts +73 -0
  61. package/dist/ir/construction-only-fields.js +211 -0
  62. package/dist/ir/dominance.d.ts +14 -0
  63. package/dist/ir/dominance.js +18 -4
  64. package/dist/ir/emission-representations.js +4 -1
  65. package/dist/ir/exception-edges.d.ts +17 -0
  66. package/dist/ir/exception-edges.js +75 -0
  67. package/dist/ir/facts.d.ts +54 -3
  68. package/dist/ir/facts.js +182 -5
  69. package/dist/ir/generator-split.js +2 -2
  70. package/dist/ir/instance-reparenting.d.ts +83 -0
  71. package/dist/ir/instance-reparenting.js +167 -0
  72. package/dist/ir/integer-storage.d.ts +29 -0
  73. package/dist/ir/integer-storage.js +188 -79
  74. package/dist/ir/integers.d.ts +16 -0
  75. package/dist/ir/integers.js +327 -8
  76. package/dist/ir/integrity-restrictions.d.ts +42 -9
  77. package/dist/ir/integrity-restrictions.js +335 -42
  78. package/dist/ir/key-order-observation.d.ts +38 -0
  79. package/dist/ir/key-order-observation.js +173 -0
  80. package/dist/ir/literal-set-membership.d.ts +30 -0
  81. package/dist/ir/literal-set-membership.js +277 -0
  82. package/dist/ir/lower-allocation.js +68 -8
  83. package/dist/ir/lower-completion.d.ts +0 -9
  84. package/dist/ir/lower-completion.js +14 -5
  85. package/dist/ir/lower-destructuring.js +24 -2
  86. package/dist/ir/lower-exceptions.js +4 -4
  87. package/dist/ir/lower-flow.d.ts +21 -0
  88. package/dist/ir/lower-flow.js +37 -3
  89. package/dist/ir/lower-graph.d.ts +19 -1
  90. package/dist/ir/lower-graph.js +91 -67
  91. package/dist/ir/lower-invocation.d.ts +16 -0
  92. package/dist/ir/lower-invocation.js +182 -28
  93. package/dist/ir/lower-narrow.d.ts +0 -46
  94. package/dist/ir/lower-narrow.js +63 -1
  95. package/dist/ir/lower-operands.d.ts +52 -2
  96. package/dist/ir/lower-operands.js +491 -8
  97. package/dist/ir/lower-property.d.ts +2 -1
  98. package/dist/ir/lower-property.js +140 -31
  99. package/dist/ir/lower-protocol.js +16 -4
  100. package/dist/ir/lower-proxy.d.ts +65 -0
  101. package/dist/ir/lower-proxy.js +181 -0
  102. package/dist/ir/lower.js +112 -17
  103. package/dist/ir/model.d.ts +228 -18
  104. package/dist/ir/native-class-accessor.d.ts +9 -2
  105. package/dist/ir/native-class-accessor.js +12 -3
  106. package/dist/ir/native-class-construction.d.ts +1 -1
  107. package/dist/ir/native-class-construction.js +2 -1
  108. package/dist/ir/native-class-initialization.js +2 -2
  109. package/dist/ir/object-value-conversions.js +20 -5
  110. package/dist/ir/operation-index.d.ts +16 -0
  111. package/dist/ir/operation-index.js +17 -0
  112. package/dist/ir/pcm-loops.d.ts +11 -0
  113. package/dist/ir/pcm-loops.js +284 -0
  114. package/dist/ir/presence-proof.js +5 -0
  115. package/dist/ir/program-facts.d.ts +10 -20
  116. package/dist/ir/program-facts.js +80 -6
  117. package/dist/ir/proven-branches.d.ts +2 -2
  118. package/dist/ir/proven-branches.js +196 -3
  119. package/dist/ir/queries.js +35 -1
  120. package/dist/ir/reflection-demand.d.ts +9 -4
  121. package/dist/ir/reflection-demand.js +400 -28
  122. package/dist/ir/refusal.d.ts +1 -0
  123. package/dist/ir/refusal.js +2 -1
  124. package/dist/ir/shake.d.ts +94 -1
  125. package/dist/ir/shake.js +209 -12
  126. package/dist/ir/shared-empty-records.d.ts +12 -0
  127. package/dist/ir/shared-empty-records.js +301 -0
  128. package/dist/ir/spread-copy-elision.d.ts +59 -0
  129. package/dist/ir/spread-copy-elision.js +431 -0
  130. package/dist/ir/stable-cell-reads.d.ts +40 -0
  131. package/dist/ir/stable-cell-reads.js +123 -0
  132. package/dist/ir/static-callables.js +50 -0
  133. package/dist/ir/string-layout-reuse.d.ts +16 -1
  134. package/dist/ir/string-layout-reuse.js +66 -0
  135. package/dist/ir/transfer.d.ts +72 -3
  136. package/dist/ir/transfer.js +511 -19
  137. package/dist/ir/typed-property-access.d.ts +18 -2
  138. package/dist/ir/typed-property-access.js +109 -12
  139. package/dist/ir/verify.js +2 -1
  140. package/dist/plugins/apple/plugin.js +3 -2
  141. package/dist/plugins/gea/host.d.ts +7 -0
  142. package/dist/plugins/gea/host.js +30 -1
  143. package/dist/plugins/gea/lower.js +6 -1
  144. package/dist/plugins/gea/plugin.js +7 -4
  145. package/dist/plugins/gea/render-bridge.d.ts +4 -2
  146. package/dist/plugins/gea/render-bridge.js +23 -7
  147. package/dist/plugins/model.d.ts +2 -0
  148. package/dist/project-preparation.d.ts +5 -1
  149. package/dist/project-preparation.js +164 -7
  150. package/dist/projection/bindings.d.ts +5 -2
  151. package/dist/projection/bindings.js +40 -1
  152. package/dist/projection/callee.d.ts +16 -0
  153. package/dist/projection/callee.js +35 -6
  154. package/dist/projection/class-prototype.js +5 -1
  155. package/dist/projection/classes.d.ts +65 -2
  156. package/dist/projection/classes.js +159 -2
  157. package/dist/projection/dispatch.d.ts +69 -18
  158. package/dist/projection/dispatch.js +391 -19
  159. package/dist/projection/fields.d.ts +4 -3
  160. package/dist/projection/fields.js +89 -21
  161. package/dist/projection/instance-test.d.ts +12 -0
  162. package/dist/projection/instance-test.js +40 -10
  163. package/dist/projection/method-value-escapes.d.ts +43 -0
  164. package/dist/projection/method-value-escapes.js +113 -0
  165. package/dist/projection/slots.d.ts +34 -1
  166. package/dist/projection/slots.js +82 -5
  167. package/dist/representation/any-null-record-members.d.ts +41 -0
  168. package/dist/representation/any-null-record-members.js +115 -0
  169. package/dist/representation/collections.d.ts +3 -1
  170. package/dist/representation/collections.js +5 -2
  171. package/dist/representation/derive.d.ts +9 -1
  172. package/dist/representation/derive.js +366 -25
  173. package/dist/representation/difference.js +14 -0
  174. package/dist/representation/embedded-carriers.js +1 -0
  175. package/dist/representation/host-abi.d.ts +15 -0
  176. package/dist/representation/host-abi.js +28 -0
  177. package/dist/representation/host-templates.d.ts +1 -1
  178. package/dist/representation/host-templates.js +7 -3
  179. package/dist/representation/intersection.js +22 -0
  180. package/dist/representation/json-nullable.d.ts +18 -0
  181. package/dist/representation/json-nullable.js +22 -0
  182. package/dist/representation/model.d.ts +141 -0
  183. package/dist/representation/model.js +47 -3
  184. package/dist/representation/object-shape.d.ts +5 -1
  185. package/dist/representation/object-shape.js +5 -2
  186. package/dist/representation/policies.d.ts +66 -1
  187. package/dist/representation/proxy-carriers.d.ts +71 -0
  188. package/dist/representation/proxy-carriers.js +330 -0
  189. package/dist/representation/publish.d.ts +9 -1
  190. package/dist/representation/publish.js +186 -18
  191. package/dist/representation/record-fields.d.ts +5 -0
  192. package/dist/representation/record-fields.js +7 -1
  193. package/dist/representation/static-field-cells.d.ts +61 -0
  194. package/dist/representation/static-field-cells.js +80 -0
  195. package/dist/representation/unassigned-binding-cells.d.ts +32 -0
  196. package/dist/representation/unassigned-binding-cells.js +44 -0
  197. package/dist/representation/union.js +17 -1
  198. package/dist/representation/unwritten-record-members.d.ts +54 -0
  199. package/dist/representation/unwritten-record-members.js +167 -0
  200. package/dist/representation/value-records.js +153 -11
  201. package/dist/semantics/ambient.d.ts +102 -0
  202. package/dist/semantics/ambient.js +183 -0
  203. package/dist/semantics/any-null-member-declarations.d.ts +32 -0
  204. package/dist/semantics/any-null-member-declarations.js +90 -0
  205. package/dist/semantics/borrowed-builtin-call-bind-source-transform.js +14 -0
  206. package/dist/semantics/borrowed-method-receiver-copy-source-transform.d.ts +51 -0
  207. package/dist/semantics/borrowed-method-receiver-copy-source-transform.js +367 -0
  208. package/dist/semantics/call-invalidated-narrowing.d.ts +3 -0
  209. package/dist/semantics/call-invalidated-narrowing.js +715 -0
  210. package/dist/semantics/callable-origins.d.ts +39 -0
  211. package/dist/semantics/callable-origins.js +73 -2
  212. package/dist/semantics/class-alias.d.ts +21 -0
  213. package/dist/semantics/class-alias.js +45 -14
  214. package/dist/semantics/class-heritage.d.ts +64 -0
  215. package/dist/semantics/class-heritage.js +278 -2
  216. package/dist/semantics/commonjs-wrapper.d.ts +1 -1
  217. package/dist/semantics/commonjs-wrapper.js +24 -7
  218. package/dist/semantics/constructor-function-class-source-transform.d.ts +4 -0
  219. package/dist/semantics/constructor-function-class-source-transform.js +327 -0
  220. package/dist/semantics/constructor-slot-subclasses.js +163 -4
  221. package/dist/semantics/declaration-overlay-transform.js +197 -3
  222. package/dist/semantics/frontend.d.ts +31 -0
  223. package/dist/semantics/frontend.js +177 -19
  224. package/dist/semantics/host-methods.d.ts +27 -2
  225. package/dist/semantics/host-methods.js +57 -4
  226. package/dist/semantics/host-protocols.d.ts +19 -8
  227. package/dist/semantics/host-protocols.js +145 -31
  228. package/dist/semantics/interface-families.d.ts +51 -2
  229. package/dist/semantics/interface-families.js +609 -23
  230. package/dist/semantics/interface-implementors.d.ts +20 -0
  231. package/dist/semantics/interface-implementors.js +130 -6
  232. package/dist/semantics/known-caller-predicate-parameters.d.ts +2 -0
  233. package/dist/semantics/known-caller-predicate-parameters.js +161 -0
  234. package/dist/semantics/model/intrinsic-accessor-getters.d.ts +10 -0
  235. package/dist/semantics/model/intrinsic-accessor-getters.js +4 -0
  236. package/dist/semantics/model/operands.d.ts +10 -0
  237. package/dist/semantics/model/operations.d.ts +107 -0
  238. package/dist/semantics/model/selected-signature.d.ts +33 -0
  239. package/dist/semantics/model/structural-type-table.d.ts +15 -0
  240. package/dist/semantics/model/structural-type-table.js +19 -1
  241. package/dist/semantics/model/structural-types.d.ts +99 -0
  242. package/dist/semantics/model/structural-types.js +31 -2
  243. package/dist/semantics/normalize/absent-globals.d.ts +1 -1
  244. package/dist/semantics/normalize/absent-globals.js +3 -3
  245. package/dist/semantics/normalize/bivariant-slot-parameter.d.ts +46 -0
  246. package/dist/semantics/normalize/bivariant-slot-parameter.js +357 -0
  247. package/dist/semantics/normalize/callback-parameter-contracts.d.ts +2 -0
  248. package/dist/semantics/normalize/callback-parameter-contracts.js +21 -0
  249. package/dist/semantics/normalize/census.d.ts +2 -1
  250. package/dist/semantics/normalize/census.js +25 -2
  251. package/dist/semantics/normalize/class-instance-record-statement.d.ts +37 -0
  252. package/dist/semantics/normalize/class-instance-record-statement.js +74 -0
  253. package/dist/semantics/normalize/collection-bindings.js +242 -5
  254. package/dist/semantics/normalize/commonjs-require.d.ts +26 -0
  255. package/dist/semantics/normalize/commonjs-require.js +355 -121
  256. package/dist/semantics/normalize/dead-event-emissions.d.ts +6 -0
  257. package/dist/semantics/normalize/dead-event-emissions.js +866 -0
  258. package/dist/semantics/normalize/dead-method-copies.d.ts +56 -0
  259. package/dist/semantics/normalize/dead-method-copies.js +239 -0
  260. package/dist/semantics/normalize/derived-expression-type.d.ts +149 -2
  261. package/dist/semantics/normalize/derived-expression-type.js +418 -14
  262. package/dist/semantics/normalize/field-bindings.js +139 -4
  263. package/dist/semantics/normalize/flow/callable-reach.js +162 -9
  264. package/dist/semantics/normalize/flow/member-call-forwarding.js +6 -0
  265. package/dist/semantics/normalize/flow/owned-class-receivers.js +40 -2
  266. package/dist/semantics/normalize/flow/source-class-data.d.ts +7 -1
  267. package/dist/semantics/normalize/flow/source-class-data.js +28 -9
  268. package/dist/semantics/normalize/flow/source-value-session.js +32 -13
  269. package/dist/semantics/normalize/flow/targets.d.ts +32 -0
  270. package/dist/semantics/normalize/flow/targets.js +65 -6
  271. package/dist/semantics/normalize/flow/value-flow.d.ts +7 -0
  272. package/dist/semantics/normalize/flow/value-flow.js +30 -2
  273. package/dist/semantics/normalize/fresh-array-result.d.ts +32 -0
  274. package/dist/semantics/normalize/fresh-array-result.js +125 -0
  275. package/dist/semantics/normalize/gating.js +2 -0
  276. package/dist/semantics/normalize/global-host-mutations.d.ts +11 -1
  277. package/dist/semantics/normalize/global-host-mutations.js +691 -27
  278. package/dist/semantics/normalize/host-effect-contracts.d.ts +43 -1
  279. package/dist/semantics/normalize/host-effect-contracts.js +60 -3
  280. package/dist/semantics/normalize/identities.js +49 -3
  281. package/dist/semantics/normalize/intrinsic-accessor-getter.d.ts +59 -0
  282. package/dist/semantics/normalize/intrinsic-accessor-getter.js +129 -0
  283. package/dist/semantics/normalize/local-bindings.js +117 -21
  284. package/dist/semantics/normalize/merged-declaration.d.ts +41 -0
  285. package/dist/semantics/normalize/merged-declaration.js +81 -0
  286. package/dist/semantics/normalize/never-override-result.d.ts +21 -0
  287. package/dist/semantics/normalize/never-override-result.js +64 -0
  288. package/dist/semantics/normalize/null-blind-parameter.d.ts +21 -0
  289. package/dist/semantics/normalize/null-blind-parameter.js +77 -0
  290. package/dist/semantics/normalize/object-bag-bindings.js +10 -0
  291. package/dist/semantics/normalize/override-field-arms.d.ts +33 -0
  292. package/dist/semantics/normalize/override-field-arms.js +362 -0
  293. package/dist/semantics/normalize/parameter-bindings.d.ts +64 -1
  294. package/dist/semantics/normalize/parameter-bindings.js +801 -81
  295. package/dist/semantics/normalize/parameter-slot.d.ts +1 -0
  296. package/dist/semantics/normalize/parameter-slot.js +69 -0
  297. package/dist/semantics/normalize/physical-overload-result.d.ts +45 -0
  298. package/dist/semantics/normalize/physical-overload-result.js +132 -0
  299. package/dist/semantics/normalize/producer-context.d.ts +23 -0
  300. package/dist/semantics/normalize/producers/allocations.d.ts +13 -0
  301. package/dist/semantics/normalize/producers/allocations.js +8 -15
  302. package/dist/semantics/normalize/producers/binding-kind.d.ts +11 -0
  303. package/dist/semantics/normalize/producers/binding-kind.js +31 -0
  304. package/dist/semantics/normalize/producers/bindings.js +16 -1
  305. package/dist/semantics/normalize/producers/boundary.js +21 -1
  306. package/dist/semantics/normalize/producers/class-lifecycle.js +23 -14
  307. package/dist/semantics/normalize/producers/computations.js +8 -67
  308. package/dist/semantics/normalize/producers/control-loops.d.ts +1 -1
  309. package/dist/semantics/normalize/producers/control-loops.js +25 -1
  310. package/dist/semantics/normalize/producers/control.js +87 -60
  311. package/dist/semantics/normalize/producers/declaration-lifecycle.js +5 -0
  312. package/dist/semantics/normalize/producers/enums.d.ts +10 -0
  313. package/dist/semantics/normalize/producers/enums.js +169 -0
  314. package/dist/semantics/normalize/producers/erasure.d.ts +10 -0
  315. package/dist/semantics/normalize/producers/erasure.js +23 -0
  316. package/dist/semantics/normalize/producers/intrinsic-accessor-getter.d.ts +15 -0
  317. package/dist/semantics/normalize/producers/intrinsic-accessor-getter.js +33 -0
  318. package/dist/semantics/normalize/producers/invocations.js +255 -18
  319. package/dist/semantics/normalize/producers/iteration-yield.js +6 -1
  320. package/dist/semantics/normalize/producers/logical-merge-type.d.ts +36 -0
  321. package/dist/semantics/normalize/producers/logical-merge-type.js +163 -0
  322. package/dist/semantics/normalize/producers/properties.js +207 -18
  323. package/dist/semantics/normalize/producers/protocol.d.ts +4 -0
  324. package/dist/semantics/normalize/producers/protocol.js +147 -12
  325. package/dist/semantics/normalize/producers/references.js +50 -9
  326. package/dist/semantics/normalize/producers/shared.d.ts +120 -0
  327. package/dist/semantics/normalize/producers/shared.js +359 -10
  328. package/dist/semantics/normalize/producers/spread-arguments.js +85 -4
  329. package/dist/semantics/normalize/producers/tuple-spread.d.ts +1 -0
  330. package/dist/semantics/normalize/producers/tuple-spread.js +1 -1
  331. package/dist/semantics/normalize/property-key-domain.js +6 -0
  332. package/dist/semantics/normalize/reachability.d.ts +7 -0
  333. package/dist/semantics/normalize/reachability.js +166 -9
  334. package/dist/semantics/normalize/record-home-arms.d.ts +25 -0
  335. package/dist/semantics/normalize/record-home-arms.js +87 -0
  336. package/dist/semantics/normalize/record-link-families.d.ts +74 -0
  337. package/dist/semantics/normalize/record-link-families.js +327 -0
  338. package/dist/semantics/normalize/record-stand-in-arms.d.ts +56 -0
  339. package/dist/semantics/normalize/record-stand-in-arms.js +484 -0
  340. package/dist/semantics/normalize/return-bindings.d.ts +6 -2
  341. package/dist/semantics/normalize/return-bindings.js +60 -7
  342. package/dist/semantics/normalize/script-global-redefinition.js +5 -0
  343. package/dist/semantics/normalize/shared-array-storage.d.ts +42 -0
  344. package/dist/semantics/normalize/shared-array-storage.js +373 -0
  345. package/dist/semantics/normalize/sloppy-absence.d.ts +41 -0
  346. package/dist/semantics/normalize/sloppy-absence.js +199 -0
  347. package/dist/semantics/normalize/specialization.d.ts +40 -0
  348. package/dist/semantics/normalize/specialization.js +770 -36
  349. package/dist/semantics/normalize/structural-array-element.js +196 -6
  350. package/dist/semantics/normalize/structural-array-read.js +12 -0
  351. package/dist/semantics/normalize/structural-creation-order.d.ts +15 -0
  352. package/dist/semantics/normalize/structural-creation-order.js +51 -48
  353. package/dist/semantics/normalize/structural-declarations.d.ts +16 -0
  354. package/dist/semantics/normalize/structural-declarations.js +43 -0
  355. package/dist/semantics/normalize/structural-generics.d.ts +13 -0
  356. package/dist/semantics/normalize/structural-generics.js +22 -0
  357. package/dist/semantics/normalize/structural-indexed-access.js +16 -0
  358. package/dist/semantics/normalize/structural-instantiated-member.js +68 -2
  359. package/dist/semantics/normalize/structural-joins.js +2 -0
  360. package/dist/semantics/normalize/structural-layout-relevance.d.ts +30 -0
  361. package/dist/semantics/normalize/structural-layout-relevance.js +164 -0
  362. package/dist/semantics/normalize/structural-layout-type.js +270 -14
  363. package/dist/semantics/normalize/structural-leaves.d.ts +1 -1
  364. package/dist/semantics/normalize/structural-leaves.js +17 -3
  365. package/dist/semantics/normalize/structural-mutable-method.js +5 -1
  366. package/dist/semantics/normalize/structural-parts.d.ts +18 -5
  367. package/dist/semantics/normalize/structural-parts.js +68 -19
  368. package/dist/semantics/normalize/structural-receiver.d.ts +1 -1
  369. package/dist/semantics/normalize/structural-receiver.js +23 -1
  370. package/dist/semantics/normalize/structural-self-reference.d.ts +1 -1
  371. package/dist/semantics/normalize/structural-self-reference.js +23 -2
  372. package/dist/semantics/normalize/structural.d.ts +12 -1
  373. package/dist/semantics/normalize/structural.js +1420 -53
  374. package/dist/semantics/normalize/suppressed-write-arms.d.ts +47 -0
  375. package/dist/semantics/normalize/suppressed-write-arms.js +393 -0
  376. package/dist/semantics/normalize/unresolvable-names.d.ts +15 -2
  377. package/dist/semantics/normalize/unresolvable-names.js +30 -6
  378. package/dist/semantics/normalize/unshared-array-result.d.ts +32 -0
  379. package/dist/semantics/normalize/unshared-array-result.js +125 -0
  380. package/dist/semantics/package-sources.d.ts +1 -1
  381. package/dist/semantics/package-sources.js +120 -8
  382. package/dist/semantics/program.d.ts +20 -0
  383. package/dist/semantics/program.js +145 -14
  384. package/dist/semantics/stable-checker.d.ts +1 -1
  385. package/dist/semantics/stable-checker.js +7 -2
  386. package/dist/semantics/unchecked-guard-argument-copies.d.ts +67 -0
  387. package/dist/semantics/unchecked-guard-argument-copies.js +163 -0
  388. package/dist/semantics/unchecked-write-member-declarations.d.ts +46 -0
  389. package/dist/semantics/unchecked-write-member-declarations.js +331 -0
  390. package/dist/targets/cpp/balanced-units.d.ts +31 -0
  391. package/dist/targets/cpp/balanced-units.js +57 -0
  392. package/dist/targets/cpp/borrowed-arm-projections.d.ts +31 -0
  393. package/dist/targets/cpp/borrowed-arm-projections.js +106 -0
  394. package/dist/targets/cpp/borrowed-call-entry.d.ts +1 -1
  395. package/dist/targets/cpp/borrowed-call-entry.js +24 -20
  396. package/dist/targets/cpp/captures.js +314 -8
  397. package/dist/targets/cpp/char-code-buffers.d.ts +36 -0
  398. package/dist/targets/cpp/char-code-buffers.js +214 -0
  399. package/dist/targets/cpp/class-layout.d.ts +7 -1
  400. package/dist/targets/cpp/class-layout.js +42 -17
  401. package/dist/targets/cpp/class-properties/computed-method-value.d.ts +17 -2
  402. package/dist/targets/cpp/class-properties/computed-method-value.js +50 -4
  403. package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +31 -1
  404. package/dist/targets/cpp/class-properties/emit-class-properties.js +174 -17
  405. package/dist/targets/cpp/class-ref-transport.d.ts +32 -0
  406. package/dist/targets/cpp/class-ref-transport.js +56 -1
  407. package/dist/targets/cpp/conversions.js +1827 -1502
  408. package/dist/targets/cpp/coroutine-bodies.d.ts +97 -0
  409. package/dist/targets/cpp/coroutine-bodies.js +187 -0
  410. package/dist/targets/cpp/document.js +11 -1
  411. package/dist/targets/cpp/emit-allocation.js +535 -21
  412. package/dist/targets/cpp/emit-arrays.d.ts +10 -0
  413. package/dist/targets/cpp/emit-arrays.js +147 -16
  414. package/dist/targets/cpp/emit-binding-reference.d.ts +26 -2
  415. package/dist/targets/cpp/emit-binding-reference.js +97 -5
  416. package/dist/targets/cpp/emit-bindings.d.ts +9 -1
  417. package/dist/targets/cpp/emit-bindings.js +108 -8
  418. package/dist/targets/cpp/emit-buffers.d.ts +2 -16
  419. package/dist/targets/cpp/emit-buffers.js +124 -25
  420. package/dist/targets/cpp/emit-callable.d.ts +35 -72
  421. package/dist/targets/cpp/emit-callable.js +647 -82
  422. package/dist/targets/cpp/emit-carrier-members.d.ts +13 -2
  423. package/dist/targets/cpp/emit-carrier-members.js +50 -9
  424. package/dist/targets/cpp/emit-constructor-identity-family.d.ts +20 -0
  425. package/dist/targets/cpp/emit-constructor-identity-family.js +45 -0
  426. package/dist/targets/cpp/emit-context.d.ts +329 -15
  427. package/dist/targets/cpp/emit-context.js +104 -13
  428. package/dist/targets/cpp/emit-dynamic-properties.d.ts +44 -0
  429. package/dist/targets/cpp/emit-dynamic-properties.js +256 -37
  430. package/dist/targets/cpp/emit-equality.js +117 -2
  431. package/dist/targets/cpp/emit-error-constructor.d.ts +19 -0
  432. package/dist/targets/cpp/emit-error-constructor.js +73 -0
  433. package/dist/targets/cpp/emit-exceptions.d.ts +25 -3
  434. package/dist/targets/cpp/emit-exceptions.js +215 -26
  435. package/dist/targets/cpp/emit-in.js +52 -5
  436. package/dist/targets/cpp/emit-instanceof.js +59 -11
  437. package/dist/targets/cpp/emit-integers.d.ts +6 -0
  438. package/dist/targets/cpp/emit-integers.js +10 -0
  439. package/dist/targets/cpp/emit-iterable-object-view.d.ts +15 -0
  440. package/dist/targets/cpp/emit-iterable-object-view.js +51 -0
  441. package/dist/targets/cpp/emit-iterator-object-view.d.ts +15 -0
  442. package/dist/targets/cpp/emit-iterator-object-view.js +125 -0
  443. package/dist/targets/cpp/emit-iterator.d.ts +7 -10
  444. package/dist/targets/cpp/emit-iterator.js +361 -24
  445. package/dist/targets/cpp/emit-json.d.ts +7 -1
  446. package/dist/targets/cpp/emit-json.js +595 -55
  447. package/dist/targets/cpp/emit-jsx.d.ts +0 -37
  448. package/dist/targets/cpp/emit-jsx.js +70 -8
  449. package/dist/targets/cpp/emit-narrowing.d.ts +192 -28
  450. package/dist/targets/cpp/emit-narrowing.js +1595 -120
  451. package/dist/targets/cpp/emit-native-selection.js +8 -2
  452. package/dist/targets/cpp/emit-presence.js +13 -0
  453. package/dist/targets/cpp/emit-properties.d.ts +3 -1
  454. package/dist/targets/cpp/emit-properties.js +303 -54
  455. package/dist/targets/cpp/emit-protocol-iterator.d.ts +14 -0
  456. package/dist/targets/cpp/emit-protocol-iterator.js +203 -0
  457. package/dist/targets/cpp/emit-proxy.d.ts +17 -0
  458. package/dist/targets/cpp/emit-proxy.js +35 -0
  459. package/dist/targets/cpp/emit-record-view.d.ts +12 -1
  460. package/dist/targets/cpp/emit-record-view.js +429 -18
  461. package/dist/targets/cpp/emit-return.d.ts +38 -0
  462. package/dist/targets/cpp/emit-return.js +143 -21
  463. package/dist/targets/cpp/emit-scopes.d.ts +78 -0
  464. package/dist/targets/cpp/emit-scopes.js +196 -0
  465. package/dist/targets/cpp/emit-sum-widening.js +6 -3
  466. package/dist/targets/cpp/emit-tonumber.d.ts +40 -1
  467. package/dist/targets/cpp/emit-tonumber.js +75 -4
  468. package/dist/targets/cpp/emit-tostring.d.ts +28 -2
  469. package/dist/targets/cpp/emit-tostring.js +115 -12
  470. package/dist/targets/cpp/emit-typeof.js +2 -2
  471. package/dist/targets/cpp/emit-union-properties.d.ts +9 -0
  472. package/dist/targets/cpp/emit-union-properties.js +375 -46
  473. package/dist/targets/cpp/emit.d.ts +3 -1
  474. package/dist/targets/cpp/emit.js +725 -90
  475. package/dist/targets/cpp/evaluated-once.d.ts +51 -0
  476. package/dist/targets/cpp/evaluated-once.js +140 -0
  477. package/dist/targets/cpp/host/core-globals.d.ts +0 -21
  478. package/dist/targets/cpp/host/core-globals.js +10 -2
  479. package/dist/targets/cpp/host/emit-host-invoke.d.ts +0 -10
  480. package/dist/targets/cpp/host/emit-host-invoke.js +454 -36
  481. package/dist/targets/cpp/host/emit-host-object.js +733 -88
  482. package/dist/targets/cpp/host/emit-host-properties.js +26 -2
  483. package/dist/targets/cpp/host/emit-host-reflect.d.ts +1 -0
  484. package/dist/targets/cpp/host/emit-host-reflect.js +26 -7
  485. package/dist/targets/cpp/host/emit-host-value.d.ts +13 -0
  486. package/dist/targets/cpp/host/emit-host-value.js +47 -3
  487. package/dist/targets/cpp/host/host-members.d.ts +11 -0
  488. package/dist/targets/cpp/host/host-members.js +50 -4
  489. package/dist/targets/cpp/host/native-protocols.js +3 -0
  490. package/dist/targets/cpp/host/object-protocol.d.ts +8 -0
  491. package/dist/targets/cpp/host/object-protocol.js +55 -13
  492. package/dist/targets/cpp/key-order-tracking.d.ts +11 -0
  493. package/dist/targets/cpp/key-order-tracking.js +18 -0
  494. package/dist/targets/cpp/manifest/capabilities.js +107 -8
  495. package/dist/targets/cpp/manifest.js +14 -0
  496. package/dist/targets/cpp/native-collection-base.d.ts +27 -0
  497. package/dist/targets/cpp/native-collection-base.js +202 -0
  498. package/dist/targets/cpp/native-error-base.d.ts +14 -0
  499. package/dist/targets/cpp/native-error-base.js +19 -4
  500. package/dist/targets/cpp/native-promise-base.d.ts +23 -0
  501. package/dist/targets/cpp/native-promise-base.js +30 -0
  502. package/dist/targets/cpp/native-symbol-keys.js +1 -1
  503. package/dist/targets/cpp/prototype/emit-prototype-array.js +258 -20
  504. package/dist/targets/cpp/prototype/emit-prototype-date.js +5 -4
  505. package/dist/targets/cpp/prototype/emit-prototype-invoke.d.ts +9 -1
  506. package/dist/targets/cpp/prototype/emit-prototype-invoke.js +232 -26
  507. package/dist/targets/cpp/prototype/emit-prototype-iterator.d.ts +1 -1
  508. package/dist/targets/cpp/prototype/emit-prototype-iterator.js +111 -13
  509. package/dist/targets/cpp/prototype/emit-prototype-promise.d.ts +33 -4
  510. package/dist/targets/cpp/prototype/emit-prototype-promise.js +218 -10
  511. package/dist/targets/cpp/prototype/emit-prototype-regexp.js +8 -5
  512. package/dist/targets/cpp/prototype/emit-prototype-string.js +24 -7
  513. package/dist/targets/cpp/prototype/prototype-method-reads.js +18 -3
  514. package/dist/targets/cpp/reactive-dependencies.d.ts +2 -0
  515. package/dist/targets/cpp/reactive-dependencies.js +94 -3
  516. package/dist/targets/cpp/realm-storage.d.ts +5 -0
  517. package/dist/targets/cpp/realm-storage.js +12 -0
  518. package/dist/targets/cpp/records.d.ts +151 -1
  519. package/dist/targets/cpp/records.js +1321 -122
  520. package/dist/targets/cpp/recursive-containers.d.ts +4 -1
  521. package/dist/targets/cpp/recursive-containers.js +28 -9
  522. package/dist/targets/cpp/translation-unit.d.ts +10 -0
  523. package/dist/targets/cpp/translation-unit.js +686 -140
  524. package/dist/targets/cpp/types.d.ts +56 -6
  525. package/dist/targets/cpp/types.js +218 -12
  526. package/dist/targets/cpp/virtual-methods.d.ts +3 -3
  527. package/dist/targets/cpp/virtual-methods.js +174 -17
  528. package/package.json +6 -5
  529. package/src/targets/cpp/runtime/gea_dynamic_proxy.h +155 -25
  530. package/src/targets/cpp/runtime/gea_eval.h +3 -0
  531. package/src/targets/cpp/runtime/gea_pcm.h +116 -0
  532. package/src/targets/cpp/runtime/gea_runtime.h +18096 -4876
  533. package/src/targets/cpp/runtime/gea_runtime_builtins.cpp +7 -45
  534. package/src/targets/cpp/runtime/gea_runtime_rtc.h +247 -0
@@ -1,15 +1,19 @@
1
1
  import ts from 'typescript';
2
2
  import { genericFunctionChoiceMembersOf } from './generic-function-choice.js';
3
- import { layoutRelevantParameterIndices, parametersInInstanceStorage } from './structural-layout-relevance.js';
3
+ import { layoutRelevantParameterIndices, parametersInInstanceStorage, storedDataDiffers } from './structural-layout-relevance.js';
4
4
  import { isAmbientDeclaration } from '../ambient.js';
5
+ import { mergedDeclarationOf } from './merged-declaration.js';
5
6
  export const emptySpecializationCensus = {
6
7
  specializationsOf: () => [],
7
8
  isGeneric: () => false,
8
9
  specializationAt: () => null,
10
+ erasedAnyRefilledAt: () => false,
9
11
  setSpecializationsAt: () => null,
10
12
  specializationOfInstance: () => null,
11
13
  copiesMayDifferInLayout: () => false,
12
- canonicalLayoutFillings: () => null
14
+ canonicalLayoutFillings: () => null,
15
+ reinterpretedCopyOf: () => null,
16
+ admittedUnder: () => true
13
17
  };
14
18
  const typeParametersOf = (declaration) => {
15
19
  const withParameters = declaration;
@@ -83,13 +87,13 @@ const isHole = (type) => (type.flags & ts.TypeFlags.TypeParameter) !== 0;
83
87
  * from a call the checker itself already resolved -- is recorded from that
84
88
  * call, not from this reference.
85
89
  */
86
- const containsHole = (type, depth = 0) => {
90
+ const containsHoleIn = (checker, type, depth = 0) => {
87
91
  if (isHole(type))
88
92
  return true;
89
93
  if (depth > 6)
90
94
  return false;
91
95
  if (type.isUnionOrIntersection())
92
- return type.types.some((one) => containsHole(one, depth + 1));
96
+ return type.types.some((one) => containsHoleIn(checker, one, depth + 1));
93
97
  // BOTH argument lists, always. An instantiated ANONYMOUS type -- a mapped
94
98
  // alias like tsc's `Mutable<T>`, an inline object or function type -- has a
95
99
  // `target` (the open anonymous type it was instantiated from) and NO
@@ -101,7 +105,82 @@ const containsHole = (type, depth = 0) => {
101
105
  // was the constraint image while the call wanted the instantiation.
102
106
  const reference = type;
103
107
  const args = [...(reference.typeArguments ?? []), ...(type.aliasTypeArguments ?? [])];
104
- return args.some((one) => containsHole(one, depth + 1));
108
+ if (args.some((one) => containsHoleIn(checker, one, depth + 1)))
109
+ return true;
110
+ return literalMembersHoldHole(checker, type);
111
+ };
112
+ /**
113
+ * Whether a written object LITERAL type keeps a hole in one of its PROPERTIES.
114
+ *
115
+ * `new Box<{ v: T }>()` inside `Holder<T>` fills `Box` with a type that has
116
+ * no type arguments at all -- its hole sits in the property `v` -- so the
117
+ * argument walk above read it as closed, and one OPEN copy `Box<{ v: T }>` was
118
+ * minted: its `items: T[]` reached representation with no carrier. The site is
119
+ * open exactly like `Box<T>` would be, and each copy of `Holder` closes it
120
+ * through `filledBy`.
121
+ *
122
+ * Only an anonymous type-literal or object-literal type is walked, and only its
123
+ * properties. A function type's hole sits in a signature, which this module
124
+ * reads nowhere else either, and a class or interface instance's members are
125
+ * that declaration's own business: its arguments already said whether it is
126
+ * open. Remembered per type, because the census asks it of the same fillings on
127
+ * every round of the fixpoint, and a literal that mentions itself answers
128
+ * `false` for the cycle rather than recursing.
129
+ */
130
+ const literalHoles = new WeakMap();
131
+ const isObjectLiteralType = (type) => (type.flags & ts.TypeFlags.Object) !== 0 &&
132
+ (type.objectFlags & ts.ObjectFlags.Anonymous) !== 0 &&
133
+ ((type.getSymbol()?.flags ?? 0) & (ts.SymbolFlags.TypeLiteral | ts.SymbolFlags.ObjectLiteral)) !== 0;
134
+ const literalMembersHoldHole = (checker, type) => {
135
+ if (!isObjectLiteralType(type))
136
+ return false;
137
+ const known = literalHoles.get(type);
138
+ if (known !== undefined)
139
+ return known;
140
+ literalHoles.set(type, false);
141
+ const answer = checker.getPropertiesOfType(type).some((property) => containsHoleIn(checker, checker.getTypeOfSymbol(property), 1));
142
+ literalHoles.set(type, answer);
143
+ return answer;
144
+ };
145
+ /**
146
+ * Whether two OPEN object literal types are one type written twice.
147
+ *
148
+ * Every `{ v: T }` the program writes is its own anonymous type, so the one at
149
+ * `new Box<{ v: T }>()` and the one in `make(): Box<{ v: T }>` are different
150
+ * checker objects even though they are the same type -- and the copy's closed
151
+ * image of the second (`{ v: number }`, read off the copy's instantiated
152
+ * members) is then unreachable from the first by identity. Two literals with
153
+ * the same property names, the same optionality and readonly-ness, and
154
+ * property types that are the SAME type objects (or literals equal by this
155
+ * same rule) are instantiated by any one mapping into equal types, so the
156
+ * image of one is an image of the other.
157
+ *
158
+ * Exact, not assignability: `{ v: T }` and `{ v: T; w?: T }` differ in a
159
+ * property and are never confused, and any signature or index signature on
160
+ * either side declines, because this does not compare those.
161
+ */
162
+ const sameOpenLiteral = (checker, left, right, depth = 0) => {
163
+ if (left === right)
164
+ return true;
165
+ if (depth > 4 || !isObjectLiteralType(left) || !isObjectLiteralType(right))
166
+ return false;
167
+ const shapeless = (type) => type.getCallSignatures().length === 0 && type.getConstructSignatures().length === 0 && checker.getIndexInfosOfType(type).length === 0;
168
+ if (!shapeless(left) || !shapeless(right))
169
+ return false;
170
+ const leftProperties = checker.getPropertiesOfType(left);
171
+ const rightProperties = checker.getPropertiesOfType(right);
172
+ if (leftProperties.length !== rightProperties.length)
173
+ return false;
174
+ const modifiersOf = (property) => {
175
+ const at = property.valueDeclaration ?? property.declarations?.[0];
176
+ return (property.flags & ts.SymbolFlags.Optional) | (at ? ts.getCombinedModifierFlags(at) & ts.ModifierFlags.Readonly : 0);
177
+ };
178
+ return leftProperties.every((property) => {
179
+ const other = rightProperties.find((candidate) => candidate.escapedName === property.escapedName);
180
+ return (other !== undefined &&
181
+ modifiersOf(property) === modifiersOf(other) &&
182
+ sameOpenLiteral(checker, checker.getTypeOfSymbol(property), checker.getTypeOfSymbol(other), depth + 1));
183
+ });
105
184
  };
106
185
  /**
107
186
  * The declaration whose body a set of type parameters belongs to.
@@ -149,6 +228,12 @@ const openFilling = (type, depth = 0) => {
149
228
  const reference = type;
150
229
  return [...(reference.typeArguments ?? []), ...(type.aliasTypeArguments ?? [])].some((one) => openFilling(one, depth + 1));
151
230
  };
231
+ /** A type parameter's declaration, the class's where a class and interface merge -- see `mergedDeclarationOf`. */
232
+ const typeParameterDeclarationOf = (parameter) => {
233
+ const symbol = parameter?.getSymbol();
234
+ const declaration = symbol ? mergedDeclarationOf(symbol) : null;
235
+ return declaration && ts.isTypeParameterDeclaration(declaration) ? declaration : undefined;
236
+ };
152
237
  const ownerOf = (parameter) => {
153
238
  const parent = parameter?.parent;
154
239
  if (!parent)
@@ -202,7 +287,7 @@ const unify = (checker, generic, concrete, depth, record, recordPair = null) =>
202
287
  }
203
288
  // A composite with a hole inside, beside the closed type the checker built
204
289
  // for it: the pair a copy answers `fillingOf` from.
205
- if (recordPair && containsHole(generic))
290
+ if (recordPair && containsHoleIn(checker, generic))
206
291
  recordPair(generic, concrete);
207
292
  // An OPTIONALITY MARKER is not content. `status?: U` is read as `U |
208
293
  // undefined` and its instantiation as `ContentfulStatusCode | undefined`:
@@ -326,6 +411,15 @@ const unifySignatures = (checker, generics, concretes, depth, record, recordPair
326
411
  if (generics.length !== 1 || concretes.length !== 1 || !generic || !concrete)
327
412
  return;
328
413
  unify(checker, generic.getReturnType(), concrete.getReturnType(), depth + 1, record, recordPair);
414
+ // A type guard's predicate is part of its return: `getReturnType` answers
415
+ // `boolean` and drops `value is readonly T[]`. mongodb's
416
+ // `isReadonlyArray<T>(value: any): value is readonly T[]` mentions `T`
417
+ // nowhere else, so the call bound nothing, minted no copy, and the callee
418
+ // stayed the generic's uninstantiated set.
419
+ const genericPredicate = checker.getTypePredicateOfSignature(generic)?.type;
420
+ const concretePredicate = checker.getTypePredicateOfSignature(concrete)?.type;
421
+ if (genericPredicate && concretePredicate)
422
+ unify(checker, genericPredicate, concretePredicate, depth + 1, record, recordPair);
329
423
  // `this` IS a parameter -- ECMA-262 gives it a binding in the function
330
424
  // environment like any other -- and it is the only one `Signature.parameters`
331
425
  // leaves out. A generic whose type parameter appears ONLY there was therefore
@@ -379,6 +473,7 @@ const typeOfParameter = (checker, parameter) => {
379
473
  // verified by reading `frontend.ts`'s call order. See the matching note in
380
474
  // `instantiation.ts`.
381
475
  export const censusSpecializations = (checker, sourceFiles, reachable, namespacePaths) => {
476
+ const containsHole = (type, depth = 0) => containsHoleIn(checker, type, depth);
382
477
  // Per generic declaration: the instantiations seen so far, each a tuple of
383
478
  // the types filling its parameters, in declaration order. Tuples are compared
384
479
  // by the identity of the types in them -- the checker interns types, so two
@@ -457,6 +552,21 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
457
552
  // exact tuple somewhere the program never instantiated from; see
458
553
  // `deferredSpellings`.
459
554
  const spelling = instantiated ?? deferredSpellingFor(declaration, tuple);
555
+ // A copy's SPELLING carries the member images too, however the copy was
556
+ // minted. A copy minted by substitution -- `new Collection<TSchema>(...)`
557
+ // inside `Db.collection<TSchema>`, closed per copy of the method -- gets
558
+ // its spelling from `deferredSpellings` (the call's resolved return type)
559
+ // and nothing else, so reading the member images only where a written
560
+ // reference passed them in left every such copy without `WithId<DataKey>`
561
+ // for the `new FindCursor<WithId<TSchema>>(...)` its `find` builds.
562
+ const spellingImages = spelling ? fillingsOfSpelling(declaration, spelling) : null;
563
+ if (spellingImages && spellingImages !== fillings) {
564
+ const merged = new Map(fillings ?? []);
565
+ for (const [open, closed] of spellingImages)
566
+ if (!merged.has(open))
567
+ merged.set(open, closed);
568
+ fillings = merged;
569
+ }
460
570
  if (existing >= 0) {
461
571
  if (spelling && !spelled[existing]) {
462
572
  spelled[existing] = spelling;
@@ -466,8 +576,18 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
466
576
  paired[existing] = signatures;
467
577
  signaturePairs.set(declaration, paired);
468
578
  }
469
- if (fillings && !filled[existing]) {
470
- filled[existing] = fillings;
579
+ // MERGED, not first-wins. `new Holder(1)` is seen twice at one node: as a
580
+ // call, whose constructor signature pairs no composite at all (an EMPTY
581
+ // table), and as a written instance type, whose members pair
582
+ // `Box<{ v: T }>` with `Box<{ v: number }>`. Keeping only the first
583
+ // left the copy with no image for the composite its own body builds.
584
+ const held = filled[existing];
585
+ if (fillings && fillings !== held && [...fillings.keys()].some((open) => !held?.has(open))) {
586
+ const merged = new Map(held ?? []);
587
+ for (const [open, closed] of fillings)
588
+ if (!merged.has(open))
589
+ merged.set(open, closed);
590
+ filled[existing] = merged;
471
591
  compositeFillings.set(declaration, filled);
472
592
  }
473
593
  if (constructs && !minted[existing]) {
@@ -511,6 +631,43 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
511
631
  const found = seen.findIndex((candidate) => candidate.length === args.length && candidate.every((t, index) => t === args[index]));
512
632
  return found >= 0 ? found : null;
513
633
  };
634
+ /**
635
+ * What one copy's composite fillings close `open` to: the copy's own entry
636
+ * for that very type, or else the entry for an object literal type that is
637
+ * the same type written elsewhere (`sameOpenLiteral`).
638
+ *
639
+ * The census's `filledBy` and every reader's `fillingOf` both answer through
640
+ * here, so a site and the copy it names cannot disagree over which closed
641
+ * object stands for `{ v: T }` in that copy -- the tuple a copy is minted
642
+ * with and the tuple a reader looks up are the same objects. When several
643
+ * written literals match, the first recorded wins, the same rule for both.
644
+ */
645
+ const literalImages = new WeakMap();
646
+ const compositeImageOf = (declaration, ordinal, open) => {
647
+ const table = compositeFillings.get(declaration)?.[ordinal];
648
+ if (!table)
649
+ return null;
650
+ const direct = table.get(open);
651
+ if (direct !== undefined)
652
+ return direct;
653
+ if (!isObjectLiteralType(open) || !containsHole(open))
654
+ return null;
655
+ let known = literalImages.get(table);
656
+ if (!known)
657
+ literalImages.set(table, (known = new Map()));
658
+ const remembered = known.get(open);
659
+ if (remembered !== undefined)
660
+ return remembered;
661
+ let answer = null;
662
+ for (const [written, closed] of table) {
663
+ if (written !== open && sameOpenLiteral(checker, written, open)) {
664
+ answer = closed;
665
+ break;
666
+ }
667
+ }
668
+ known.set(open, answer);
669
+ return answer;
670
+ };
514
671
  /**
515
672
  * The instantiations one recorded instantiation induces through its heritage.
516
673
  *
@@ -541,7 +698,7 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
541
698
  const args = reference.typeArguments;
542
699
  if (!parameters || !args)
543
700
  return;
544
- const owner = ownerOf(parameters[0]?.getSymbol()?.declarations?.[0]);
701
+ const owner = ownerOf(typeParameterDeclarationOf(parameters[0]));
545
702
  if (!owner)
546
703
  return;
547
704
  const induced = new Map();
@@ -558,6 +715,39 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
558
715
  }
559
716
  recordTuple(owner, induced, null, depth + 1);
560
717
  };
718
+ /**
719
+ * Whether the copies of a class extend DIFFERENT copies of a base that
720
+ * itself may split. Each copy's base arguments are its own fillings
721
+ * substituted into the `extends` clause, exactly as `induceFromHeritage`
722
+ * substitutes them; a clause whose argument keeps a hole nested inside it
723
+ * names no tuple, and answers "no" rather than guessing one.
724
+ */
725
+ const baseCopiesDiffer = (declaration, copies) => {
726
+ const base = declaration.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
727
+ if (!base)
728
+ return false;
729
+ const reference = checker.getTypeAtLocation(base);
730
+ const parameters = reference.target?.typeParameters;
731
+ const args = reference.typeArguments;
732
+ if (!parameters || !args)
733
+ return false;
734
+ const owner = ownerOf(typeParameterDeclarationOf(parameters[0]));
735
+ if (!owner || !census.copiesMayDifferInLayout(owner))
736
+ return false;
737
+ const own = typeParametersOf(declaration).map((parameter) => checker.getSymbolAtLocation(parameter.name));
738
+ // A class reference's arguments end with its polymorphic `this`, which is
739
+ // no parameter of the base.
740
+ const baseTuples = copies.map((copy) => args.slice(0, parameters.length).map((filling) => {
741
+ if (!isHole(filling))
742
+ return containsHole(filling) ? null : filling;
743
+ const index = own.indexOf(filling.getSymbol());
744
+ return index < 0 ? null : (copy.arguments[index] ?? null);
745
+ }));
746
+ if (baseTuples.some((tuple) => tuple.some((filling) => filling === null)))
747
+ return false;
748
+ const [first, ...rest] = baseTuples;
749
+ return first !== undefined && rest.some((tuple) => tuple.some((filling, index) => filling !== first[index]));
750
+ };
561
751
  /** Record one instantiation, given the owner and the fillings keyed by parameter symbol. */
562
752
  const recordTuple = (declaration, bound, at, depth = 0, instantiated = null, signatures = null, spellingOnly = false, fillings = null, collect = null, constructs = true) => {
563
753
  const parameters = typeParametersOf(declaration);
@@ -677,9 +867,11 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
677
867
  * declaration the program never introduces.
678
868
  *
679
869
  * Used ONLY as the member walk's admission gate, where a wider answer can
680
- * only add pairs. `containsHole` itself stays as it is: it also gates whether
681
- * a TUPLE is concrete enough to mint a copy, and widening it there would
682
- * withhold copies that are minted today.
870
+ * only add pairs. `containsHole` itself does not follow signatures: it also
871
+ * gates whether a TUPLE is concrete enough to mint a copy, and widening it
872
+ * there would withhold copies that are minted today. (It does follow an
873
+ * object literal's properties -- see `literalMembersHoldHole` -- because a
874
+ * copy minted over `{ v: T }` is exactly the open copy it exists to refuse.)
683
875
  */
684
876
  const mentionsHoleThroughSignatures = (type, depth = 0) => {
685
877
  if (containsHole(type, depth))
@@ -718,15 +910,52 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
718
910
  const openType = checker.getTypeOfSymbol(open);
719
911
  if (!mentionsHoleThroughSignatures(openType))
720
912
  continue;
721
- unify(checker, openType, checker.getTypeOfSymbol(property), 0, () => undefined, (composite, closed) => {
913
+ const closedType = checker.getTypeOfSymbol(property);
914
+ const recordPair = (composite, closed) => {
722
915
  if (!pairs.has(composite))
723
916
  pairs.set(composite, closed);
724
- });
917
+ };
918
+ unify(checker, openType, closedType, 0, () => undefined, recordPair);
919
+ // An OVERLOADED member, signature by signature. `unifySignatures`
920
+ // declines a list of several because, against a type INFERRED from a
921
+ // call, nothing orders the two lists alike. Here nothing needs to: both
922
+ // sides are one member symbol, read off the generic and off its
923
+ // instantiation, so signature `i` is the image of signature `i` (the
924
+ // pairing `structural-instantiated-member.ts` relies on for the same
925
+ // reason). mongodb's `Collection.find` is the case: its three overloads
926
+ // were the only place `FindCursor<WithId<TSchema>>` was spelled, so no
927
+ // copy of `Collection` ever had the image `WithId<DataKey>` that the
928
+ // `new FindCursor<WithId<TSchema>>(...)` in its body needs, and
929
+ // `FindCursor` was minted no copy at all.
930
+ for (const [openList, closedList] of [
931
+ [openType.getCallSignatures(), closedType.getCallSignatures()],
932
+ [openType.getConstructSignatures(), closedType.getConstructSignatures()]
933
+ ]) {
934
+ if (openList.length < 2 || openList.length !== closedList.length)
935
+ continue;
936
+ openList.forEach((signature, index) => {
937
+ const image = closedList[index];
938
+ if (image)
939
+ unifySignatures(checker, [signature], [image], 0, () => undefined, recordPair);
940
+ });
941
+ }
725
942
  }
726
943
  const answer = pairs.size > 0 ? pairs : null;
727
944
  memberFillings.set(reference, answer);
728
945
  return answer;
729
946
  };
947
+ /** `memberFillingsOf` for a copy's spelling, when the spelling is a closed instantiation of a generic class. */
948
+ const fillingsOfSpelling = (declaration, spelling) => {
949
+ const reference = spelling;
950
+ const target = reference.target;
951
+ const parameters = target?.typeParameters;
952
+ const args = reference.typeArguments;
953
+ if (!target || !parameters || !args)
954
+ return null;
955
+ if (!args.some((argument, index) => argument !== parameters[index]) || args.some((argument) => containsHole(argument)))
956
+ return null;
957
+ return memberFillingsOf(declaration, target, reference);
958
+ };
730
959
  /**
731
960
  * A type reference the program writes -- or, with `node` null, one it makes
732
961
  * without writing (a call's own resolved return type).
@@ -749,7 +978,7 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
749
978
  const args = reference.typeArguments;
750
979
  if (!target || !parameters || !args)
751
980
  return;
752
- const owner = ownerOf(parameters[0]?.getSymbol()?.declarations?.[0]);
981
+ const owner = ownerOf(typeParameterDeclarationOf(parameters[0]));
753
982
  if (!owner)
754
983
  return;
755
984
  const bound = new Map();
@@ -913,6 +1142,105 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
913
1142
  * call: module-level function declarations with a body, at least two.
914
1143
  */
915
1144
  const genericSetMembersOf = (node) => ts.isCallExpression(node) ? genericFunctionChoiceMembersOf(checker, node.expression) : null;
1145
+ /**
1146
+ * The type parameters a call filled with `any` ONLY because the argument
1147
+ * that binds them was erased with `as any`, refilled with the type of the
1148
+ * value the assertion wraps.
1149
+ *
1150
+ * mongodb's `Collection.insertOne` is `executeOperation(this.client, new
1151
+ * InsertOneOperation(...) as TODO_NODE_3286)` (`TODO_NODE_3286 = any`). The
1152
+ * checker infers `T = any` from the erased argument, so the copy that call
1153
+ * mints takes `operation: any` and every read of the operation in the copy
1154
+ * -- and in the `tryOperation` copy it calls -- is a box of a value whose
1155
+ * class the program states one token earlier. An assertion is erased at
1156
+ * runtime (`producers/erasure.ts`): the argument the copy receives IS the
1157
+ * `InsertOneOperation`, so a copy over that type receives exactly what
1158
+ * arrives.
1159
+ *
1160
+ * Sound only where the erased argument is the SOLE source of the filling:
1161
+ * every parameter whose declared type mentions the type parameter must be
1162
+ * the bare parameter itself, with an `as any` argument whose operand has one
1163
+ * static type that meets the constraint. A parameter that mentions it inside
1164
+ * a composite (`items: T[]`, `(value: T) => void`) was checked against `any`
1165
+ * and could hold anything, so the whole call keeps the checker's answer.
1166
+ */
1167
+ const erasedAnyRefilledSites = new Set();
1168
+ const erasedAnyFillingsOf = (node, open, bound) => {
1169
+ if (!ts.isCallExpression(node) && !ts.isNewExpression(node))
1170
+ return null;
1171
+ const args = node.arguments ?? [];
1172
+ // A spread argument no longer lines up with a parameter index.
1173
+ if (args.some((argument) => ts.isSpreadElement(argument)))
1174
+ return null;
1175
+ const refilled = new Map();
1176
+ for (const parameter of open.getTypeParameters() ?? []) {
1177
+ const symbol = parameter.getSymbol();
1178
+ const filling = symbol ? bound.get(symbol) : undefined;
1179
+ if (!symbol || !filling || (filling.flags & ts.TypeFlags.Any) === 0)
1180
+ continue;
1181
+ let operandType = null;
1182
+ let sole = true;
1183
+ for (const [index, formal] of open.getParameters().entries()) {
1184
+ const declared = typeOfParameter(checker, formal);
1185
+ if (!declared)
1186
+ return null;
1187
+ if (declared !== parameter) {
1188
+ // Conservative: a composite that could mention the parameter is a
1189
+ // second source this call checked against `any`.
1190
+ if (containsHole(declared))
1191
+ sole = false;
1192
+ continue;
1193
+ }
1194
+ const declaration = formal.valueDeclaration;
1195
+ if (declaration && ts.isParameter(declaration) && declaration.dotDotDotToken)
1196
+ return null;
1197
+ const argument = args[index];
1198
+ if (!argument)
1199
+ continue;
1200
+ const operand = erasedAnyOperandOf(argument);
1201
+ if (operand === null) {
1202
+ sole = false;
1203
+ continue;
1204
+ }
1205
+ const type = checker.getTypeAtLocation(operand);
1206
+ if ((type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0) {
1207
+ sole = false;
1208
+ continue;
1209
+ }
1210
+ if (operandType !== null && operandType !== type)
1211
+ sole = false;
1212
+ operandType = type;
1213
+ }
1214
+ if (!sole || operandType === null)
1215
+ continue;
1216
+ const constraint = checker.getBaseConstraintOfType(parameter);
1217
+ if (constraint && (constraint.flags & ts.TypeFlags.Unknown) === 0 && !checker.isTypeAssignableTo(operandType, constraint))
1218
+ continue;
1219
+ refilled.set(symbol, operandType);
1220
+ }
1221
+ return refilled.size > 0 ? refilled : null;
1222
+ };
1223
+ /** The operand of an `x as any` / `<any>x` argument (through parentheses and `!`), or `null`. */
1224
+ const erasedAnyOperandOf = (argument) => {
1225
+ let current = argument;
1226
+ let erasedToAny = false;
1227
+ for (;;) {
1228
+ if (ts.isParenthesizedExpression(current) || ts.isNonNullExpression(current)) {
1229
+ current = current.expression;
1230
+ continue;
1231
+ }
1232
+ if (ts.isAsExpression(current) || ts.isTypeAssertionExpression(current)) {
1233
+ const target = checker.getTypeFromTypeNode(current.type);
1234
+ if ((target.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) === 0)
1235
+ return null;
1236
+ if ((target.flags & ts.TypeFlags.Any) !== 0)
1237
+ erasedToAny = true;
1238
+ current = current.expression;
1239
+ continue;
1240
+ }
1241
+ return erasedToAny ? current : null;
1242
+ }
1243
+ };
916
1244
  const fromCall = (node) => {
917
1245
  const resolved = checker.getResolvedSignature(node);
918
1246
  const setMembers = resolved ? genericSetMembersOf(node) : null;
@@ -946,13 +1274,22 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
946
1274
  return;
947
1275
  }
948
1276
  const generic = genericSignatureOf(node, resolved);
949
- if (!generic || !resolved || generic === resolved)
1277
+ // A NON-generic overload of a generic implementation still runs that
1278
+ // implementation at some filling: mongodb's `Db.listCollections(filter,
1279
+ // {nameOnly: false})` resolves to the overload returning
1280
+ // `ListCollectionsCursor<CollectionInfo>`, and the body builds `new
1281
+ // ListCollectionsCursor<T>(...)`. Unifying the implementation against the
1282
+ // overload binds `T` from the overload's stated result, so the copy the
1283
+ // body constructs is the copy the caller holds.
1284
+ const overload = !generic && resolved ? resolved.getDeclaration() : undefined;
1285
+ const viaOverload = overload !== undefined && implementationOf(overload) !== overload ? overload : undefined;
1286
+ if (!resolved || (viaOverload === undefined && (!generic || generic === resolved)))
950
1287
  return;
951
- const declared = generic.getDeclaration();
1288
+ const declared = viaOverload ?? generic?.getDeclaration();
952
1289
  if (!declared)
953
1290
  return;
954
1291
  const owner = implementationOf(declared);
955
- let open = generic;
1292
+ let open = generic ?? resolved;
956
1293
  if (owner !== declared) {
957
1294
  const implementation = checker.getSignatureFromDeclaration(owner);
958
1295
  // An implementation with no type parameters of its own is one function
@@ -971,6 +1308,36 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
971
1308
  if (!pairs.has(composite))
972
1309
  pairs.set(composite, closed);
973
1310
  });
1311
+ // A written type argument states its parameter outright, and is the only
1312
+ // statement of one that no parameter type unifies against: `emit<K>(...args:
1313
+ // Parameters<E[K]>)` called as `emit<'started'>(event)` pairs the deferred
1314
+ // conditional with a closed tuple, which binds nothing, and a call whose
1315
+ // tuple is incomplete mints no copy at all -- the method was then read off
1316
+ // the instance as a dynamic property and aborted. Only for the declaration
1317
+ // the call resolved to: an implementation behind overloads names its own
1318
+ // parameters.
1319
+ const written = 'typeArguments' in node ? node.typeArguments : undefined;
1320
+ if (written && owner === declared) {
1321
+ for (const [index, parameter] of (open.getTypeParameters() ?? []).entries()) {
1322
+ const argument = written[index];
1323
+ const symbol = parameter.getSymbol();
1324
+ if (argument && symbol && !bound.has(symbol))
1325
+ bound.set(symbol, checker.getTypeFromTypeNode(argument));
1326
+ }
1327
+ }
1328
+ const erased = owner === declared ? erasedAnyFillingsOf(node, open, bound) : null;
1329
+ if (erased !== null) {
1330
+ for (const [symbol, filling] of erased)
1331
+ bound.set(symbol, filling);
1332
+ erasedAnyRefilledSites.add(node);
1333
+ // The checker's resolved signature types every rebound position `any`;
1334
+ // pairing it with the copy would read those positions back as `any`.
1335
+ // The tuple is the whole statement, exactly as for an overload copy.
1336
+ recordTuple(owner, bound, node, 0, null, null, false, null);
1337
+ fromTypeReference(null, resolved.getReturnType(), true);
1338
+ fromImplementation(node, owner, bound);
1339
+ return;
1340
+ }
974
1341
  // No signature pair for a copy minted through an overload: the resolved
975
1342
  // signature is the OVERLOAD's frame instantiated, not the implementation's
976
1343
  // -- `some<T>(array, predicate?)` resolved through `some(array, predicate)`
@@ -1268,7 +1635,7 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1268
1635
  * instantiated exactly one way by the program -- the one case a hole a
1269
1636
  * type owns has a single answer. See `fromValueUse`.
1270
1637
  */
1271
- const holeOwnerOf = (hole) => ownerOf(hole.getSymbol()?.declarations?.[0]);
1638
+ const holeOwnerOf = (hole) => ownerOf(typeParameterDeclarationOf(hole));
1272
1639
  const uniquelyInstantiatedTypeOwnersOf = (fillings) => {
1273
1640
  const owners = [];
1274
1641
  for (const filling of fillings) {
@@ -1358,6 +1725,66 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1358
1725
  };
1359
1726
  for (const file of sourceFiles)
1360
1727
  declareVisit(file);
1728
+ /**
1729
+ * The generic classes the program REINTERPRETS: casts one of their
1730
+ * instances, through `unknown` or `any`, to another instantiation of the
1731
+ * same class hierarchy -- mongodb's `AbstractCursor.map` composing a
1732
+ * transform and returning `this as unknown as AbstractCursor<T>`.
1733
+ *
1734
+ * That is one object viewed at two instantiations, and the program has
1735
+ * declared the relationship UNCHECKED: the `transform` field typed
1736
+ * `(doc: TSchema) => any` keeps receiving the raw documents whatever
1737
+ * `TSchema` the current view claims, and a closure composed in one view
1738
+ * forwards a value of another view's type. No per-copy layout is honest
1739
+ * for such a class -- the object cannot be converted between copies
1740
+ * without losing identity, and any concrete filling is a claim the program
1741
+ * itself has said it does not keep. So every copy folds onto ONE layout
1742
+ * whose type-parameter-dependent storage is filled with `any`
1743
+ * (`reinterpretationTuples`), the filling TypeScript gives the unchecked
1744
+ * top; values crossing into it are checked on the way out, never trusted.
1745
+ */
1746
+ const reinterpretedClasses = new Set();
1747
+ const classOfInstance = (type) => {
1748
+ const thisClass = (type.flags & ts.TypeFlags.TypeParameter) !== 0 && type.getSymbol()?.declarations?.some((one) => ts.isClassLike(one)) === true;
1749
+ const instance = thisClass ? (checker.getBaseConstraintOfType(type) ?? type) : type;
1750
+ const symbol = instance.target?.getSymbol() ?? instance.getSymbol();
1751
+ const declaration = symbol ? mergedDeclarationOf(symbol) : null;
1752
+ return declaration && ts.isClassLike(declaration) ? declaration : null;
1753
+ };
1754
+ const baseClassOf = (declaration) => {
1755
+ const base = declaration.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
1756
+ return base ? classOfInstance(checker.getTypeAtLocation(base)) : null;
1757
+ };
1758
+ const ancestryOf = (declaration) => {
1759
+ const chain = [];
1760
+ for (let current = declaration; current && chain.length < 16; current = baseClassOf(current)) {
1761
+ if (chain.includes(current))
1762
+ break;
1763
+ chain.push(current);
1764
+ }
1765
+ return chain;
1766
+ };
1767
+ const noteReinterpretation = (node) => {
1768
+ let inner = node.expression;
1769
+ while (ts.isParenthesizedExpression(inner))
1770
+ inner = inner.expression;
1771
+ if (!ts.isAsExpression(inner) && !ts.isTypeAssertionExpression(inner))
1772
+ return;
1773
+ if ((checker.getTypeFromTypeNode(inner.type).flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) === 0)
1774
+ return;
1775
+ const target = checker.getTypeFromTypeNode(node.type);
1776
+ const source = checker.getTypeAtLocation(inner.expression);
1777
+ const targetClass = classOfInstance(target);
1778
+ const sourceClass = classOfInstance(source);
1779
+ if (!targetClass || !sourceClass || typeParametersOf(targetClass).length === 0)
1780
+ return;
1781
+ // Only a view of the SAME object: the target class is the source's own
1782
+ // class or one of its ancestors or descendants. A cast from an unrelated
1783
+ // value is a dynamic assertion, not a reinterpretation.
1784
+ if (!ancestryOf(sourceClass).includes(targetClass) && !ancestryOf(targetClass).includes(sourceClass))
1785
+ return;
1786
+ reinterpretedClasses.add(targetClass);
1787
+ };
1361
1788
  const visit = (node) => {
1362
1789
  // A PRUNED member is a site this compilation does not reach either, and
1363
1790
  // statement-level reachability cannot say so: a dead method lives inside a
@@ -1375,6 +1802,8 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1375
1802
  // probe's 516 mandatory obligations, all from one method nothing calls.
1376
1803
  if (reachable.memberIsPruned(node))
1377
1804
  return;
1805
+ if (ts.isAsExpression(node) || ts.isTypeAssertionExpression(node))
1806
+ noteReinterpretation(node);
1378
1807
  if (ts.isCallOrNewExpression(node) || ts.isTaggedTemplateExpression(node) || ts.isJsxOpeningLikeElement(node))
1379
1808
  fromCall(node);
1380
1809
  if (ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isIdentifier(node) || ts.isPropertyAccessExpression(node))
@@ -1389,6 +1818,162 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1389
1818
  for (const file of sourceFiles)
1390
1819
  for (const statement of reachable.statementsOf(file))
1391
1820
  visit(statement);
1821
+ // A reinterpreted object is a whole object, so the fold reaches its whole
1822
+ // hierarchy: the ancestors whose storage it carries, and the descendants
1823
+ // it may really be -- `FindCursor.map` returns `super.map(t) as
1824
+ // FindCursor<T>`, a checked downcast that holds only if the `FindCursor`
1825
+ // being viewed is the one physical class the `FindCursor<T>` view names.
1826
+ const reinterpretationFamily = new Set();
1827
+ for (const declaration of reinterpretedClasses)
1828
+ for (const one of ancestryOf(declaration))
1829
+ reinterpretationFamily.add(one);
1830
+ if (reinterpretationFamily.size > 0)
1831
+ for (const declaration of declared)
1832
+ if (ts.isClassLike(declaration) && ancestryOf(declaration).some((one) => reinterpretationFamily.has(one)))
1833
+ for (const one of ancestryOf(declaration))
1834
+ reinterpretationFamily.add(one);
1835
+ /**
1836
+ * The one tuple every copy of a reinterpreted class folds onto: a copy's
1837
+ * own fillings with `any` at each layout-relevant position where the
1838
+ * copies disagree. `null` while the copies agree, which leaves the class
1839
+ * exactly as it was -- there is nothing to fold.
1840
+ */
1841
+ const reinterpretationTupleOf = (declaration) => {
1842
+ if (!reinterpretationFamily.has(declaration) || !ts.isClassLike(declaration) || isAmbientDeclaration(declaration))
1843
+ return null;
1844
+ const seen = tuples.get(declaration) ?? [];
1845
+ const [first] = seen;
1846
+ if (first === undefined || seen.length < 2)
1847
+ return null;
1848
+ const layoutRelevant = layoutRelevantParameterIndices(checker, declaration);
1849
+ const any = checker.getAnyType();
1850
+ let differs = false;
1851
+ const tuple = first.map((filling, index) => {
1852
+ if (!layoutRelevant.has(index) || seen.every((other) => other[index] === filling))
1853
+ return filling;
1854
+ differs = true;
1855
+ return any;
1856
+ });
1857
+ return differs ? tuple : null;
1858
+ };
1859
+ /** Mint each reinterpreted class's folded copy, through `recordTuple` so its heritage is induced too. */
1860
+ const mintReinterpretationCopies = () => {
1861
+ let minted = false;
1862
+ for (const declaration of reinterpretationFamily) {
1863
+ const tuple = reinterpretationTupleOf(declaration);
1864
+ if (tuple === null || ordinalOfArguments(declaration, tuple) !== null)
1865
+ continue;
1866
+ const bound = new Map();
1867
+ typeParametersOf(declaration).forEach((parameter, index) => {
1868
+ const symbol = checker.getSymbolAtLocation(parameter.name);
1869
+ const filling = tuple[index];
1870
+ if (symbol && filling)
1871
+ bound.set(symbol, filling);
1872
+ });
1873
+ recordTuple(declaration, bound, null);
1874
+ minted = true;
1875
+ }
1876
+ return minted;
1877
+ };
1878
+ /**
1879
+ * The generic METHODS that override one another, as groups: a class's
1880
+ * generic method with a body, and every same-named instance method with a
1881
+ * body and as many type parameters along its class's ancestry.
1882
+ *
1883
+ * A receiver typed as the base runs whichever class allocated it, so every
1884
+ * copy a call through the base mints is a copy each override must have too:
1885
+ * mongodb's `OnDemandDocument.getNumber` calls `this.get(name, 'bool')`,
1886
+ * and a `MongoDBResponse` -- which overrides `get<T>` and is only ever
1887
+ * called at `'object'` itself -- must answer that call with its own body.
1888
+ * Minting only where a call is WRITTEN left the subclass with no copy at
1889
+ * that convention, and the whole dispatch family refused
1890
+ * (`projection/dispatch.ts`'s `copyAbsentFrom`). The other direction holds
1891
+ * for the same reason: a copy a subclass-typed call mints is a copy of the
1892
+ * family's member at that convention, which the base declares too.
1893
+ */
1894
+ const overrideGroups = (() => {
1895
+ const nameOf = (member) => ts.isMethodDeclaration(member) && member.body !== undefined && (ts.isIdentifier(member.name) || ts.isStringLiteral(member.name))
1896
+ ? member.name.text
1897
+ : null;
1898
+ const isInstance = (member) => !(ts.canHaveModifiers(member) && ts.getModifiers(member)?.some((one) => one.kind === ts.SyntaxKind.StaticKeyword));
1899
+ const parents = new Map();
1900
+ const find = (member) => {
1901
+ let root = member;
1902
+ while (parents.get(root) !== undefined && parents.get(root) !== root)
1903
+ root = parents.get(root);
1904
+ return root;
1905
+ };
1906
+ for (const member of declared) {
1907
+ const name = nameOf(member);
1908
+ if (name === null || !isInstance(member) || !ts.isClassLike(member.parent) || reachable.memberIsPruned(member))
1909
+ continue;
1910
+ const arity = typeParametersOf(member).length;
1911
+ for (const base of ancestryOf(member.parent).slice(1)) {
1912
+ const overridden = base.members.find((candidate) => nameOf(candidate) === name &&
1913
+ isInstance(candidate) &&
1914
+ typeParametersOf(candidate).length === arity &&
1915
+ !reachable.memberIsPruned(candidate));
1916
+ if (overridden === undefined)
1917
+ continue;
1918
+ if (!parents.has(member))
1919
+ parents.set(member, member);
1920
+ if (!parents.has(overridden))
1921
+ parents.set(overridden, overridden);
1922
+ parents.set(find(member), find(overridden));
1923
+ break;
1924
+ }
1925
+ }
1926
+ const groups = new Map();
1927
+ for (const member of parents.keys()) {
1928
+ const root = find(member);
1929
+ const group = groups.get(root) ?? [];
1930
+ group.push(member);
1931
+ groups.set(root, group);
1932
+ }
1933
+ return [...groups.values()].filter((group) => group.length > 1);
1934
+ })();
1935
+ /**
1936
+ * Whether `member` can be instantiated at `tuple`: every filling satisfies
1937
+ * the member's own written constraint. An override may state its type
1938
+ * parameters differently from the method it overrides, and a copy outside
1939
+ * its constraint is one no call could make.
1940
+ */
1941
+ const admitsTuple = (member, tuple) => typeParametersOf(member).every((parameter, index) => {
1942
+ const filling = tuple[index];
1943
+ if (filling === undefined)
1944
+ return false;
1945
+ if (parameter.constraint === undefined)
1946
+ return true;
1947
+ const constraint = checker.getTypeFromTypeNode(parameter.constraint);
1948
+ return !containsHole(constraint) && checker.isTypeAssignableTo(filling, constraint);
1949
+ });
1950
+ /** Mint, for each override group, every member's copy at every tuple another member has. */
1951
+ const mintOverrideCopies = () => {
1952
+ let minted = false;
1953
+ for (const group of overrideGroups) {
1954
+ const union = [];
1955
+ for (const member of group)
1956
+ for (const tuple of tuples.get(member) ?? [])
1957
+ if (!union.some((held) => held.length === tuple.length && held.every((one, index) => one === tuple[index])))
1958
+ union.push(tuple);
1959
+ for (const member of group) {
1960
+ for (const tuple of union) {
1961
+ if (ordinalOfArguments(member, tuple) !== null || !admitsTuple(member, tuple))
1962
+ continue;
1963
+ const bound = new Map();
1964
+ typeParametersOf(member).forEach((parameter, index) => {
1965
+ const symbol = checker.getSymbolAtLocation(parameter.name);
1966
+ const filling = tuple[index];
1967
+ if (symbol && filling)
1968
+ bound.set(symbol, filling);
1969
+ });
1970
+ recordTuple(member, bound, null);
1971
+ minted = true;
1972
+ }
1973
+ }
1974
+ }
1975
+ return minted;
1976
+ };
1392
1977
  /**
1393
1978
  * The generic declarations a node is written INSIDE, innermost first --
1394
1979
  * exactly the scopes whose copies can fill a hole this node spells.
@@ -1421,10 +2006,10 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1421
2006
  // resolved signature `fillingOf` reads. A copy with no call behind it
1422
2007
  // answers nothing and the site stays honestly uninstantiated.
1423
2008
  const ordinal = ordinalOfArguments(owner, tuple);
1424
- const image = ordinal === null ? undefined : compositeFillings.get(owner)?.[ordinal]?.get(filling);
1425
- return image !== undefined && !containsHole(image) ? image : null;
2009
+ const image = ordinal === null ? null : compositeImageOf(owner, ordinal, filling);
2010
+ return image !== null && !containsHole(image) ? image : null;
1426
2011
  }
1427
- const parameter = filling.getSymbol()?.declarations?.[0];
2012
+ const parameter = typeParameterDeclarationOf(filling);
1428
2013
  if (!parameter || !ts.isTypeParameterDeclaration(parameter))
1429
2014
  return null;
1430
2015
  const index = typeParametersOf(owner).indexOf(parameter);
@@ -1523,6 +2108,10 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1523
2108
  }
1524
2109
  }
1525
2110
  }
2111
+ if (mintReinterpretationCopies())
2112
+ minted = true;
2113
+ if (mintOverrideCopies())
2114
+ minted = true;
1526
2115
  if (!minted)
1527
2116
  break;
1528
2117
  }
@@ -1538,19 +2127,69 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1538
2127
  // what it answered before.
1539
2128
  for (const declaration of declared)
1540
2129
  built.set(declaration, []);
2130
+ const constructingUnion = checker;
2131
+ const joinOf = (types) => {
2132
+ const distinct = [...new Set(types)];
2133
+ if (distinct.length === 1)
2134
+ return distinct[0] ?? null;
2135
+ return typeof constructingUnion.getUnionType === 'function' ? constructingUnion.getUnionType(distinct) : null;
2136
+ };
1541
2137
  for (const [declaration, seen] of tuples) {
1542
2138
  const parameters = typeParametersOf(declaration);
1543
- built.set(declaration, seen.map((tuple, ordinal) => ({
1544
- ordinal,
1545
- arguments: tuple,
1546
- instantiated: spellings.get(declaration)?.[ordinal] ?? null,
1547
- instantiatedSignature: signaturePairs.get(declaration)?.[ordinal] ?? null,
1548
- bindingOf: (parameter) => {
1549
- const index = parameters.indexOf(parameter);
1550
- return index >= 0 ? (tuple[index] ?? null) : null;
1551
- },
1552
- fillingOf: (open) => compositeFillings.get(declaration)?.[ordinal]?.get(open) ?? null
1553
- })));
2139
+ // A generic function literal STORED in a cell that exists once -- a class
2140
+ // field or an object-literal property -- is ONE function at runtime, so
2141
+ // every call through the cell enters the same body. Minting a body per
2142
+ // instantiation and storing whichever the initializer's first implicit
2143
+ // return reaches keys the stored function to its first instantiation:
2144
+ // `cached = <Key extends keyof Readers>(key) => ...` held the `'text'`
2145
+ // copy, whose `key === 'text'` branch folded to true, and `count()`
2146
+ // read a string payload as a number. (A `const f = <T>...` binding is a
2147
+ // cell per copy -- the census forks the declaration -- so it is exempt.)
2148
+ // Every copy of such a literal is therefore the JOIN of the instantiations
2149
+ // the program makes: each parameter filled with the union of its fillings.
2150
+ // The ordinals stay (calls name them), the bodies agree.
2151
+ const joinedArguments = (() => {
2152
+ if (seen.length < 2 || !(ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)))
2153
+ return null;
2154
+ const holder = declaration.parent;
2155
+ if (!(ts.isPropertyDeclaration(holder) || ts.isPropertyAssignment(holder)) || holder.initializer !== declaration)
2156
+ return null;
2157
+ const joined = [];
2158
+ for (let index = 0; index < parameters.length; index += 1) {
2159
+ const joinedType = joinOf(seen.map((tuple) => tuple[index]).filter((type) => type !== undefined));
2160
+ if (!joinedType)
2161
+ return null;
2162
+ joined.push(joinedType);
2163
+ }
2164
+ return joined;
2165
+ })();
2166
+ const joinedImage = (open) => {
2167
+ const images = seen.map((_, ordinal) => compositeImageOf(declaration, ordinal, open));
2168
+ return images.every((image) => image !== null) ? joinOf(images) : null;
2169
+ };
2170
+ built.set(declaration, seen.map((tuple, ordinal) => joinedArguments
2171
+ ? {
2172
+ ordinal,
2173
+ arguments: joinedArguments,
2174
+ instantiated: null,
2175
+ instantiatedSignature: null,
2176
+ bindingOf: (parameter) => {
2177
+ const index = parameters.indexOf(parameter);
2178
+ return index >= 0 ? (joinedArguments[index] ?? null) : null;
2179
+ },
2180
+ fillingOf: joinedImage
2181
+ }
2182
+ : {
2183
+ ordinal,
2184
+ arguments: tuple,
2185
+ instantiated: spellings.get(declaration)?.[ordinal] ?? null,
2186
+ instantiatedSignature: signaturePairs.get(declaration)?.[ordinal] ?? null,
2187
+ bindingOf: (parameter) => {
2188
+ const index = parameters.indexOf(parameter);
2189
+ return index >= 0 ? (tuple[index] ?? null) : null;
2190
+ },
2191
+ fillingOf: (open) => compositeImageOf(declaration, ordinal, open)
2192
+ }));
1554
2193
  }
1555
2194
  /** Whether the program mints this copy at all, rather than only naming it. See `constructions`. */
1556
2195
  const isConstructed = (declaration, ordinal) => constructions.get(declaration)?.[ordinal] ?? true;
@@ -1606,7 +2245,50 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1606
2245
  return copies.map((copy) => (copy === chosen ? copy : { ...chosen, ordinal: copy.ordinal }));
1607
2246
  },
1608
2247
  isGeneric: (declaration) => built.has(declaration),
1609
- specializationOfInstance: (type, substitute) => {
2248
+ admittedUnder: (declaration, copy, path) => {
2249
+ const parameters = typeParametersOf(declaration);
2250
+ for (const [index, parameter] of parameters.entries()) {
2251
+ const constraint = parameter.constraint;
2252
+ if (!constraint || !ts.isTypeOperatorNode(constraint) || constraint.operator !== ts.SyntaxKind.KeyOfKeyword)
2253
+ continue;
2254
+ const keyed = checker.getTypeFromTypeNode(constraint.type);
2255
+ const keyedSymbol = keyed.flags & ts.TypeFlags.TypeParameter ? keyed.getSymbol() : undefined;
2256
+ const keyedDeclaration = keyedSymbol ? mergedDeclarationOf(keyedSymbol) : null;
2257
+ if (!keyedDeclaration)
2258
+ continue;
2259
+ let bound = null;
2260
+ for (let step = path.length - 1; step >= 0 && bound === null; step -= 1) {
2261
+ const enclosing = path[step];
2262
+ if (enclosing)
2263
+ bound = census.specializationsOf(enclosing.owner)[enclosing.ordinal]?.bindingOf(keyedDeclaration) ?? null;
2264
+ }
2265
+ if (bound === null || openFilling(bound) || bound.getStringIndexType() !== undefined)
2266
+ continue;
2267
+ if ((bound.flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection)) === 0)
2268
+ continue;
2269
+ const filling = copy.arguments[index];
2270
+ if (!filling)
2271
+ continue;
2272
+ const arms = filling.isUnion() ? filling.types : [filling];
2273
+ if (!arms.every((arm) => arm.isStringLiteral()))
2274
+ continue;
2275
+ if (arms.some((arm) => arm.isStringLiteral() && !bound.getProperty(arm.value)))
2276
+ return false;
2277
+ }
2278
+ return true;
2279
+ },
2280
+ specializationOfInstance: (receiverType, substitute) => {
2281
+ // A polymorphic `this` is a type PARAMETER whose symbol is the class
2282
+ // itself: it has no type arguments of its own, and its constraint is the
2283
+ // class at its own parameters (`FindCursor<TSchema>`), which `substitute`
2284
+ // fills with the enclosing copy's fillings. Read as it was, every
2285
+ // `this.method()` in a copy answered "no copy" and ran the base at its
2286
+ // root -- the MongoDB driver's `FindCursor.maxTimeMS` calling
2287
+ // `this.throwIfInitialized()` against an `AbstractCursor` it does not
2288
+ // derive from.
2289
+ const thisClass = (receiverType.flags & ts.TypeFlags.TypeParameter) !== 0 &&
2290
+ receiverType.getSymbol()?.declarations?.some((one) => ts.isClassLike(one) || ts.isInterfaceDeclaration(one)) === true;
2291
+ const type = thisClass ? (checker.getBaseConstraintOfType(receiverType) ?? receiverType) : receiverType;
1610
2292
  const symbol = type.getSymbol();
1611
2293
  const declaration = symbol?.declarations?.find((one) => ts.isClassLike(one) || ts.isInterfaceDeclaration(one));
1612
2294
  if (!declaration || !built.has(declaration))
@@ -1639,6 +2321,20 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1639
2321
  const sameFillings = (left, right) => left.length === right.length && left.every((one, index) => one === right[index]);
1640
2322
  if (fillings.every((right) => sameFillings(fillings[0] ?? [], right)))
1641
2323
  return false;
2324
+ // A reinterpreted class is one object at every instantiation, so it
2325
+ // is one layout; `canonicalLayoutFillings` names it.
2326
+ if (reinterpretationTupleOf(declaration) !== null)
2327
+ return false;
2328
+ // A base class that splits splits its derived classes with it: one
2329
+ // derived struct can extend only one base struct. mongodb's
2330
+ // `ListCollectionsCursor<T> extends AbstractCursor<T>` is held at
2331
+ // `CollectionInfo` and at its default union, which are assignable to
2332
+ // each other and would fold below -- while `AbstractCursor` keeps
2333
+ // `TSchema` in mutable storage and splits on the very same fillings,
2334
+ // so `cursor.toArray()` resolved against an `AbstractCursor` copy the
2335
+ // one folded struct did not extend.
2336
+ if (baseCopiesDiffer(declaration, copies))
2337
+ return true;
1642
2338
  // Two copies may be separate STRUCTS only if the program can never put
1643
2339
  // one where the other is declared. TypeScript's own assignability is
1644
2340
  // that question: `Token<number>` is assignable to `Token<unknown>`, so
@@ -1677,10 +2373,37 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1677
2373
  // which `TransformStream` really does construct and enqueue into,
1678
2374
  // apart from the `Token<unknown>` arm of a union that only ever
1679
2375
  // receives a `Token<number>`.
1680
- const written = copies.filter((copy) => isConstructed(declaration, copy.ordinal)).map((copy) => copy.arguments);
2376
+ //
2377
+ // The `any` licence covers a copy that is only ever SPELLED, never one
2378
+ // that is minted too. Two constructed copies, one at `any` and one
2379
+ // concrete, each write their own values into the stored position, and
2380
+ // one struct cannot hold both: `class CaseInsensitiveMap<V = any>
2381
+ // extends Map<string, V>`, constructed at its `any` default and at
2382
+ // `unknown[]`, folded onto the `unknown[]` copy, and
2383
+ // `new CaseInsensitiveMap([['ReplicaSet', 'rs0']])` stored `'rs0'` into
2384
+ // a map of arrays -- an abort at run time. A bare `any` filling is
2385
+ // therefore a real difference here; a filling that is merely OPEN (a
2386
+ // type parameter, or a type built over one) is not yet a filling.
2387
+ const unfilled = (type) => type !== undefined && (type.flags & ts.TypeFlags.Any) === 0 && openFilling(type);
2388
+ //
2389
+ // A bare `any` beside a concrete filling splits only where the two
2390
+ // copies would really write different data (`storedDataDiffers`):
2391
+ // hono's `Context<E>` keeps `E` only in dynamic, callable and
2392
+ // self-referencing members, and splitting it made the `Context` the
2393
+ // dispatcher builds a different type from the one every handler takes.
2394
+ const bareAny = (type) => type !== undefined && (type.flags & ts.TypeFlags.Any) !== 0;
2395
+ const written = copies.filter((copy) => isConstructed(declaration, copy.ordinal));
1681
2396
  if ([...layoutRelevant]
1682
2397
  .filter((index) => stored.has(index))
1683
- .some((index) => written.some((left) => written.some((right) => left[index] !== right[index] && !openFilling(left[index]) && !openFilling(right[index])))))
2398
+ .some((index) => written.some((left) => written.some((right) => {
2399
+ const leftFilling = left.arguments[index];
2400
+ const rightFilling = right.arguments[index];
2401
+ if (leftFilling === rightFilling || unfilled(leftFilling) || unfilled(rightFilling))
2402
+ return false;
2403
+ if (!bareAny(leftFilling) && !bareAny(rightFilling))
2404
+ return true;
2405
+ return storedDataDiffers(checker, declaration, index, left, right);
2406
+ }))))
1684
2407
  return true;
1685
2408
  const flowsInto = (source, target) => source.length === target.length &&
1686
2409
  source.every((one, index) => {
@@ -1712,6 +2435,12 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1712
2435
  // Only a class that must NOT split has one shared struct to name.
1713
2436
  if (census.copiesMayDifferInLayout(declaration))
1714
2437
  return null;
2438
+ // A reinterpreted class folds onto its `any` copy, never onto a
2439
+ // concrete one: every concrete filling is a view the program casts
2440
+ // away from (`reinterpretedClasses`).
2441
+ const reinterpreted = reinterpretationTupleOf(declaration);
2442
+ if (reinterpreted !== null)
2443
+ return ordinalOfArguments(declaration, reinterpreted) === null ? null : reinterpreted;
1715
2444
  const copies = built.get(declaration) ?? [];
1716
2445
  if (copies.length < 2)
1717
2446
  return null;
@@ -1788,6 +2517,11 @@ export const censusSpecializations = (checker, sourceFiles, reachable, namespace
1788
2517
  canonicalFillings.set(declaration, answer);
1789
2518
  return answer;
1790
2519
  },
2520
+ reinterpretedCopyOf: (declaration) => {
2521
+ const tuple = reinterpretationTupleOf(declaration);
2522
+ return tuple === null ? null : ordinalOfArguments(declaration, tuple);
2523
+ },
2524
+ erasedAnyRefilledAt: (node) => erasedAnyRefilledSites.has(node),
1791
2525
  specializationAt: (node, substitute) => {
1792
2526
  const site = sites.get(node);
1793
2527
  if (!site)