@geastack/compiler 1.0.33 → 1.0.34

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 (215) hide show
  1. package/README.md +1 -1
  2. package/dist/cli-coverage.js +2 -4
  3. package/dist/cli-emit.js +1 -1
  4. package/dist/cli-module-graph.js +1 -1
  5. package/dist/cli.js +2 -1
  6. package/dist/compiler.js +14 -6
  7. package/dist/conversion/array-view.js +9 -0
  8. package/dist/conversion/build.js +15 -8
  9. package/dist/conversion/dictionary-view.js +2 -2
  10. package/dist/conversion/nodes.d.ts +7 -0
  11. package/dist/conversion/nodes.js +58 -11
  12. package/dist/identity/ids.d.ts +4 -0
  13. package/dist/identity/ids.js +4 -0
  14. package/dist/ir/build.d.ts +4 -3
  15. package/dist/ir/build.js +14 -5
  16. package/dist/ir/call-dispatch.js +67 -7
  17. package/dist/ir/call-entry.js +1 -1
  18. package/dist/ir/callable-class-flow.d.ts +4 -0
  19. package/dist/ir/callable-class-flow.js +123 -1
  20. package/dist/ir/certify/parallel-region.js +698 -20
  21. package/dist/ir/certify.d.ts +3 -0
  22. package/dist/ir/certify.js +44 -9
  23. package/dist/ir/class-allocation-domain.js +2 -2
  24. package/dist/ir/class-construction-status.d.ts +10 -0
  25. package/dist/ir/class-construction-status.js +36 -0
  26. package/dist/ir/convert-carriers.d.ts +10 -1
  27. package/dist/ir/convert-carriers.js +18 -1
  28. package/dist/ir/dynamic-constructor-conversions.js +4 -2
  29. package/dist/ir/finite-iterator-close.d.ts +14 -0
  30. package/dist/ir/finite-iterator-close.js +50 -0
  31. package/dist/ir/host-template-conversions.js +9 -0
  32. package/dist/ir/lower-allocation.js +1 -1
  33. package/dist/ir/lower-flow.js +24 -3
  34. package/dist/ir/lower-operands.js +88 -7
  35. package/dist/ir/lower-protocol.js +1 -1
  36. package/dist/ir/lower.js +9 -4
  37. package/dist/ir/model.d.ts +39 -1
  38. package/dist/ir/native-accessor-definition.d.ts +3 -2
  39. package/dist/ir/native-accessor-definition.js +29 -10
  40. package/dist/ir/native-array-transport.js +2 -2
  41. package/dist/ir/native-field-view-domains.d.ts +23 -0
  42. package/dist/ir/native-field-view-domains.js +115 -9
  43. package/dist/ir/native-field-view-facts.d.ts +46 -2
  44. package/dist/ir/native-field-view-facts.js +159 -4
  45. package/dist/ir/native-iterable.d.ts +17 -0
  46. package/dist/ir/native-iterable.js +72 -0
  47. package/dist/ir/native-key-query.js +1 -1
  48. package/dist/ir/operation-conversions.js +15 -3
  49. package/dist/ir/program-conversions.js +2 -2
  50. package/dist/ir/publish-native-field-view-facts.js +9 -4
  51. package/dist/ir/pure-conversion-reuse.d.ts +58 -0
  52. package/dist/ir/pure-conversion-reuse.js +263 -0
  53. package/dist/ir/queries.js +1 -0
  54. package/dist/ir/reflection-demand.js +70 -18
  55. package/dist/ir/shake.js +2 -2
  56. package/dist/ir/unobserved-callable-inputs.d.ts +37 -0
  57. package/dist/ir/unobserved-callable-inputs.js +111 -0
  58. package/dist/native-signatures.d.ts +1 -1
  59. package/dist/native-signatures.js +27 -2
  60. package/dist/plugins/gea/component-classes.d.ts +2 -1
  61. package/dist/plugins/gea/component-classes.js +3 -3
  62. package/dist/plugins/gea/contract.d.ts +26 -3
  63. package/dist/plugins/gea/contract.js +42 -43
  64. package/dist/plugins/gea/host.d.ts +9 -0
  65. package/dist/plugins/gea/host.js +25 -4
  66. package/dist/plugins/gea/plugin.js +12 -6
  67. package/dist/plugins/gea/producer.d.ts +2 -2
  68. package/dist/plugins/gea/producer.js +2 -2
  69. package/dist/plugins/load.js +18 -1
  70. package/dist/plugins/model.d.ts +5 -0
  71. package/dist/plugins/native-iterable-providers.d.ts +18 -0
  72. package/dist/plugins/native-iterable-providers.js +51 -0
  73. package/dist/project-preparation.d.ts +8 -0
  74. package/dist/project-preparation.js +49 -7
  75. package/dist/projection/bindings.d.ts +13 -0
  76. package/dist/projection/bindings.js +20 -1
  77. package/dist/projection/classes.d.ts +4 -2
  78. package/dist/projection/classes.js +12 -5
  79. package/dist/representation/collections.js +2 -0
  80. package/dist/representation/derive.js +1 -0
  81. package/dist/representation/model.d.ts +2 -0
  82. package/dist/representation/model.js +1 -0
  83. package/dist/representation/policies.d.ts +2 -0
  84. package/dist/representation/primitive-domain.d.ts +1 -1
  85. package/dist/representation/primitive-domain.js +1 -1
  86. package/dist/representation/unassigned-binding-cells.js +6 -1
  87. package/dist/representation/union.js +6 -6
  88. package/dist/semantics/frontend.d.ts +2 -0
  89. package/dist/semantics/frontend.js +34 -5
  90. package/dist/semantics/host-methods.js +4 -1
  91. package/dist/semantics/jsx-runtime-provider.d.ts +5 -0
  92. package/dist/semantics/jsx-runtime-provider.js +16 -0
  93. package/dist/semantics/model/native-iterable.d.ts +14 -0
  94. package/dist/semantics/model/native-iterable.js +7 -0
  95. package/dist/semantics/model/operands.d.ts +30 -0
  96. package/dist/semantics/model/operations.d.ts +15 -0
  97. package/dist/semantics/model/primitive-domain.d.ts +15 -0
  98. package/dist/semantics/model/primitive-domain.js +27 -4
  99. package/dist/semantics/module-resolution.js +13 -2
  100. package/dist/semantics/normalize/any-cast-member-call.d.ts +1 -1
  101. package/dist/semantics/normalize/callback-parameter-contracts.d.ts +32 -4
  102. package/dist/semantics/normalize/callback-parameter-contracts.js +66 -6
  103. package/dist/semantics/normalize/census.d.ts +3 -0
  104. package/dist/semantics/normalize/census.js +17 -12
  105. package/dist/semantics/normalize/deferred-intrinsic-protocols.d.ts +4 -1
  106. package/dist/semantics/normalize/deferred-intrinsic-protocols.js +29 -9
  107. package/dist/semantics/normalize/flow/callable-reach.js +94 -3
  108. package/dist/semantics/normalize/flow/component-solver.js +144 -55
  109. package/dist/semantics/normalize/flow/member-call-forwarding.d.ts +9 -0
  110. package/dist/semantics/normalize/flow/member-call-forwarding.js +30 -4
  111. package/dist/semantics/normalize/flow/proof-hypotheses.d.ts +2 -0
  112. package/dist/semantics/normalize/flow/proof-hypotheses.js +27 -0
  113. package/dist/semantics/normalize/flow/source-callable-own-data.js +87 -14
  114. package/dist/semantics/normalize/flow/source-commonjs-exports.d.ts +2 -0
  115. package/dist/semantics/normalize/flow/source-commonjs-exports.js +2 -0
  116. package/dist/semantics/normalize/flow/source-native-iterable-integrity.d.ts +17 -0
  117. package/dist/semantics/normalize/flow/source-native-iterable-integrity.js +125 -0
  118. package/dist/semantics/normalize/flow/source-native-origin.d.ts +6 -0
  119. package/dist/semantics/normalize/flow/source-native-origin.js +38 -0
  120. package/dist/semantics/normalize/flow/source-proxy-operation.d.ts +37 -0
  121. package/dist/semantics/normalize/flow/source-proxy-operation.js +201 -0
  122. package/dist/semantics/normalize/flow/source-raw-constructor-entry.d.ts +20 -0
  123. package/dist/semantics/normalize/flow/source-raw-constructor-entry.js +73 -0
  124. package/dist/semantics/normalize/flow/source-value-session.d.ts +50 -1
  125. package/dist/semantics/normalize/flow/source-value-session.js +596 -57
  126. package/dist/semantics/normalize/flow/value-flow.d.ts +17 -0
  127. package/dist/semantics/normalize/flow/value-flow.js +84 -0
  128. package/dist/semantics/normalize/fresh-dictionary-storage.js +2 -2
  129. package/dist/semantics/normalize/fresh-table-last-use.d.ts +52 -0
  130. package/dist/semantics/normalize/fresh-table-last-use.js +129 -0
  131. package/dist/semantics/normalize/gating.js +5 -1
  132. package/dist/semantics/normalize/global-host-mutations.js +190 -13
  133. package/dist/semantics/normalize/identities.js +20 -1
  134. package/dist/semantics/normalize/local-bindings.js +2 -2
  135. package/dist/semantics/normalize/parameter-bindings.js +10 -1
  136. package/dist/semantics/normalize/producer-context.d.ts +5 -0
  137. package/dist/semantics/normalize/producers/binding-kind.js +2 -0
  138. package/dist/semantics/normalize/producers/cited-value-type.d.ts +2 -2
  139. package/dist/semantics/normalize/producers/cited-value-type.js +2 -2
  140. package/dist/semantics/normalize/producers/class-lifecycle.js +39 -9
  141. package/dist/semantics/normalize/producers/computations.js +36 -5
  142. package/dist/semantics/normalize/producers/conditional-guard.d.ts +6 -0
  143. package/dist/semantics/normalize/producers/conditional-guard.js +5 -0
  144. package/dist/semantics/normalize/producers/control.js +7 -1
  145. package/dist/semantics/normalize/producers/destructuring.js +8 -9
  146. package/dist/semantics/normalize/producers/invocations.js +55 -2
  147. package/dist/semantics/normalize/producers/properties.d.ts +13 -0
  148. package/dist/semantics/normalize/producers/properties.js +21 -3
  149. package/dist/semantics/normalize/producers/references.js +8 -4
  150. package/dist/semantics/normalize/reachability.d.ts +15 -3
  151. package/dist/semantics/normalize/reachability.js +128 -20
  152. package/dist/semantics/normalize/return-bindings.js +3 -3
  153. package/dist/semantics/normalize/source-native-iterable.d.ts +19 -0
  154. package/dist/semantics/normalize/source-native-iterable.js +74 -0
  155. package/dist/semantics/normalize/structural-any-binding-consumer.d.ts +1 -1
  156. package/dist/semantics/normalize/structural-declarations.js +1 -1
  157. package/dist/semantics/normalize/structural-indexed-access.js +9 -0
  158. package/dist/semantics/normalize/structural.js +11 -0
  159. package/dist/semantics/normalize/unshared-array-result.d.ts +42 -0
  160. package/dist/semantics/normalize/unshared-array-result.js +269 -0
  161. package/dist/semantics/package-sources.js +3 -2
  162. package/dist/semantics/program.d.ts +8 -0
  163. package/dist/semantics/program.js +55 -0
  164. package/dist/semantics/source-transform-exports.d.ts +8 -0
  165. package/dist/semantics/source-transform-exports.js +145 -0
  166. package/dist/semantics/source-transform-protocol.d.ts +22 -0
  167. package/dist/semantics/this-constructor-source-transform.js +19 -1
  168. package/dist/targets/cpp/class-layout.d.ts +2 -0
  169. package/dist/targets/cpp/class-layout.js +35 -1
  170. package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +11 -1
  171. package/dist/targets/cpp/class-properties/emit-class-properties.js +33 -4
  172. package/dist/targets/cpp/conversions.js +6 -3
  173. package/dist/targets/cpp/emit-callable.js +73 -7
  174. package/dist/targets/cpp/emit-context.d.ts +23 -9
  175. package/dist/targets/cpp/emit-context.js +19 -8
  176. package/dist/targets/cpp/emit-equality.js +41 -1
  177. package/dist/targets/cpp/emit-exceptions.js +29 -11
  178. package/dist/targets/cpp/emit-iterator-object-view.js +2 -2
  179. package/dist/targets/cpp/emit-iterator.js +33 -9
  180. package/dist/targets/cpp/emit-live-field-view.js +6 -1
  181. package/dist/targets/cpp/emit-narrowing.d.ts +11 -3
  182. package/dist/targets/cpp/emit-narrowing.js +58 -18
  183. package/dist/targets/cpp/emit-native-field-view.d.ts +3 -1
  184. package/dist/targets/cpp/emit-native-field-view.js +84 -8
  185. package/dist/targets/cpp/emit-properties.d.ts +1 -0
  186. package/dist/targets/cpp/emit-properties.js +26 -7
  187. package/dist/targets/cpp/emit-protocol-iterator.js +5 -2
  188. package/dist/targets/cpp/emit-record-view.js +14 -2
  189. package/dist/targets/cpp/emit-tostring.js +23 -1
  190. package/dist/targets/cpp/emit-union-properties.js +1 -1
  191. package/dist/targets/cpp/emit.d.ts +1 -1
  192. package/dist/targets/cpp/emit.js +99 -13
  193. package/dist/targets/cpp/host/emit-host-invoke.js +2 -2
  194. package/dist/targets/cpp/host/emit-host-object.js +28 -2
  195. package/dist/targets/cpp/host/emit-host-properties.js +9 -1
  196. package/dist/targets/cpp/host/emit-host-value.d.ts +1 -1
  197. package/dist/targets/cpp/host/emit-host-value.js +8 -6
  198. package/dist/targets/cpp/host/host-members.js +2 -1
  199. package/dist/targets/cpp/prototype/emit-prototype-array.js +69 -5
  200. package/dist/targets/cpp/prototype/emit-prototype-invoke.js +9 -9
  201. package/dist/targets/cpp/prototype/emit-prototype-iterator.js +2 -2
  202. package/dist/targets/cpp/prototype/emit-prototype-promise.js +7 -7
  203. package/dist/targets/cpp/records.d.ts +1 -0
  204. package/dist/targets/cpp/records.js +16 -7
  205. package/dist/targets/cpp/translation-unit.js +30 -16
  206. package/package.json +8 -7
  207. package/src/targets/cpp/runtime/gea_dynamic_proxy.h +42 -3
  208. package/src/targets/cpp/runtime/gea_eval.h +3 -0
  209. package/src/targets/cpp/runtime/gea_runtime.h +2399 -676
  210. package/dist/representation/any-null-record-members.d.ts +0 -41
  211. package/dist/representation/any-null-record-members.js +0 -115
  212. package/dist/semantics/any-null-member-declarations.d.ts +0 -32
  213. package/dist/semantics/any-null-member-declarations.js +0 -90
  214. package/dist/semantics/normalize/fresh-array-result.d.ts +0 -32
  215. package/dist/semantics/normalize/fresh-array-result.js +0 -125
