@geastack/compiler 1.0.31 → 1.0.33
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.
- package/dist/compiler.js +21 -22
- package/dist/ir/dynamic-protocol.d.ts +20 -0
- package/dist/ir/dynamic-protocol.js +88 -0
- package/dist/ir/lower.js +13 -2
- package/dist/ir/model.d.ts +2 -0
- package/dist/representation/any-null-record-members.d.ts +41 -0
- package/dist/representation/any-null-record-members.js +115 -0
- package/dist/representation/unassigned-binding-cells.d.ts +4 -3
- package/dist/representation/unassigned-binding-cells.js +13 -1
- package/dist/semantics/any-null-member-declarations.d.ts +32 -0
- package/dist/semantics/any-null-member-declarations.js +90 -0
- package/dist/semantics/normalize/fresh-array-result.d.ts +32 -0
- package/dist/semantics/normalize/fresh-array-result.js +125 -0
- package/dist/targets/cpp/emit-narrowing.js +17 -0
- package/dist/targets/cpp/emit-native-field-view.js +23 -13
- package/dist/targets/cpp/emit.js +33 -8
- package/dist/targets/cpp/records.d.ts +2 -0
- package/dist/targets/cpp/records.js +6 -3
- package/dist/targets/cpp/translation-unit.js +38 -1
- package/package.json +1 -1
- package/src/targets/cpp/runtime/gea_document_view.h +988 -0
- package/src/targets/cpp/runtime/gea_runtime.h +354 -212
package/dist/compiler.js
CHANGED
|
@@ -235,6 +235,23 @@ export const compile = (request) => {
|
|
|
235
235
|
const nativeTypes = new Map([...coreNativeTypes, ...plugins.flatMap((plugin) => [...plugin.capabilities.nativeTypes])]);
|
|
236
236
|
const nativeTypesByDeclaration = new Map(plugins.flatMap((plugin) => [...(plugin.capabilities.nativeTypesByDeclaration ?? new Map())]));
|
|
237
237
|
const hostNamespaceRootDeclarations = plugins.flatMap((plugin) => plugin.capabilities.hostNamespaceRootDeclarations ?? []);
|
|
238
|
+
const hostProvidedNames = new Set([
|
|
239
|
+
...coreHostFunctions.keys(),
|
|
240
|
+
...coreHostConstants.keys(),
|
|
241
|
+
...plugins.flatMap((plugin) => [
|
|
242
|
+
...plugin.capabilities.hostFunctions.keys(),
|
|
243
|
+
...plugin.capabilities.nativeConstants.keys(),
|
|
244
|
+
...[...plugin.capabilities.hostFunctionsByDeclaration.values()].flatMap((names) => [...names.keys()]),
|
|
245
|
+
...[...plugin.capabilities.hostConstantsByDeclaration.values()].flatMap((names) => [...names.keys()]),
|
|
246
|
+
// A namespace root or singleton is equally the host's by name: the
|
|
247
|
+
// framework states `declare const __gea_Panel: {...}` module-locally
|
|
248
|
+
// for a root its host table claims, and no global declares it.
|
|
249
|
+
...plugin.capabilities.hostNamespaces.roots,
|
|
250
|
+
...[...plugin.capabilities.hostNamespaceRootsByDeclaration.values()].flatMap((names) => [...names]),
|
|
251
|
+
...plugin.capabilities.hostSingletons,
|
|
252
|
+
...[...plugin.capabilities.hostSingletonsByDeclaration.values()].flatMap((names) => [...names])
|
|
253
|
+
])
|
|
254
|
+
]);
|
|
238
255
|
const frontend = runFrontend({
|
|
239
256
|
nativeFunctionDeclarations: plugins.flatMap((plugin) => plugin.capabilities.nativeFunctionDeclarations ?? []),
|
|
240
257
|
...(request.packageSources ? { packageSources: request.packageSources } : {}),
|
|
@@ -254,33 +271,15 @@ export const compile = (request) => {
|
|
|
254
271
|
nativeTypesByDeclaration,
|
|
255
272
|
hostSingletonDeclarations: new Map(plugins.flatMap((plugin) => [...(plugin.capabilities.hostSingletonDeclarations ?? new Map())])),
|
|
256
273
|
standardClasses: new Set(coreNativeTypes.keys()),
|
|
257
|
-
//
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
// declaration is believed exactly as before.
|
|
261
|
-
absentGlobals: new Set(plugins.flatMap((plugin) => [...plugin.capabilities.absentGlobals])),
|
|
274
|
+
// An optional feature cannot erase a global another installed host
|
|
275
|
+
// actually supplies (for example WebGL absence versus Gea worker self).
|
|
276
|
+
absentGlobals: new Set(plugins.flatMap((plugin) => [...plugin.capabilities.absentGlobals]).filter((name) => !hostProvidedNames.has(name))),
|
|
262
277
|
// The members the installed hosts reach on their own, unioned the same way
|
|
263
278
|
// every other plugin table here is. `reachability.ts` keeps an instance
|
|
264
279
|
// method only when live code names it, and a name only a lowering
|
|
265
280
|
// synthesizes is one no walk over the source can see.
|
|
266
281
|
hostReachedMemberKeys: new Set(plugins.flatMap((plugin) => [...plugin.capabilities.reachedMemberKeys])),
|
|
267
|
-
hostProvidedNames
|
|
268
|
-
...coreHostFunctions.keys(),
|
|
269
|
-
...coreHostConstants.keys(),
|
|
270
|
-
...plugins.flatMap((plugin) => [
|
|
271
|
-
...plugin.capabilities.hostFunctions.keys(),
|
|
272
|
-
...plugin.capabilities.nativeConstants.keys(),
|
|
273
|
-
...[...plugin.capabilities.hostFunctionsByDeclaration.values()].flatMap((names) => [...names.keys()]),
|
|
274
|
-
...[...plugin.capabilities.hostConstantsByDeclaration.values()].flatMap((names) => [...names.keys()]),
|
|
275
|
-
// A namespace root or singleton is equally the host's by name: the
|
|
276
|
-
// framework states `declare const __gea_Panel: {...}` module-locally
|
|
277
|
-
// for a root its host table claims, and no global declares it.
|
|
278
|
-
...plugin.capabilities.hostNamespaces.roots,
|
|
279
|
-
...[...plugin.capabilities.hostNamespaceRootsByDeclaration.values()].flatMap((names) => [...names]),
|
|
280
|
-
...plugin.capabilities.hostSingletons,
|
|
281
|
-
...[...plugin.capabilities.hostSingletonsByDeclaration.values()].flatMap((names) => [...names])
|
|
282
|
-
])
|
|
283
|
-
]),
|
|
282
|
+
hostProvidedNames,
|
|
284
283
|
hostNamespaceRoots: new Set(['Reflect', ...plugins.flatMap((plugin) => [...plugin.capabilities.hostNamespaces.roots])]),
|
|
285
284
|
hostNamespaceRootDeclarations,
|
|
286
285
|
// Every leaf path a plugin's own namespace tables publish -- see
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DeclarationId, StructuralTypeId } from '../identity/ids.js';
|
|
2
|
+
import type { ClassLayout } from '../projection/classes.js';
|
|
3
|
+
import { type Representation } from '../representation/model.js';
|
|
4
|
+
import type { ReflectionExposure } from './reflection-demand.js';
|
|
5
|
+
/**
|
|
6
|
+
* The program carriers a box holds with no dynamic protocol at all: no
|
|
7
|
+
* document table and no array table. The runtime builds both for every boxable
|
|
8
|
+
* type unless told otherwise (`gea::detail::dynamicProtocolOmitted`), so this
|
|
9
|
+
* is the one place that says which types nothing reads dynamically. The
|
|
10
|
+
* reflection census is the evidence: a carrier is omitted only when it has a
|
|
11
|
+
* row and the row is below `full`. No row, an incomplete census, or any
|
|
12
|
+
* sharing carrier at `full` keeps the protocol.
|
|
13
|
+
*/
|
|
14
|
+
export interface DynamicProtocolOmissions {
|
|
15
|
+
readonly classes: ReadonlySet<DeclarationId>;
|
|
16
|
+
readonly records: ReadonlySet<StructuralTypeId>;
|
|
17
|
+
/** Every array carrier of an omitted element identity. */
|
|
18
|
+
readonly arrays: readonly Representation[];
|
|
19
|
+
}
|
|
20
|
+
export declare const dynamicProtocolOmissionsOf: (classes: ReadonlyMap<DeclarationId, ClassLayout>, reflection: ReflectionExposure | undefined, representations: readonly Representation[]) => DynamicProtocolOmissions | null;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { abiKey, representationKey, walkRepresentation } from '../representation/model.js';
|
|
2
|
+
const programRecordShapeOf = (representation) => {
|
|
3
|
+
if (representation.kind === 'record' || representation.kind === 'record-with-index')
|
|
4
|
+
return representation.shapeId;
|
|
5
|
+
if (representation.kind === 'native-record-ref' && representation.native === null && representation.recursive === undefined)
|
|
6
|
+
return representation.shapeId;
|
|
7
|
+
return null;
|
|
8
|
+
};
|
|
9
|
+
// One physical runtime type per identity: the carriers it groups are every
|
|
10
|
+
// carrier that can name the same `ArrayObject<E>`, so one of them read
|
|
11
|
+
// dynamically keeps the protocol for all of them. A plain `function` element
|
|
12
|
+
// takes the callable identity without ever being eligible -- it shares the
|
|
13
|
+
// spelling of the callable families and the census does not row it -- and an
|
|
14
|
+
// element with no identity here (a string, a number, a union, `any`) is an
|
|
15
|
+
// array the runtime also builds for itself, out of the census's sight.
|
|
16
|
+
const arrayElementIdentityOf = (element) => {
|
|
17
|
+
switch (element.kind) {
|
|
18
|
+
case 'class-ref':
|
|
19
|
+
return { identity: JSON.stringify(['class', element.declaration]), eligible: true };
|
|
20
|
+
case 'record':
|
|
21
|
+
case 'record-with-index':
|
|
22
|
+
case 'native-record-ref': {
|
|
23
|
+
const shape = programRecordShapeOf(element);
|
|
24
|
+
return shape === null ? null : { identity: JSON.stringify(['record', shape]), eligible: true };
|
|
25
|
+
}
|
|
26
|
+
case 'function':
|
|
27
|
+
return { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: false };
|
|
28
|
+
case 'function-family':
|
|
29
|
+
case 'function-value-family':
|
|
30
|
+
return { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: true };
|
|
31
|
+
case 'function-value-dispatch':
|
|
32
|
+
return element.recursive === undefined ? { identity: JSON.stringify(['call', abiKey(element.abi)]), eligible: true } : null;
|
|
33
|
+
case 'function-and-constructor':
|
|
34
|
+
return { identity: JSON.stringify(['call-construct', abiKey(element.call), abiKey(element.construct)]), eligible: true };
|
|
35
|
+
default:
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
export const dynamicProtocolOmissionsOf = (classes, reflection, representations) => {
|
|
40
|
+
if (reflection === undefined || !reflection.complete)
|
|
41
|
+
return null;
|
|
42
|
+
const unread = (representation) => {
|
|
43
|
+
const demand = reflection.byRepresentation.get(representationKey(representation));
|
|
44
|
+
return demand !== undefined && demand.level !== 'full';
|
|
45
|
+
};
|
|
46
|
+
// A derived class reaches its dynamic protocol through its bases, so every
|
|
47
|
+
// base of a class kept below keeps its own.
|
|
48
|
+
const keptClasses = new Set();
|
|
49
|
+
for (const declaration of classes.keys()) {
|
|
50
|
+
const demand = reflection.classes.get(declaration);
|
|
51
|
+
if (demand !== undefined && demand.level !== 'full')
|
|
52
|
+
continue;
|
|
53
|
+
for (let base = declaration; base !== null && !keptClasses.has(base); base = classes.get(base)?.base ?? null)
|
|
54
|
+
keptClasses.add(base);
|
|
55
|
+
}
|
|
56
|
+
const recordRead = new Set();
|
|
57
|
+
const recordSeen = new Set();
|
|
58
|
+
const arrays = new Map();
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
for (const root of representations)
|
|
61
|
+
for (const representation of walkRepresentation(root, seen)) {
|
|
62
|
+
const shape = programRecordShapeOf(representation);
|
|
63
|
+
if (shape !== null) {
|
|
64
|
+
recordSeen.add(shape);
|
|
65
|
+
if (!unread(representation))
|
|
66
|
+
recordRead.add(shape);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (representation.kind !== 'array-object')
|
|
70
|
+
continue;
|
|
71
|
+
const element = representation.recursive === undefined ? arrayElementIdentityOf(representation.element) : null;
|
|
72
|
+
if (element === null)
|
|
73
|
+
continue;
|
|
74
|
+
const group = arrays.get(element.identity) ?? { members: [], omitted: true };
|
|
75
|
+
group.members.push(representation);
|
|
76
|
+
if (!element.eligible || !unread(representation))
|
|
77
|
+
group.omitted = false;
|
|
78
|
+
arrays.set(element.identity, group);
|
|
79
|
+
}
|
|
80
|
+
for (const [shape, demand] of reflection.records)
|
|
81
|
+
if (demand.level === 'full')
|
|
82
|
+
recordRead.add(shape);
|
|
83
|
+
return {
|
|
84
|
+
classes: new Set([...classes.keys()].filter((declaration) => !keptClasses.has(declaration))),
|
|
85
|
+
records: new Set([...recordSeen].filter((shape) => !recordRead.has(shape))),
|
|
86
|
+
arrays: [...arrays.values()].flatMap((group) => (group.omitted ? group.members : []))
|
|
87
|
+
};
|
|
88
|
+
};
|
package/dist/ir/lower.js
CHANGED
|
@@ -1119,6 +1119,7 @@ exactArmNarrowing) => {
|
|
|
1119
1119
|
exactArmNarrowing
|
|
1120
1120
|
};
|
|
1121
1121
|
const blockStarts = new Map();
|
|
1122
|
+
const bindingLifetimeOrigins = new Map();
|
|
1122
1123
|
const finiteCloseStarts = new Map();
|
|
1123
1124
|
const finiteCloseByStart = new Map();
|
|
1124
1125
|
for (const id of order) {
|
|
@@ -1221,6 +1222,15 @@ exactArmNarrowing) => {
|
|
|
1221
1222
|
finiteEntries.set(finiteCloseAtStart, { start: operationId, entry: block });
|
|
1222
1223
|
}
|
|
1223
1224
|
blockStarts.set(operationId, block);
|
|
1225
|
+
if (operation.family === 'binding' && operation.action === 'declare') {
|
|
1226
|
+
// A var belongs to the call's environment, even when its declaration is
|
|
1227
|
+
// inside a loop. An uninitialized lexical declaration still creates its
|
|
1228
|
+
// cell here; dropping that event lets a later conditional write move the
|
|
1229
|
+
// cell into a loop and reset it on every back edge.
|
|
1230
|
+
const origin = operation.blockScoped ? block : builder.blockOrder()[0];
|
|
1231
|
+
if (origin !== undefined && !bindingLifetimeOrigins.has(operation.declaration))
|
|
1232
|
+
bindingLifetimeOrigins.set(operation.declaration, origin);
|
|
1233
|
+
}
|
|
1224
1234
|
const finiteStart = finiteCloseStarts.get(operationId);
|
|
1225
1235
|
if (operation.family === 'destructuring' && operation.form === 'array-pattern-close' && finiteStart) {
|
|
1226
1236
|
const entry = finiteEntries.get(operation.id);
|
|
@@ -1257,10 +1267,11 @@ exactArmNarrowing) => {
|
|
|
1257
1267
|
join: info.join,
|
|
1258
1268
|
...(info.enclosingLoopBlocks.length > 0 ? { enclosingLoopBlocks: info.enclosingLoopBlocks } : {})
|
|
1259
1269
|
})), iteratorCloseRegions);
|
|
1270
|
+
const withBindingLifetimes = { ...sealed, bindingLifetimeOrigins };
|
|
1260
1271
|
if (!isGenerator)
|
|
1261
|
-
return
|
|
1272
|
+
return withBindingLifetimes;
|
|
1262
1273
|
const boundary = generatorPrologueBoundaryOf(graph, order, parameterPrologue, blockStarts, sealed);
|
|
1263
|
-
return boundary === null ?
|
|
1274
|
+
return boundary === null ? withBindingLifetimes : { ...withBindingLifetimes, generatorPrologueBoundary: boundary };
|
|
1264
1275
|
}
|
|
1265
1276
|
catch (error) {
|
|
1266
1277
|
throw new IrLoweringBlockedError(`IR verification failed for owner ${owner}: ${error.message}`);
|
package/dist/ir/model.d.ts
CHANGED
|
@@ -1478,6 +1478,8 @@ export interface IrBlock {
|
|
|
1478
1478
|
/** Every operation in a block, in execution order, terminator last. */
|
|
1479
1479
|
export declare const allOperationsOf: (block: IrBlock) => readonly IrOperation[];
|
|
1480
1480
|
export interface IrBody {
|
|
1481
|
+
/** Binding creation sites constrain cell lifetime even when no initializer writes a value. */
|
|
1482
|
+
readonly bindingLifetimeOrigins?: ReadonlyMap<DeclarationId, IrBlockId>;
|
|
1481
1483
|
/** Host binding/path resolution published before conversion certification. */
|
|
1482
1484
|
readonly hostNamespaceCensus?: import('./host-namespace-reads.js').HostNamespaceCensus;
|
|
1483
1485
|
/** Certified adaptations used by the published dense storage windows. */
|
|
@@ -0,0 +1,41 @@
|
|
|
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;
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
};
|
|
@@ -7,7 +7,8 @@ import type { SemanticGraph } from '../semantics/model/graph.js';
|
|
|
7
7
|
* `let x: T` with no initializer binds `undefined` (ECMA-262
|
|
8
8
|
* LexicalDeclaration evaluation with no Initializer), and the checker's
|
|
9
9
|
* definite-assignment analysis is scoped to one function body: a read in the
|
|
10
|
-
* declaring body is proven assigned
|
|
10
|
+
* declaring body is proven assigned, carries an undefined arm, or is rejected;
|
|
11
|
+
* a read from any other
|
|
11
12
|
* function -- a module helper, a closure -- is simply assumed assigned. So
|
|
12
13
|
* such a cell's declared type is a claim the language does not enforce there.
|
|
13
14
|
* A lazily loaded optional module (`let codec: Codec`, then
|
|
@@ -19,8 +20,8 @@ import type { SemanticGraph } from '../semantics/model/graph.js';
|
|
|
19
20
|
* read of it -- the storage is one carrier, and a read in the declaring body
|
|
20
21
|
* that the checker proved is still a load of that storage. A read feeding a
|
|
21
22
|
* position that states `T` converts through the ordinary checked unwrap. A
|
|
22
|
-
* cell every read of which sits in the declaring body
|
|
23
|
-
* carrier: the checker's analysis is the proof there.
|
|
23
|
+
* cell every read of which sits in the declaring body and excludes undefined
|
|
24
|
+
* keeps its declared carrier: the checker's analysis is the proof there.
|
|
24
25
|
*
|
|
25
26
|
* Same shape as an unwritten static field (`static-field-cells.ts`), whose
|
|
26
27
|
* carrier helper this shares.
|
|
@@ -2,6 +2,15 @@ import { withoutFunctionSpecialization } from '../identity/ids.js';
|
|
|
2
2
|
import { resultOf } from '../semantics/model/operands.js';
|
|
3
3
|
const ownerOf = (caller) => caller.kind === 'function' ? `f:${withoutFunctionSpecialization(caller.functionId)}` : `r:${caller.regionId}`;
|
|
4
4
|
export const unassignedBindingAbsenceOf = (graph) => {
|
|
5
|
+
const includesUndefined = (type, seen = new Set()) => {
|
|
6
|
+
if (seen.has(type))
|
|
7
|
+
return false;
|
|
8
|
+
seen.add(type);
|
|
9
|
+
const shape = graph.structuralTypes.get(type)?.shape;
|
|
10
|
+
return shape?.kind === 'primitive'
|
|
11
|
+
? shape.primitive === 'undefined'
|
|
12
|
+
: shape?.kind === 'union' && shape.members.some((member) => includesUndefined(member, seen));
|
|
13
|
+
};
|
|
5
14
|
const declared = new Map();
|
|
6
15
|
for (const operation of graph.operations.values()) {
|
|
7
16
|
if (operation.family !== 'binding' || operation.action !== 'declare' || !operation.mutable)
|
|
@@ -30,7 +39,10 @@ export const unassignedBindingAbsenceOf = (graph) => {
|
|
|
30
39
|
bucket.push(value.id);
|
|
31
40
|
else
|
|
32
41
|
reads.set(operation.declaration, [value.id]);
|
|
33
|
-
|
|
42
|
+
// An evolving, unannotated local can legally be read before a conditional
|
|
43
|
+
// write in the same function. Its read's published undefined arm is the
|
|
44
|
+
// checker's proof that the initial absence must survive the writer census.
|
|
45
|
+
if (ownerOf(operation.caller) !== cell.owner || includesUndefined(value.type))
|
|
34
46
|
declarations.add(operation.declaration);
|
|
35
47
|
}
|
|
36
48
|
for (const declaration of declarations) {
|
|
@@ -0,0 +1,32 @@
|
|
|
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>;
|
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
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;
|