@@ -1,41 +0,0 @@
1
- import type { SemanticResultId, StructuralTypeId } from '../identity/ids.js';
2
- import type { SemanticGraph } from '../semantics/model/graph.js';
3
- /**
4
- * The record members an `any` value writes `null` into although their
5
- * declared type admits no `null`, and every read of one.
6
- *
7
- * `any` is the unchecked top: the checker lets it into every position, so a
8
- * declared type says nothing about what an `any` write stores. mongodb's
9
- * `ServerDescription` declares `$clusterTime?: ClusterTime` and writes
10
- * `this.$clusterTime = hello?.$clusterTime ?? null` with `hello` a
11
- * `Document`; the right-hand side is `any`, and node stores the `null` the
12
- * `?? null` produces. Laid out from the declaration alone the member had one
13
- * `undefined` absence, the write converted the boxed `null` through a checked
14
- * record unwrap, and the driver threw a TypeError building its first
15
- * topology.
16
- *
17
- * Only a `null` the program SPELLS is admitted -- the literal itself, or the
18
- * fallback arm of a `??`/`||`, or an arm of a conditional -- reached from a
19
- * write whose value the checker typed `any`. A `null` arriving inside an
20
- * opaque `any` (a parsed document's field) is not visible here and still
21
- * meets the checked unwrap: this widens the layouts a program visibly writes
22
- * `null` into, it does not guess at what a dynamic value holds.
23
- *
24
- * Such a member is laid out as its declared type plus `null` (`derive.ts`'s
25
- * `recordFieldsOf`), and each constant-keyed read publishes that same field
26
- * carrier, so the read observes the `null` rather than converting it back
27
- * into the declaration's carrier.
28
- *
29
- * Keyed by the object shape the record -- or a class instance, through its
30
- * body -- is laid out under, exactly as `unwritten-record-members.ts` keys its
31
- * census.
32
- */
33
- export interface AnyNullRecordMembers {
34
- readonly members: ReadonlyMap<StructuralTypeId, ReadonlySet<string>>;
35
- /** Every constant-keyed read of one of those members, with the layout it reads. */
36
- readonly reads: ReadonlyMap<SemanticResultId, {
37
- readonly layout: StructuralTypeId;
38
- readonly key: string;
39
- }>;
40
- }
41
- export declare const anyNullRecordMembersOf: (graph: SemanticGraph) => AnyNullRecordMembers;
@@ -1,115 +0,0 @@
1
- import { operandOf } from '../semantics/model/operands.js';
2
- import { recordFieldKeyOf } from './object-shape.js';
3
- /** The object shape a record or class-instance carrier of `id` is laid out under. */
4
- const layoutShapeOf = (types, id) => {
5
- let current = id;
6
- for (let depth = 0; current !== null && depth < 16; depth += 1) {
7
- const shape = types.get(current)?.shape;
8
- if (!shape)
9
- return null;
10
- if (shape.kind === 'object')
11
- return current;
12
- if (shape.kind === 'declared' || shape.kind === 'object-anchor' || shape.kind === 'class-instance')
13
- current = shape.body;
14
- else
15
- return null;
16
- }
17
- return null;
18
- };
19
- const isAny = (types, id) => {
20
- const shape = types.get(id)?.shape;
21
- return shape?.kind === 'primitive' && shape.primitive === 'any';
22
- };
23
- const admitsNull = (types, id, depth = 0) => {
24
- const shape = types.get(id)?.shape;
25
- if (!shape || depth > 16)
26
- return true;
27
- if (shape.kind === 'primitive')
28
- return shape.primitive === 'null' || shape.primitive === 'any' || shape.primitive === 'unknown';
29
- if (shape.kind === 'union')
30
- return shape.members.some((member) => admitsNull(types, member, depth + 1));
31
- if (shape.kind === 'declared' && shape.body !== null && types.get(shape.body)?.shape.kind === 'union')
32
- return admitsNull(types, shape.body, depth + 1);
33
- return false;
34
- };
35
- export const anyNullRecordMembersOf = (graph) => {
36
- const types = graph.structuralTypes;
37
- const producerOf = (operand) => {
38
- if (operand.source.kind !== 'result')
39
- return undefined;
40
- const id = graph.results.get(operand.source.result);
41
- return id === undefined ? undefined : graph.operations.get(id);
42
- };
43
- // Whether the value an operand carries may be a `null` the source spells.
44
- const spellsNull = (operand, depth = 0) => {
45
- if (!operand || depth > 8)
46
- return false;
47
- if (operand.source.kind === 'constant')
48
- return operand.source.literal === 'null';
49
- const producer = producerOf(operand);
50
- if (producer?.family !== 'computation')
51
- return false;
52
- if (producer.form === 'assignment' || producer.form === 'comma') {
53
- return spellsNull(operandOf(producer, 'value') ?? operandOf(producer, 'right'), depth + 1);
54
- }
55
- if (producer.form === 'logical' && (producer.operator === '??' || producer.operator === '||')) {
56
- return spellsNull(operandOf(producer, 'right'), depth + 1);
57
- }
58
- if (producer.form === 'conditional') {
59
- return producer.operands.some((candidate) => candidate.role !== 'condition' && spellsNull(candidate, depth + 1));
60
- }
61
- return false;
62
- };
63
- // The type the checker gave the value before the write converted it into
64
- // the member's storage: the producing computation's own result.
65
- const writtenAsAny = (operand) => {
66
- if (isAny(types, operand.type))
67
- return true;
68
- const producer = producerOf(operand);
69
- const result = producer?.results.find((candidate) => candidate.role === 'value');
70
- return result !== undefined && isAny(types, result.type);
71
- };
72
- const members = new Map();
73
- for (const operation of graph.operations.values()) {
74
- if (operation.family !== 'property' || operation.keyIsComputed)
75
- continue;
76
- if (operation.internalMethod !== 'set' && operation.internalMethod !== 'define-own-property')
77
- continue;
78
- const key = operandOf(operation, 'key');
79
- const receiver = operandOf(operation, 'receiver');
80
- const value = operandOf(operation, 'value');
81
- if (key?.source.kind !== 'constant' || !receiver || !value)
82
- continue;
83
- if (!writtenAsAny(value) || !spellsNull(value))
84
- continue;
85
- const layout = layoutShapeOf(types, receiver.type);
86
- const shape = layout === null ? undefined : types.get(layout)?.shape;
87
- if (layout === null || shape?.kind !== 'object')
88
- continue;
89
- const keyText = key.source.text;
90
- const member = shape.members.find((candidate) => candidate.key.kind !== 'symbol' && recordFieldKeyOf(candidate.key) === keyText);
91
- if (!member || member.accessor !== null || admitsNull(types, member.type))
92
- continue;
93
- const known = members.get(layout);
94
- if (known)
95
- known.add(keyText);
96
- else
97
- members.set(layout, new Set([keyText]));
98
- }
99
- const reads = new Map();
100
- if (members.size === 0)
101
- return { members, reads };
102
- for (const operation of graph.operations.values()) {
103
- if (operation.family !== 'property' || operation.internalMethod !== 'get' || operation.keyIsComputed)
104
- continue;
105
- const key = operandOf(operation, 'key');
106
- const receiver = operandOf(operation, 'receiver');
107
- const value = operation.results.find((candidate) => candidate.role === 'value');
108
- if (key?.source.kind !== 'constant' || !receiver || !value)
109
- continue;
110
- const layout = layoutShapeOf(types, receiver.type);
111
- if (layout !== null && members.get(layout)?.has(key.source.text))
112
- reads.set(value.id, { layout, key: key.source.text });
113
- }
114
- return { members, reads };
115
- };
@@ -1,32 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * Property declarations the program writes a spelled `null` into through an
4
- * `any` value, restated to admit that `null`.
5
- *
6
- * `any` is the unchecked top: the checker lets it into every position, so a
7
- * declaration says nothing about what an `any` write stores there. mongodb's
8
- * `ServerDescription` declares `$clusterTime?: ClusterTime` and writes
9
- * `this.$clusterTime = hello?.$clusterTime ?? null` with `hello` a
10
- * `Document`: the right-hand side is `any`, the checker accepts it, and node
11
- * stores the `null` the `?? null` produces. Every later stage took the
12
- * declaration at its word -- the field was laid out with one `undefined`
13
- * absence, the write converted the boxed `null` through a checked record
14
- * unwrap and threw a TypeError, and a read of it would have folded
15
- * `=== null` to `false`.
16
- *
17
- * The declaration is the one authority every read, local and comparison
18
- * inherits its type from, so it is the place to state the truth: its type
19
- * becomes `(T) | null` and the checker re-derives everything downstream from
20
- * that, including a diagnostic wherever the program then passes the member on
21
- * to a position that cannot hold `null` -- which is a real defect in the
22
- * program's typing, not one this introduces.
23
- *
24
- * Only a `null` the program SPELLS counts -- the literal, the fallback arm of
25
- * `??`/`||`, an arm of a conditional -- written by a plain `=` whose
26
- * right-hand side the checker typed `any`. A `null` hidden inside an opaque
27
- * `any` is not visible here and still meets the checked unwrap.
28
- *
29
- * Positions are those of the program's own current text, so the rewritten
30
- * text supersedes every earlier preparation of the same file.
31
- */
32
- export declare const anyNullMemberDeclarations: (program: ts.Program, checker: ts.TypeChecker) => ReadonlyMap<string, string>;
@@ -1,90 +0,0 @@
1
- import { resolve } from 'node:path';
2
- import ts from 'typescript';
3
- /**
4
- * Property declarations the program writes a spelled `null` into through an
5
- * `any` value, restated to admit that `null`.
6
- *
7
- * `any` is the unchecked top: the checker lets it into every position, so a
8
- * declaration says nothing about what an `any` write stores there. mongodb's
9
- * `ServerDescription` declares `$clusterTime?: ClusterTime` and writes
10
- * `this.$clusterTime = hello?.$clusterTime ?? null` with `hello` a
11
- * `Document`: the right-hand side is `any`, the checker accepts it, and node
12
- * stores the `null` the `?? null` produces. Every later stage took the
13
- * declaration at its word -- the field was laid out with one `undefined`
14
- * absence, the write converted the boxed `null` through a checked record
15
- * unwrap and threw a TypeError, and a read of it would have folded
16
- * `=== null` to `false`.
17
- *
18
- * The declaration is the one authority every read, local and comparison
19
- * inherits its type from, so it is the place to state the truth: its type
20
- * becomes `(T) | null` and the checker re-derives everything downstream from
21
- * that, including a diagnostic wherever the program then passes the member on
22
- * to a position that cannot hold `null` -- which is a real defect in the
23
- * program's typing, not one this introduces.
24
- *
25
- * Only a `null` the program SPELLS counts -- the literal, the fallback arm of
26
- * `??`/`||`, an arm of a conditional -- written by a plain `=` whose
27
- * right-hand side the checker typed `any`. A `null` hidden inside an opaque
28
- * `any` is not visible here and still meets the checked unwrap.
29
- *
30
- * Positions are those of the program's own current text, so the rewritten
31
- * text supersedes every earlier preparation of the same file.
32
- */
33
- export const anyNullMemberDeclarations = (program, checker) => {
34
- if (!program.getCompilerOptions().strictNullChecks && !program.getCompilerOptions().strict)
35
- return new Map();
36
- const spellsNull = (expression, depth = 0) => {
37
- if (depth > 8)
38
- return false;
39
- if (expression.kind === ts.SyntaxKind.NullKeyword)
40
- return true;
41
- if (ts.isParenthesizedExpression(expression))
42
- return spellsNull(expression.expression, depth + 1);
43
- if (ts.isConditionalExpression(expression))
44
- return spellsNull(expression.whenTrue, depth + 1) || spellsNull(expression.whenFalse, depth + 1);
45
- if (ts.isBinaryExpression(expression) &&
46
- (expression.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken || expression.operatorToken.kind === ts.SyntaxKind.BarBarToken))
47
- return spellsNull(expression.right, depth + 1);
48
- return false;
49
- };
50
- const admitsNull = (type) => (type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0 ||
51
- (type.isUnion() && type.types.some((member) => (member.flags & ts.TypeFlags.Null) !== 0));
52
- const widened = new Map();
53
- const visit = (node) => {
54
- if (ts.isBinaryExpression(node) &&
55
- node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
56
- (ts.isPropertyAccessExpression(node.left) || ts.isElementAccessExpression(node.left)) &&
57
- spellsNull(node.right) &&
58
- (checker.getTypeAtLocation(node.right).flags & ts.TypeFlags.Any) !== 0) {
59
- const symbol = checker.getSymbolAtLocation(ts.isPropertyAccessExpression(node.left) ? node.left.name : node.left.argumentExpression);
60
- for (const declaration of symbol?.declarations ?? []) {
61
- if (!ts.isPropertyDeclaration(declaration) && !ts.isPropertySignature(declaration))
62
- continue;
63
- const typeNode = declaration.type;
64
- const file = declaration.getSourceFile();
65
- if (!typeNode || file.isDeclarationFile || admitsNull(checker.getTypeFromTypeNode(typeNode)))
66
- continue;
67
- const known = widened.get(file);
68
- if (known)
69
- known.add(typeNode);
70
- else
71
- widened.set(file, new Set([typeNode]));
72
- }
73
- }
74
- ts.forEachChild(node, visit);
75
- };
76
- for (const file of program.getSourceFiles())
77
- if (!file.isDeclarationFile)
78
- visit(file);
79
- const sourceText = new Map();
80
- for (const [file, typeNodes] of widened) {
81
- let text = file.text;
82
- const ordered = [...typeNodes].sort((left, right) => right.getStart(file) - left.getStart(file));
83
- for (const typeNode of ordered) {
84
- const start = typeNode.getStart(file);
85
- text = `${text.slice(0, start)}(${text.slice(start, typeNode.end)}) | null${text.slice(typeNode.end)}`;
86
- }
87
- sourceText.set(resolve(file.fileName), text);
88
- }
89
- return sourceText;
90
- };
@@ -1,32 +0,0 @@
1
- import ts from 'typescript';
2
- /**
3
- * The method body a member call runs when it returns an array NO OTHER
4
- * REFERENCE holds: every `return` hands back an array literal, or a local
5
- * initialized with one that the body only fills, indexes and measures.
6
- *
7
- * This is the one fact that makes converting an `array-object` between two
8
- * element carriers sound. A conversion between them has to allocate, and for
9
- * a mutable array a second array loses every write through the other alias
10
- * (`array-object-call-argument-aliasing`). A call result proven fresh has no
11
- * other alias: the callee dropped its own reference when it returned, so the
12
- * converted array is the only one the program can ever observe, and a
13
- * one-time element-wise copy at the call is indistinguishable from the
14
- * callee having built it at the caller's element type.
15
- *
16
- * An `async` body qualifies the same way: its promise settles with the
17
- * array its `return` names, and that array is still held by nothing else.
18
- * mongodb's `AbstractCursor.toArray` is the shape this exists for -- it
19
- * builds `const array: TSchema[] = []`, pushes every document and returns it,
20
- * while the cursor class is folded onto its `any` copy (`specialization.ts`'s
21
- * reinterpreted classes), so the body hands back `any[]` where the caller's
22
- * view declares `CollectionInfo[]`.
23
- *
24
- * Deliberately syntactic and small. Any use of the local outside the listed
25
- * forms -- passing it as an argument, storing it, capturing it in a closure,
26
- * a method whose result aliases it (`reverse`, `sort`), iteration protocol
27
- * hooks -- refuses, and a refusal only keeps the conversion refused. The key
28
- * is returned alongside so lowering can confirm the call it runs really
29
- * reaches this body: an override in the receiver's family, or a prototype
30
- * write, would run a different body the proof never saw.
31
- */
32
- export declare const freshArrayResultBodyOf: (checker: ts.TypeChecker, node: ts.CallExpression) => ts.MethodDeclaration | null;
@@ -1,125 +0,0 @@
1
- import ts from 'typescript';
2
- import { inheritedImplementationOf } from './merged-declaration.js';
3
- /**
4
- * The method body a member call runs when it returns an array NO OTHER
5
- * REFERENCE holds: every `return` hands back an array literal, or a local
6
- * initialized with one that the body only fills, indexes and measures.
7
- *
8
- * This is the one fact that makes converting an `array-object` between two
9
- * element carriers sound. A conversion between them has to allocate, and for
10
- * a mutable array a second array loses every write through the other alias
11
- * (`array-object-call-argument-aliasing`). A call result proven fresh has no
12
- * other alias: the callee dropped its own reference when it returned, so the
13
- * converted array is the only one the program can ever observe, and a
14
- * one-time element-wise copy at the call is indistinguishable from the
15
- * callee having built it at the caller's element type.
16
- *
17
- * An `async` body qualifies the same way: its promise settles with the
18
- * array its `return` names, and that array is still held by nothing else.
19
- * mongodb's `AbstractCursor.toArray` is the shape this exists for -- it
20
- * builds `const array: TSchema[] = []`, pushes every document and returns it,
21
- * while the cursor class is folded onto its `any` copy (`specialization.ts`'s
22
- * reinterpreted classes), so the body hands back `any[]` where the caller's
23
- * view declares `CollectionInfo[]`.
24
- *
25
- * Deliberately syntactic and small. Any use of the local outside the listed
26
- * forms -- passing it as an argument, storing it, capturing it in a closure,
27
- * a method whose result aliases it (`reverse`, `sort`), iteration protocol
28
- * hooks -- refuses, and a refusal only keeps the conversion refused. The key
29
- * is returned alongside so lowering can confirm the call it runs really
30
- * reaches this body: an override in the receiver's family, or a prototype
31
- * write, would run a different body the proof never saw.
32
- */
33
- export const freshArrayResultBodyOf = (checker, node) => {
34
- if (ts.isOptionalChain(node))
35
- return null;
36
- const callee = skipParentheses(node.expression);
37
- if (!ts.isPropertyAccessExpression(callee) || !ts.isIdentifier(callee.name))
38
- return null;
39
- const member = checker.getSymbolAtLocation(callee.name);
40
- if (!member)
41
- return null;
42
- const implementation = inheritedImplementationOf(checker, member) ?? member;
43
- const bodies = (implementation.declarations ?? []).filter((declaration) => ts.isMethodDeclaration(declaration) && declaration.body !== undefined && ts.isClassLike(declaration.parent));
44
- const body = bodies.length === 1 ? bodies[0] : null;
45
- if (!body || body.asteriskToken !== undefined)
46
- return null;
47
- return returnsFreshArray(checker, body) ? body : null;
48
- };
49
- const returnsFreshArray = (checker, body) => {
50
- const returns = [];
51
- let bare = false;
52
- const collect = (node) => {
53
- if (ts.isFunctionLike(node) || ts.isClassLike(node))
54
- return;
55
- if (ts.isReturnStatement(node)) {
56
- if (node.expression)
57
- returns.push(skipParentheses(node.expression));
58
- else
59
- bare = true;
60
- }
61
- ts.forEachChild(node, collect);
62
- };
63
- ts.forEachChild(body.body, collect);
64
- if (bare || returns.length === 0)
65
- return false;
66
- return returns.every((expression) => ts.isArrayLiteralExpression(expression) || freshLocal(checker, body, expression));
67
- };
68
- const freshLocal = (checker, body, expression) => {
69
- if (!ts.isIdentifier(expression))
70
- return false;
71
- const symbol = checker.getSymbolAtLocation(expression);
72
- const declaration = symbol?.declarations?.length === 1 ? symbol.declarations[0] : undefined;
73
- if (!symbol || !declaration || !ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name))
74
- return false;
75
- if (!declaration.initializer || !ts.isArrayLiteralExpression(skipParentheses(declaration.initializer)))
76
- return false;
77
- const list = declaration.parent;
78
- if (!ts.isVariableDeclarationList(list) || (list.flags & ts.NodeFlags.Const) === 0)
79
- return false;
80
- if (enclosingFunction(declaration) !== body)
81
- return false;
82
- let admitted = true;
83
- const visit = (node) => {
84
- if (!admitted)
85
- return;
86
- if (ts.isIdentifier(node) && node !== declaration.name && checker.getSymbolAtLocation(node) === symbol) {
87
- admitted = enclosingFunction(node) === body && nonEscapingUse(node);
88
- return;
89
- }
90
- ts.forEachChild(node, visit);
91
- };
92
- ts.forEachChild(body.body, visit);
93
- return admitted;
94
- };
95
- /** A use that reads, fills or measures the array and hands no reference to it anywhere. */
96
- const nonEscapingUse = (use) => {
97
- const parent = use.parent;
98
- if (ts.isReturnStatement(parent))
99
- return true;
100
- if (ts.isParenthesizedExpression(parent))
101
- return false;
102
- if (ts.isPropertyAccessExpression(parent) && parent.expression === use && ts.isIdentifier(parent.name)) {
103
- const name = parent.name.text;
104
- if (name === 'length')
105
- return !isAssignmentTarget(parent);
106
- const call = parent.parent;
107
- return (name === 'push' || name === 'unshift') && ts.isCallExpression(call) && call.expression === parent;
108
- }
109
- if (ts.isElementAccessExpression(parent) && parent.expression === use)
110
- return true;
111
- return false;
112
- };
113
- const isAssignmentTarget = (node) => ts.isBinaryExpression(node.parent) &&
114
- node.parent.left === node &&
115
- node.parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
116
- node.parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
117
- const enclosingFunction = (node) => {
118
- let current = node.parent;
119
- while (current && !ts.isFunctionLike(current))
120
- current = current.parent;
121
- return current;
122
- };
123
- const skipParentheses = (node) => ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node) || ts.isSatisfiesExpression(node)
124
- ? skipParentheses(node.expression)
125
- : node;