@dancrumb/cant-compiler 0.0.0-stage → 0.10.0
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/bodies.d.ts +33 -0
- package/dist/bodies.d.ts.map +1 -0
- package/dist/bodies.js +512 -0
- package/dist/bodies.js.map +1 -0
- package/dist/check.d.ts +18 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +30 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +90 -0
- package/dist/cli.js.map +1 -0
- package/dist/compile.d.ts +20 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +777 -0
- package/dist/compile.js.map +1 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +262 -0
- package/dist/constants.js.map +1 -0
- package/dist/declarations.d.ts +54 -0
- package/dist/declarations.d.ts.map +1 -0
- package/dist/declarations.js +202 -0
- package/dist/declarations.js.map +1 -0
- package/dist/diagnostic.d.ts +19 -0
- package/dist/diagnostic.d.ts.map +1 -0
- package/dist/diagnostic.js +27 -0
- package/dist/diagnostic.js.map +1 -0
- package/dist/entities.d.ts +32 -0
- package/dist/entities.d.ts.map +1 -0
- package/dist/entities.js +235 -0
- package/dist/entities.js.map +1 -0
- package/dist/evaluate.d.ts +30 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +79 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/host.d.ts +8 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +13 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +93 -0
- package/dist/manifest.js.map +1 -0
- package/dist/modules.d.ts +84 -0
- package/dist/modules.d.ts.map +1 -0
- package/dist/modules.js +259 -0
- package/dist/modules.js.map +1 -0
- package/dist/program.d.ts +24 -0
- package/dist/program.d.ts.map +1 -0
- package/dist/program.js +38 -0
- package/dist/program.js.map +1 -0
- package/dist/resolve.d.ts +24 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +203 -0
- package/dist/resolve.js.map +1 -0
- package/dist/stdlib.d.ts +12 -0
- package/dist/stdlib.d.ts.map +1 -0
- package/dist/stdlib.js +38 -0
- package/dist/stdlib.js.map +1 -0
- package/dist/typecheck.d.ts +52 -0
- package/dist/typecheck.d.ts.map +1 -0
- package/dist/typecheck.js +1408 -0
- package/dist/typecheck.js.map +1 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +199 -0
- package/dist/types.js.map +1 -0
- package/dist/weave.d.ts +3 -0
- package/dist/weave.d.ts.map +1 -0
- package/dist/weave.js +40 -0
- package/dist/weave.js.map +1 -0
- package/package.json +47 -3
- package/README.md +0 -3
|
@@ -0,0 +1,1408 @@
|
|
|
1
|
+
import { error } from './diagnostic.js';
|
|
2
|
+
import { ConstantValues } from './evaluate.js';
|
|
3
|
+
import { BOOLEAN, constantKey, createAssignable, DATE_TIME, describe, DURATION, fieldType, isBase, NOTHING, NUMBER, optional, sameSymbol, TEXT, typeOf, typeOfSymbol, UNCHECKED, UNKNOWN, VOID, } from './types.js';
|
|
4
|
+
const NO_FACTS = new Map();
|
|
5
|
+
const extend = (facts, more) => (more.size === 0 ? facts : new Map([...facts, ...more]));
|
|
6
|
+
const symbolKey = (symbol) => `${symbol.module}::${symbol.name}`;
|
|
7
|
+
/** A built-in error, in `cant::Errors` (docs/adr/0023-error-handling.md). */
|
|
8
|
+
const builtInError = (name) => ({ kind: 'type', typeKind: 'error', name, module: 'cant::Errors' });
|
|
9
|
+
/** A result that holds a value of `value`, or one of `errors`. With no errors, it is just the value. */
|
|
10
|
+
const result = (value, errors, anyError = false) => errors.length === 0 && !anyError ? value : { kind: 'result', value, errors, anyError };
|
|
11
|
+
export function checkTypes(program) {
|
|
12
|
+
const diagnostics = [];
|
|
13
|
+
const shared = new SharedTypes(program);
|
|
14
|
+
for (const module of program.modules.values()) {
|
|
15
|
+
new TypeChecker(module, program, shared, (diagnostic) => {
|
|
16
|
+
diagnostics.push({ ...diagnostic, file: module.path });
|
|
17
|
+
}).run();
|
|
18
|
+
}
|
|
19
|
+
return { diagnostics, record: { types: shared.types, expected: shared.expected, unwrapped: shared.unwrapped, modelFields: shared.modelFields } };
|
|
20
|
+
}
|
|
21
|
+
/** What every module's checker shares: field types, and the types of constants. */
|
|
22
|
+
class SharedTypes {
|
|
23
|
+
assignable;
|
|
24
|
+
constants = new Map();
|
|
25
|
+
working = new Set();
|
|
26
|
+
types = new Map();
|
|
27
|
+
expected = new Map();
|
|
28
|
+
unwrapped = new Set();
|
|
29
|
+
/** The values of constant expressions, from the evaluator the interpreter shares. */
|
|
30
|
+
constantValues;
|
|
31
|
+
program;
|
|
32
|
+
models = new Map();
|
|
33
|
+
entities = new Map();
|
|
34
|
+
dataModels = new Map();
|
|
35
|
+
constructor(program) {
|
|
36
|
+
this.program = program;
|
|
37
|
+
this.assignable = createAssignable(this.modelFields);
|
|
38
|
+
this.constantValues = new ConstantValues(program.references);
|
|
39
|
+
}
|
|
40
|
+
modelFields = (model) => {
|
|
41
|
+
let fields = this.models.get(model);
|
|
42
|
+
if (fields === undefined) {
|
|
43
|
+
fields = new Map(model.fields.map((field) => [field.name.value, { type: fieldType(field, this.program.references), required: !field.optional }]));
|
|
44
|
+
this.models.set(model, fields);
|
|
45
|
+
}
|
|
46
|
+
return fields;
|
|
47
|
+
};
|
|
48
|
+
/** The model an entity is made of (docs/adr/0022-entities.md). */
|
|
49
|
+
modelOf(entity) {
|
|
50
|
+
const name = entity.lines.find((line) => line.type === 'modelsBlock')?.models[0];
|
|
51
|
+
const model = name === undefined ? undefined : this.program.references.get(name)?.declaration;
|
|
52
|
+
return model?.type === 'model' ? model : undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* An error's `data` model, as a model declaration: its named model, or one made from its
|
|
56
|
+
* anonymous fields, so both are matched the same way (docs/adr/0023-error-handling.md).
|
|
57
|
+
*/
|
|
58
|
+
dataModelOf(declaration) {
|
|
59
|
+
const data = declaration.data;
|
|
60
|
+
if (data === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
if (!Array.isArray(data)) {
|
|
63
|
+
const model = this.program.references.get(data)?.declaration;
|
|
64
|
+
return model?.type === 'model' ? model : undefined;
|
|
65
|
+
}
|
|
66
|
+
let model = this.dataModels.get(declaration);
|
|
67
|
+
if (model === undefined) {
|
|
68
|
+
model = { type: 'model', name: declaration.name, fields: data, position: declaration.position };
|
|
69
|
+
this.dataModels.set(declaration, model);
|
|
70
|
+
}
|
|
71
|
+
return model;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The fields an entity exposes, by the names it exposes them under, and its navigation fields
|
|
75
|
+
* (docs/adr/0022-entities.md, 0035). Hidden fields aren't part of its type.
|
|
76
|
+
*/
|
|
77
|
+
entityFields(symbol, entity) {
|
|
78
|
+
let fields = this.entities.get(entity);
|
|
79
|
+
if (fields !== undefined)
|
|
80
|
+
return fields;
|
|
81
|
+
const model = this.modelOf(entity);
|
|
82
|
+
const modelFields = model === undefined ? new Map() : this.modelFields(model);
|
|
83
|
+
const exposed = new Map();
|
|
84
|
+
const expose = entity.lines.filter((line) => line.type === 'exposeBlock').flatMap((line) => line.items);
|
|
85
|
+
const hidden = new Set(entity.lines
|
|
86
|
+
.filter((line) => line.type === 'hideBlock')
|
|
87
|
+
.flatMap((line) => line.items.flatMap((item) => (item.type === 'identifier' ? [item.value] : []))));
|
|
88
|
+
if (expose.some((item) => item.type === 'builtIn')) {
|
|
89
|
+
for (const [name, field] of modelFields)
|
|
90
|
+
if (!hidden.has(name))
|
|
91
|
+
exposed.set(name, field.type);
|
|
92
|
+
}
|
|
93
|
+
for (const item of expose) {
|
|
94
|
+
if (item.type === 'builtIn')
|
|
95
|
+
continue;
|
|
96
|
+
const field = modelFields.get(item.field.value);
|
|
97
|
+
if (field === undefined)
|
|
98
|
+
continue;
|
|
99
|
+
if (item.as !== undefined)
|
|
100
|
+
exposed.delete(item.field.value);
|
|
101
|
+
exposed.set(item.as?.value ?? item.field.value, field.type);
|
|
102
|
+
}
|
|
103
|
+
for (const [name, navigation] of this.navigationOf(symbol)) {
|
|
104
|
+
const far = typeOfSymbol(navigation.far);
|
|
105
|
+
exposed.set(name, navigation.farCardinality === '1' ? far : navigation.farCardinality === '0..1' ? optional(far) : { kind: 'set', element: far });
|
|
106
|
+
}
|
|
107
|
+
fields = exposed;
|
|
108
|
+
this.entities.set(entity, fields);
|
|
109
|
+
return fields;
|
|
110
|
+
}
|
|
111
|
+
navigationOf(symbol) {
|
|
112
|
+
return this.program.navigation.get(symbolKey(symbol)) ?? new Map();
|
|
113
|
+
}
|
|
114
|
+
/** Whether an entity's field, as exposed, has a default in its model, so `delete` resets it. */
|
|
115
|
+
entityFieldHasDefault(entity, name) {
|
|
116
|
+
const model = this.modelOf(entity);
|
|
117
|
+
if (model === undefined)
|
|
118
|
+
return false;
|
|
119
|
+
const expose = entity.lines.filter((line) => line.type === 'exposeBlock').flatMap((line) => line.items);
|
|
120
|
+
const renamed = expose.find((item) => item.type === 'exposedField' && item.as?.value === name);
|
|
121
|
+
const fieldName = renamed?.type === 'exposedField' ? renamed.field.value : name;
|
|
122
|
+
return model.fields.some((field) => field.name.value === fieldName && field.defaultValue !== undefined);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
class TypeChecker {
|
|
126
|
+
module;
|
|
127
|
+
references;
|
|
128
|
+
locals;
|
|
129
|
+
shared;
|
|
130
|
+
report;
|
|
131
|
+
/** The declared type of each local: parameters, assigned identifiers, loop variables and fields. */
|
|
132
|
+
bindings = new Map();
|
|
133
|
+
/** The `NUMBER` value of each local the compiler knows, for constant divisors (docs/adr/0023-error-handling.md). */
|
|
134
|
+
localValues = new Map();
|
|
135
|
+
/** The results held in identifiers, which must be handled. */
|
|
136
|
+
held = new Map();
|
|
137
|
+
/** The expressions whose results were used unchecked, which `return` can't use. */
|
|
138
|
+
usedResults = new Set();
|
|
139
|
+
/** The action being checked, for its return type and error clauses. */
|
|
140
|
+
action;
|
|
141
|
+
/** The trigger being checked, for `@result` and `@error`. */
|
|
142
|
+
trigger;
|
|
143
|
+
constructor(module, program, shared, report) {
|
|
144
|
+
this.module = module;
|
|
145
|
+
this.references = program.references;
|
|
146
|
+
this.locals = program.locals;
|
|
147
|
+
this.shared = shared;
|
|
148
|
+
this.report = report;
|
|
149
|
+
}
|
|
150
|
+
assignable(from, to, options) {
|
|
151
|
+
return this.shared.assignable(from, to, options);
|
|
152
|
+
}
|
|
153
|
+
run() {
|
|
154
|
+
for (const declaration of this.module.program?.children ?? []) {
|
|
155
|
+
switch (declaration.type) {
|
|
156
|
+
case 'action':
|
|
157
|
+
this.checkAction(declaration);
|
|
158
|
+
break;
|
|
159
|
+
case 'const':
|
|
160
|
+
this.constantType(declaration);
|
|
161
|
+
break;
|
|
162
|
+
case 'model':
|
|
163
|
+
this.checkFields(declaration.fields);
|
|
164
|
+
break;
|
|
165
|
+
case 'errorDeclaration':
|
|
166
|
+
if (Array.isArray(declaration.data))
|
|
167
|
+
this.checkFields(declaration.data);
|
|
168
|
+
break;
|
|
169
|
+
case 'trigger':
|
|
170
|
+
this.checkTrigger(declaration);
|
|
171
|
+
break;
|
|
172
|
+
default:
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// ----- Declarations -----
|
|
178
|
+
checkAction(action) {
|
|
179
|
+
this.action = action;
|
|
180
|
+
this.held.clear();
|
|
181
|
+
this.checkErrorClauses(action);
|
|
182
|
+
for (const parameter of action.parameters) {
|
|
183
|
+
const type = typeOf(parameter.valueType, this.references);
|
|
184
|
+
// A parameter with a default always has a value inside the action (docs/adr/0014-optionals.md).
|
|
185
|
+
this.bindings.set(parameter.name, parameter.optional && parameter.defaultValue === undefined ? optional(type) : type);
|
|
186
|
+
}
|
|
187
|
+
for (const parameter of action.parameters) {
|
|
188
|
+
if (parameter.defaultValue === undefined)
|
|
189
|
+
continue;
|
|
190
|
+
this.expectValue(parameter.defaultValue, typeOf(parameter.valueType, this.references), NO_FACTS, 'The default', true);
|
|
191
|
+
}
|
|
192
|
+
const { terminates } = this.block(action.children, NO_FACTS);
|
|
193
|
+
if (action.native === undefined && action.returnType.type !== 'voidType' && !terminates) {
|
|
194
|
+
this.report(error('missing-return', `${action.name.value} returns ${describe(this.returnType(action))}, but its body can end without returning one.`, action.name.position));
|
|
195
|
+
}
|
|
196
|
+
this.reportUnhandled();
|
|
197
|
+
this.action = undefined;
|
|
198
|
+
}
|
|
199
|
+
/** An error can be listed once, in one clause, and only `passes` can take `@all` (docs/adr/0023-error-handling.md). */
|
|
200
|
+
checkErrorClauses(action) {
|
|
201
|
+
const seen = new Map();
|
|
202
|
+
let all = false;
|
|
203
|
+
for (const name of [...action.raises, ...action.passes]) {
|
|
204
|
+
if (name.type === 'builtIn') {
|
|
205
|
+
if (all)
|
|
206
|
+
this.report(error('duplicate-error', '`@all` is listed twice.', name.position));
|
|
207
|
+
all = true;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const symbol = this.references.get(name);
|
|
211
|
+
if (symbol === undefined)
|
|
212
|
+
continue;
|
|
213
|
+
const earlier = seen.get(symbolKey(symbol));
|
|
214
|
+
if (earlier !== undefined) {
|
|
215
|
+
this.report(error('duplicate-error', `${symbol.name} is already listed, on line ${String(earlier.position.start.line)}. An error can be listed once, in one clause.`, name.position));
|
|
216
|
+
}
|
|
217
|
+
seen.set(symbolKey(symbol), name);
|
|
218
|
+
}
|
|
219
|
+
if (action.native !== undefined && action.passes.length > 0) {
|
|
220
|
+
this.report(error('native-passes', "A native action can't have a `passes` clause, since it doesn't call Cant actions. List its errors in `raises`.", action.passes[0]?.position ?? action.name.position));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
returnType(action) {
|
|
224
|
+
if (action.returnType.type === 'voidType')
|
|
225
|
+
return VOID;
|
|
226
|
+
const type = typeOf(action.returnType, this.references);
|
|
227
|
+
return action.optional ? optional(type) : type;
|
|
228
|
+
}
|
|
229
|
+
/** What calling an action gives: its value, or one of the errors it raises or passes. */
|
|
230
|
+
actionResult(action, extra = []) {
|
|
231
|
+
const errors = [...extra];
|
|
232
|
+
let anyError = false;
|
|
233
|
+
for (const name of [...action.raises, ...action.passes]) {
|
|
234
|
+
if (name.type === 'builtIn') {
|
|
235
|
+
anyError = true;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const symbol = this.references.get(name);
|
|
239
|
+
if (symbol !== undefined && !errors.some((other) => sameSymbol(other, symbol)))
|
|
240
|
+
errors.push(symbol);
|
|
241
|
+
}
|
|
242
|
+
return result(this.returnType(action), errors, anyError);
|
|
243
|
+
}
|
|
244
|
+
checkFields(fields) {
|
|
245
|
+
for (const field of fields)
|
|
246
|
+
this.bindings.set(field.name, fieldType(field, this.references));
|
|
247
|
+
for (const field of fields) {
|
|
248
|
+
if (field.defaultValue === undefined)
|
|
249
|
+
continue;
|
|
250
|
+
this.expectValue(field.defaultValue, typeOf(field.valueType, this.references), NO_FACTS, 'The default', false);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** A constant's type: the one it declares, or its value's (docs/adr/0034-top-level-constants.md). */
|
|
254
|
+
constantType(constant) {
|
|
255
|
+
const known = this.shared.constants.get(constant);
|
|
256
|
+
if (known !== undefined)
|
|
257
|
+
return known;
|
|
258
|
+
if (this.shared.working.has(constant)) {
|
|
259
|
+
this.report(error('constant-cycle', `${constant.name.value}'s value depends on itself.`, constant.name.position));
|
|
260
|
+
return UNCHECKED;
|
|
261
|
+
}
|
|
262
|
+
this.shared.working.add(constant);
|
|
263
|
+
let type;
|
|
264
|
+
if (constant.valueType === undefined) {
|
|
265
|
+
type = this.value(constant.value, NO_FACTS).type;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
type = typeOf(constant.valueType, this.references);
|
|
269
|
+
// A constant can declare an enum type and give one of its values (docs/adr/0034-top-level-constants.md).
|
|
270
|
+
this.expectValue(constant.value, type, NO_FACTS, `${constant.name.value}'s value`, true);
|
|
271
|
+
}
|
|
272
|
+
this.shared.working.delete(constant);
|
|
273
|
+
this.shared.constants.set(constant, type);
|
|
274
|
+
return type;
|
|
275
|
+
}
|
|
276
|
+
checkTrigger(trigger) {
|
|
277
|
+
this.trigger = trigger;
|
|
278
|
+
const action = this.references.get(trigger.action)?.declaration;
|
|
279
|
+
const facts = NO_FACTS;
|
|
280
|
+
if (trigger.source.value === 'TestCase')
|
|
281
|
+
this.checkTestCaseParameters(trigger.parameters ?? []);
|
|
282
|
+
else
|
|
283
|
+
for (const parameter of trigger.parameters ?? [])
|
|
284
|
+
this.value(parameter.value, facts);
|
|
285
|
+
if (trigger.auth !== undefined)
|
|
286
|
+
this.condition(trigger.auth, facts, 'An `auth` rule');
|
|
287
|
+
for (const argument of trigger.arguments ?? []) {
|
|
288
|
+
const parameter = action?.type === 'action' ? action.parameters.find((candidate) => candidate.name.value === argument.name.value) : undefined;
|
|
289
|
+
if (parameter === undefined) {
|
|
290
|
+
this.value(argument.value, facts);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
this.argument(argument.value, parameter.valueType, parameter.optional, facts, argument.name.value);
|
|
294
|
+
}
|
|
295
|
+
let asserted = facts;
|
|
296
|
+
for (const assertion of trigger.assertions ?? []) {
|
|
297
|
+
this.condition(assertion.test, asserted, 'An assertion');
|
|
298
|
+
this.expectText(assertion.message, asserted, "An assertion's message");
|
|
299
|
+
// Each assertion is only reached when those before it held (docs/adr/0037-test-case-trigger-source.md).
|
|
300
|
+
asserted = extend(asserted, this.narrow(assertion.test, true, asserted));
|
|
301
|
+
}
|
|
302
|
+
this.trigger = undefined;
|
|
303
|
+
}
|
|
304
|
+
/** `name: TEXT`, `now: DATE_TIME` and a `subject` object matched against its entity's model (docs/adr/0037-test-case-trigger-source.md, 0026). */
|
|
305
|
+
checkTestCaseParameters(parameters) {
|
|
306
|
+
for (const parameter of parameters) {
|
|
307
|
+
switch (parameter.name.value) {
|
|
308
|
+
case 'name':
|
|
309
|
+
this.expectText(parameter.value, NO_FACTS, "A test case's name");
|
|
310
|
+
break;
|
|
311
|
+
case 'now':
|
|
312
|
+
this.expectValue(parameter.value, DATE_TIME, NO_FACTS, "A test case's now", false);
|
|
313
|
+
break;
|
|
314
|
+
case 'subject':
|
|
315
|
+
this.checkTestSubject(parameter.value);
|
|
316
|
+
break;
|
|
317
|
+
default:
|
|
318
|
+
this.value(parameter.value, NO_FACTS);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
checkTestSubject(value) {
|
|
323
|
+
if (value.type !== 'object') {
|
|
324
|
+
this.report(error('type-mismatch', "A test case's subject must be an object value naming its @entity and its fields.", value.position));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const entityItem = value.items.find((item) => item.name.type === 'builtIn' && item.name.name === 'entity');
|
|
328
|
+
const entityName = entityItem?.value;
|
|
329
|
+
const symbol = entityName?.type === 'identifier' || entityName?.type === 'qualifiedIdentifier' ? this.references.get(entityName) : undefined;
|
|
330
|
+
const fields = new Map();
|
|
331
|
+
for (const item of value.items) {
|
|
332
|
+
if (item.name.type === 'builtIn')
|
|
333
|
+
continue;
|
|
334
|
+
fields.set(item.name.type === 'string' ? textOf(item.name) : item.name.value, this.value(item.value, NO_FACTS).type);
|
|
335
|
+
}
|
|
336
|
+
const entity = symbol?.declaration;
|
|
337
|
+
if (entity?.type !== 'entity')
|
|
338
|
+
return;
|
|
339
|
+
const model = this.shared.modelOf(entity);
|
|
340
|
+
if (model === undefined)
|
|
341
|
+
return;
|
|
342
|
+
const anonymous = { kind: 'anonymous', fields };
|
|
343
|
+
const modelType = { kind: 'model', symbol: { kind: 'type', typeKind: 'model', name: model.name.value, module: symbol?.module ?? '' }, declaration: model };
|
|
344
|
+
if (!this.assignable(anonymous, modelType)) {
|
|
345
|
+
this.report(error('type-mismatch', `This subject's fields don't match ${entity.name.value}'s model, ${model.name.value}: it must give every required field, each with a value of its type, and nothing else.`, value.position));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// ----- Statements -----
|
|
349
|
+
/** Check a block's statements, and say whether it always ends by leaving it: by returning, raising, passing, breaking or continuing. */
|
|
350
|
+
block(statements, facts) {
|
|
351
|
+
let current = facts;
|
|
352
|
+
for (const statement of statements) {
|
|
353
|
+
const outcome = this.statement(statement, current, false);
|
|
354
|
+
current = outcome.facts;
|
|
355
|
+
if (outcome.terminates)
|
|
356
|
+
return { facts: current, terminates: true };
|
|
357
|
+
}
|
|
358
|
+
return { facts: current, terminates: false };
|
|
359
|
+
}
|
|
360
|
+
statement(statement, facts, ignored) {
|
|
361
|
+
const go = { facts, terminates: false };
|
|
362
|
+
switch (statement.type) {
|
|
363
|
+
case 'expressionStatement':
|
|
364
|
+
// A call whose result isn't used is ignored (docs/adr/0023-error-handling.md).
|
|
365
|
+
this.expression(statement.expression, facts);
|
|
366
|
+
return go;
|
|
367
|
+
case 'assignment':
|
|
368
|
+
this.assignment(statement.target, statement.value, facts, ignored);
|
|
369
|
+
return go;
|
|
370
|
+
case 'delete':
|
|
371
|
+
this.delete(statement.target, facts, ignored);
|
|
372
|
+
return go;
|
|
373
|
+
case 'ignoredStatement':
|
|
374
|
+
return this.statement(statement.statement, facts, true);
|
|
375
|
+
case 'return':
|
|
376
|
+
this.return(statement, facts);
|
|
377
|
+
return { facts, terminates: true };
|
|
378
|
+
case 'raise':
|
|
379
|
+
this.raise(statement, facts);
|
|
380
|
+
return { facts, terminates: true };
|
|
381
|
+
case 'pass':
|
|
382
|
+
this.pass(statement.argument, facts);
|
|
383
|
+
return { facts, terminates: true };
|
|
384
|
+
case 'assert':
|
|
385
|
+
this.condition(statement.test, facts, 'An `assert`');
|
|
386
|
+
if (statement.message !== undefined)
|
|
387
|
+
this.expectText(statement.message, facts, "An `assert`'s message");
|
|
388
|
+
return { facts: extend(facts, this.narrow(statement.test, true, facts)), terminates: false };
|
|
389
|
+
case 'break':
|
|
390
|
+
case 'continue':
|
|
391
|
+
return { facts, terminates: true };
|
|
392
|
+
case 'for': {
|
|
393
|
+
const iterable = this.value(statement.iterable, facts).type;
|
|
394
|
+
this.bindings.set(statement.variable, this.elementType(iterable, statement.iterable.position));
|
|
395
|
+
this.block(statement.children, facts);
|
|
396
|
+
return go;
|
|
397
|
+
}
|
|
398
|
+
case 'while': {
|
|
399
|
+
this.condition(statement.test, facts, 'A `while` condition');
|
|
400
|
+
this.block(statement.children, extend(facts, this.narrow(statement.test, true, facts)));
|
|
401
|
+
// `while true` only ends by returning, raising or passing, unless its body breaks out.
|
|
402
|
+
const forever = statement.test.type === 'boolean' && statement.test.value && !breaks(statement.children);
|
|
403
|
+
return { facts, terminates: forever };
|
|
404
|
+
}
|
|
405
|
+
case 'if': {
|
|
406
|
+
this.condition(statement.test, facts, 'An `if` condition');
|
|
407
|
+
const whenTrue = extend(facts, this.narrow(statement.test, true, facts));
|
|
408
|
+
const whenFalse = extend(facts, this.narrow(statement.test, false, facts));
|
|
409
|
+
const [consequent, alternate] = statement.children;
|
|
410
|
+
const then = this.block(consequent.children, whenTrue);
|
|
411
|
+
const otherwise = alternate === undefined
|
|
412
|
+
? { facts: whenFalse, terminates: false }
|
|
413
|
+
: alternate.type === 'if'
|
|
414
|
+
? this.statement(alternate, whenFalse, false)
|
|
415
|
+
: this.block(alternate.children, whenFalse);
|
|
416
|
+
if (then.terminates && otherwise.terminates)
|
|
417
|
+
return { facts, terminates: true };
|
|
418
|
+
// After an `if` whose branch always leaves, the code only runs when the other branch's
|
|
419
|
+
// condition held, so it keeps that narrowing (docs/adr/0016-handling-optional-values.md).
|
|
420
|
+
if (then.terminates)
|
|
421
|
+
return { facts: whenFalse, terminates: false };
|
|
422
|
+
if (otherwise.terminates && alternate !== undefined)
|
|
423
|
+
return { facts: whenTrue, terminates: false };
|
|
424
|
+
return go;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** The type of each element a `for` loop gives (docs/adr/0021-collections.md). */
|
|
429
|
+
elementType(iterable, position) {
|
|
430
|
+
switch (iterable.kind) {
|
|
431
|
+
case 'array':
|
|
432
|
+
case 'set':
|
|
433
|
+
return iterable.element.kind === 'nothing' ? UNCHECKED : iterable.element;
|
|
434
|
+
case 'unchecked':
|
|
435
|
+
return UNCHECKED;
|
|
436
|
+
case 'map':
|
|
437
|
+
this.report(error('loop-over-map', "A `for` loop can't loop over a map. Loop over its names, with `Collections::keys`.", position));
|
|
438
|
+
return UNCHECKED;
|
|
439
|
+
case 'optional':
|
|
440
|
+
this.uncheckedOptional(iterable, position);
|
|
441
|
+
return UNCHECKED;
|
|
442
|
+
default:
|
|
443
|
+
this.report(error('type-mismatch', `A \`for\` loop needs an array or a set, but this is ${describe(iterable)}.`, position));
|
|
444
|
+
return UNCHECKED;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
assignment(target, value, facts, ignored) {
|
|
448
|
+
if (target.type === 'identifier') {
|
|
449
|
+
// Giving a result to an identifier doesn't use it, so it holds the result unchecked.
|
|
450
|
+
const typed = this.expression(value, facts);
|
|
451
|
+
if (typed.type.kind === 'void') {
|
|
452
|
+
this.report(error('void-value', 'This call returns VOID, so there is no value to give the name.', value.position));
|
|
453
|
+
}
|
|
454
|
+
if (this.locals.get(target)?.declaration !== target)
|
|
455
|
+
return;
|
|
456
|
+
this.bindings.set(target, typed.type);
|
|
457
|
+
const known = this.constant(value);
|
|
458
|
+
if (known !== undefined)
|
|
459
|
+
this.localValues.set(target, known);
|
|
460
|
+
if (typed.type.kind === 'result') {
|
|
461
|
+
// `owner := ignore FindUser(email)` keeps the result, already marked as ignored.
|
|
462
|
+
const markedIgnored = value.type === 'unary' && value.operator === 'ignore';
|
|
463
|
+
this.held.set(target, { name: target, errors: typed.type.errors, anyError: typed.type.anyError, handled: markedIgnored, checked: new Set() });
|
|
464
|
+
}
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
// A member or element: the value must fit what it holds, and can't be an unchecked optional.
|
|
468
|
+
const slot = this.value(target, facts).type;
|
|
469
|
+
const expected = slot.kind === 'optional' ? slot.inner : slot;
|
|
470
|
+
this.expectValue(value, expected, facts, 'The value', false);
|
|
471
|
+
this.statementErrors(target, facts, ignored, 'assign');
|
|
472
|
+
}
|
|
473
|
+
/** `delete` clears an optional field, or removes a member or element (docs/adr/0021-collections.md, 0027). */
|
|
474
|
+
delete(target, facts, ignored) {
|
|
475
|
+
if (target.type !== 'member') {
|
|
476
|
+
this.value(target, facts);
|
|
477
|
+
this.statementErrors(target, facts, ignored, 'delete');
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const object = this.value(target.object, facts).type;
|
|
481
|
+
const field = this.memberOf(object, target.property, target.object.position);
|
|
482
|
+
if ((object.kind === 'model' || object.kind === 'entity' || object.kind === 'anonymous') && field.kind !== 'optional' && field.kind !== 'unchecked') {
|
|
483
|
+
// A field with a default is set back to its default, so it can be deleted.
|
|
484
|
+
const hasDefault = object.kind === 'model'
|
|
485
|
+
? object.declaration.fields.some((candidate) => candidate.name.value === target.property.value && candidate.defaultValue !== undefined)
|
|
486
|
+
: object.kind === 'entity' && this.shared.entityFieldHasDefault(object.declaration, target.property.value);
|
|
487
|
+
if (!hasDefault) {
|
|
488
|
+
this.report(error('delete-required', `${target.property.value} is a required field, so it can't be deleted. \`delete\` only clears optional fields.`, target.property.position));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
this.statementErrors(target, facts, ignored, 'delete');
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* The errors an assignment or `delete` can raise from the action, which it must list in
|
|
495
|
+
* `raises` unless the statement is ignored (docs/adr/0023-error-handling.md, 0021, 0027, 0035).
|
|
496
|
+
*/
|
|
497
|
+
statementErrors(target, facts, ignored, kind) {
|
|
498
|
+
if (target.type === 'index') {
|
|
499
|
+
const object = this.peek(target.object, facts);
|
|
500
|
+
const literalName = target.index.type === 'string' && target.index.parts.every((part) => part.type === 'text');
|
|
501
|
+
if (object.kind === 'array' && kind === 'assign') {
|
|
502
|
+
const index = this.constantNumber(target.index);
|
|
503
|
+
if (index === undefined || index < 0)
|
|
504
|
+
this.requireRaised('IndexBeforeStart', target.index.position, ignored, 'assigning to an index that may be before the start of the array');
|
|
505
|
+
}
|
|
506
|
+
if ((object.kind === 'model' || object.kind === 'entity') && !literalName) {
|
|
507
|
+
if (kind === 'assign')
|
|
508
|
+
this.requireRaised('UnknownField', target.index.position, ignored, 'assigning to a field named by an expression');
|
|
509
|
+
else
|
|
510
|
+
this.requireRaised('RequiredField', target.index.position, ignored, 'deleting a field named by an expression');
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (target.type === 'member' && kind === 'assign') {
|
|
514
|
+
const object = this.peek(target.object, facts);
|
|
515
|
+
if (object.kind === 'entity' && this.shared.navigationOf(object.symbol).has(target.property.value)) {
|
|
516
|
+
this.requireRaised('CardinalityViolation', target.property.position, ignored, 'changing a relationship');
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
/** Report a statement that can raise a built-in error the action doesn't declare. */
|
|
521
|
+
requireRaised(name, position, ignored, what) {
|
|
522
|
+
if (ignored || this.action === undefined)
|
|
523
|
+
return;
|
|
524
|
+
const declared = this.action.raises.some((raised) => {
|
|
525
|
+
const symbol = this.references.get(raised);
|
|
526
|
+
return symbol !== undefined && sameSymbol(symbol, builtInError(name));
|
|
527
|
+
});
|
|
528
|
+
if (declared)
|
|
529
|
+
return;
|
|
530
|
+
this.report(error('undeclared-error', `${what[0]?.toUpperCase() ?? ''}${what.slice(1)} can raise ${name}. List it in \`raises\`, with \`cant::Errors\` imported, or put \`ignore\` before the statement.`, position));
|
|
531
|
+
}
|
|
532
|
+
return(statement, facts) {
|
|
533
|
+
const action = this.action;
|
|
534
|
+
if (action === undefined)
|
|
535
|
+
return;
|
|
536
|
+
const expected = this.returnType(action);
|
|
537
|
+
const argument = statement.argument;
|
|
538
|
+
if (expected.kind === 'void') {
|
|
539
|
+
if (argument !== undefined) {
|
|
540
|
+
this.report(error('return-value', `${action.name.value} returns VOID, so \`return\` can't give a value.`, argument.position));
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (argument === undefined) {
|
|
545
|
+
this.report(error('return-value', `${action.name.value} returns ${describe(expected)}, so \`return\` needs a value.`, statement.position));
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
if (argument.type === 'nothing') {
|
|
549
|
+
if (expected.kind !== 'optional') {
|
|
550
|
+
this.report(error('return-value', 'NOTHING can only be returned from an action whose return type is marked `?`.', argument.position));
|
|
551
|
+
}
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const typed = this.expectValue(argument, expected, facts, 'The returned value', true);
|
|
555
|
+
// A value that may hold an error can't be returned (docs/adr/0023-error-handling.md).
|
|
556
|
+
if (this.containsUsedResult(argument)) {
|
|
557
|
+
this.report(error('return-unchecked', 'This uses a result that may hold an error, which `return` can\'t give. Check it with `instanceOf` first, or use it before the `return`, or `pass` it.', argument.position));
|
|
558
|
+
}
|
|
559
|
+
// Converting a value that isn't known now to an enum is checked when the program runs, and
|
|
560
|
+
// raises InvalidEnumValue from the action (docs/adr/0013-enums.md).
|
|
561
|
+
const target = expected.kind === 'optional' ? expected.inner : expected;
|
|
562
|
+
if (target.kind === 'enum' && typed.type.kind === 'base' && typed.literal === undefined) {
|
|
563
|
+
this.requireRaised('InvalidEnumValue', argument.position, false, `returning a ${typed.type.name} as ${describe(target)}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/** `raise E(…)` or `raise E with value` (docs/adr/0023-error-handling.md). */
|
|
567
|
+
raise(statement, facts) {
|
|
568
|
+
const symbol = this.references.get(statement.error);
|
|
569
|
+
const action = this.action;
|
|
570
|
+
if (symbol === undefined || action === undefined)
|
|
571
|
+
return;
|
|
572
|
+
const listed = action.raises.some((name) => {
|
|
573
|
+
const raised = this.references.get(name);
|
|
574
|
+
return raised !== undefined && sameSymbol(raised, symbol);
|
|
575
|
+
});
|
|
576
|
+
if (!listed) {
|
|
577
|
+
this.report(error('undeclared-error', `${action.name.value} doesn't list ${symbol.name} in its \`raises\` clause, so it can't raise it.`, statement.error.position));
|
|
578
|
+
}
|
|
579
|
+
const declaration = symbol.declaration?.type === 'errorDeclaration' ? symbol.declaration : undefined;
|
|
580
|
+
const data = declaration === undefined ? undefined : this.shared.dataModelOf(declaration);
|
|
581
|
+
if (data === undefined) {
|
|
582
|
+
if ((statement.arguments ?? []).length > 0 || statement.data !== undefined) {
|
|
583
|
+
this.report(error('no-data', `${symbol.name} has no \`data\`, so it is raised without any.`, statement.error.position));
|
|
584
|
+
}
|
|
585
|
+
for (const argument of statement.arguments ?? [])
|
|
586
|
+
this.value(argument.value, facts);
|
|
587
|
+
if (statement.data !== undefined)
|
|
588
|
+
this.value(statement.data, facts);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const dataType = { kind: 'model', symbol: { kind: 'type', typeKind: 'model', name: data.name.value, module: symbol.module }, declaration: data };
|
|
592
|
+
if (statement.data !== undefined) {
|
|
593
|
+
this.expectValue(statement.data, dataType, facts, `${symbol.name}'s data`, false);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
// The `data` model's fields take the place of parameters (docs/adr/0020-action-parameters.md).
|
|
597
|
+
const fields = this.shared.modelFields(data);
|
|
598
|
+
const given = new Set();
|
|
599
|
+
for (const argument of statement.arguments ?? []) {
|
|
600
|
+
const name = argument.name?.value ?? (argument.value.type === 'identifier' ? argument.value.value : undefined);
|
|
601
|
+
const field = name === undefined ? undefined : fields.get(name);
|
|
602
|
+
if (name === undefined || field === undefined) {
|
|
603
|
+
this.report(error('unknown-parameter', name === undefined ? `Name the field of ${symbol.name}'s data this is for, as in field := value.` : `${symbol.name}'s data has no field called ${name}.`, argument.position));
|
|
604
|
+
this.value(argument.value, facts);
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
given.add(name);
|
|
608
|
+
this.expectValue(argument.value, field.type.kind === 'optional' ? field.type.inner : field.type, facts, `The value for ${name}`, true);
|
|
609
|
+
}
|
|
610
|
+
const missing = [...fields].filter(([name, field]) => field.required && !given.has(name)).map(([name]) => name);
|
|
611
|
+
if (missing.length > 0) {
|
|
612
|
+
this.report(error('missing-argument', `${symbol.name}'s data needs ${missing.join(', ')}.`, statement.error.position));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
/** `pass` an error, or an unchecked result, along (docs/adr/0023-error-handling.md). */
|
|
616
|
+
pass(argument, facts) {
|
|
617
|
+
const action = this.action;
|
|
618
|
+
const typed = this.expression(argument, facts);
|
|
619
|
+
this.markHandled(argument);
|
|
620
|
+
if (action === undefined || typed.type.kind === 'unchecked')
|
|
621
|
+
return;
|
|
622
|
+
const type = typed.type;
|
|
623
|
+
let errors;
|
|
624
|
+
let anyError;
|
|
625
|
+
if (type.kind === 'result') {
|
|
626
|
+
errors = type.errors;
|
|
627
|
+
anyError = type.anyError;
|
|
628
|
+
const expected = this.returnType(action);
|
|
629
|
+
if (expected.kind !== 'void' && type.value.kind !== 'void' && !this.assignable(type.value, expected, { enumConversion: true })) {
|
|
630
|
+
this.report(error('type-mismatch', `\`pass\` returns this result's value when it has one, so it must be ${describe(expected)}, but it is ${describe(type.value)}.`, argument.position));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
else if (type.kind === 'error') {
|
|
634
|
+
errors = type.symbol !== undefined ? [type.symbol] : (type.among ?? []);
|
|
635
|
+
anyError = type.symbol === undefined && type.among === undefined;
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
this.report(error('pass-non-error', `\`pass\` takes an error, or a result that may hold one, but this is ${describe(type)}.`, argument.position));
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const passesAll = action.passes.some((name) => name.type === 'builtIn');
|
|
642
|
+
if (passesAll)
|
|
643
|
+
return;
|
|
644
|
+
if (anyError) {
|
|
645
|
+
this.report(error('undeclared-error', `This may hold any error, so ${action.name.value} can only pass it with \`passes @all\`.`, argument.position));
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
const declared = [...action.raises, ...action.passes].flatMap((name) => {
|
|
649
|
+
const symbol = name.type === 'builtIn' ? undefined : this.references.get(name);
|
|
650
|
+
return symbol === undefined ? [] : [symbol];
|
|
651
|
+
});
|
|
652
|
+
const undeclared = errors.filter((symbol) => !declared.some((other) => sameSymbol(other, symbol)));
|
|
653
|
+
if (undeclared.length > 0) {
|
|
654
|
+
this.report(error('undeclared-error', `${action.name.value} can't pass ${undeclared.map((symbol) => symbol.name).join(' or ')}: list ${undeclared.length === 1 ? 'it' : 'them'} in its \`passes\` clause.`, argument.position));
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
/** Report the results held in identifiers that were never handled (docs/adr/0023-error-handling.md). */
|
|
658
|
+
reportUnhandled() {
|
|
659
|
+
for (const held of this.held.values()) {
|
|
660
|
+
if (held.handled)
|
|
661
|
+
continue;
|
|
662
|
+
const unchecked = held.errors.filter((symbol) => !held.checked.has(symbolKey(symbol)));
|
|
663
|
+
if (unchecked.length === 0 && !held.anyError && held.checked.size > 0)
|
|
664
|
+
continue;
|
|
665
|
+
const what = held.anyError ? 'any error' : unchecked.map((symbol) => symbol.name).join(' or ');
|
|
666
|
+
this.report(error('unhandled-result', `${held.name.value} may hold ${what}, which is never handled. Check it with \`instanceOf\`, \`pass\` it, \`ignore\` it, or use it.`, held.name.position));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** Mark a result held in an identifier as handled. */
|
|
670
|
+
markHandled(expression) {
|
|
671
|
+
if (expression.type !== 'identifier')
|
|
672
|
+
return;
|
|
673
|
+
const declaration = this.locals.get(expression)?.declaration;
|
|
674
|
+
const held = declaration === undefined ? undefined : this.held.get(declaration);
|
|
675
|
+
if (held !== undefined)
|
|
676
|
+
held.handled = true;
|
|
677
|
+
}
|
|
678
|
+
containsUsedResult(expression) {
|
|
679
|
+
let found = false;
|
|
680
|
+
const walk = (node) => {
|
|
681
|
+
if (found || typeof node !== 'object' || node === null)
|
|
682
|
+
return;
|
|
683
|
+
if (Array.isArray(node)) {
|
|
684
|
+
for (const child of node)
|
|
685
|
+
walk(child);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
if (this.usedResults.has(node)) {
|
|
689
|
+
found = true;
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
for (const [key, value] of Object.entries(node))
|
|
693
|
+
if (key !== 'position')
|
|
694
|
+
walk(value);
|
|
695
|
+
};
|
|
696
|
+
walk(expression);
|
|
697
|
+
return found;
|
|
698
|
+
}
|
|
699
|
+
// ----- Checks on values -----
|
|
700
|
+
/** Check that an expression's value fits `expected`, and report it if not. */
|
|
701
|
+
expectValue(expression, expected, facts, what, enumConversion) {
|
|
702
|
+
const typed = this.value(expression, facts);
|
|
703
|
+
this.shared.expected.set(expression, expected);
|
|
704
|
+
const options = { enumConversion, ...(typed.literal !== undefined && { literal: typed.literal }) };
|
|
705
|
+
if (!this.assignable(typed.type, expected, options))
|
|
706
|
+
this.mismatch(typed.type, expected, expression.position, what);
|
|
707
|
+
return typed;
|
|
708
|
+
}
|
|
709
|
+
mismatch(found, expected, position, what) {
|
|
710
|
+
if (found.kind === 'optional' && expected.kind !== 'optional' && this.assignable(found.inner, expected)) {
|
|
711
|
+
this.uncheckedOptional(found, position);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
this.report(error('type-mismatch', `${what} must be ${describe(expected)}, but this is ${describe(found)}.`, position));
|
|
715
|
+
}
|
|
716
|
+
uncheckedOptional(found, position) {
|
|
717
|
+
this.report(error('unchecked-optional', `This is ${describe(found)}, which might be NOTHING. Check it with \`isEmpty\` first, or give a fallback with \`orElse\`.`, position));
|
|
718
|
+
}
|
|
719
|
+
condition(expression, facts, what) {
|
|
720
|
+
this.expectValue(expression, BOOLEAN, facts, what, false);
|
|
721
|
+
}
|
|
722
|
+
expectText(expression, facts, what) {
|
|
723
|
+
this.expectValue(expression, TEXT, facts, what, false);
|
|
724
|
+
}
|
|
725
|
+
/** Check an argument, and say whether converting it to an enum needs a check when the program runs. */
|
|
726
|
+
argument(value, parameterType, parameterOptional, facts, name) {
|
|
727
|
+
const expected = typeOf(parameterType, this.references);
|
|
728
|
+
const typed = this.value(value, facts);
|
|
729
|
+
// An optional can be passed to an optional parameter of its type (docs/adr/0014-optionals.md).
|
|
730
|
+
const target = parameterOptional ? optional(expected) : expected;
|
|
731
|
+
this.shared.expected.set(value, target);
|
|
732
|
+
const options = { enumConversion: true, ...(typed.literal !== undefined && { literal: typed.literal }) };
|
|
733
|
+
if (!this.assignable(typed.type, target, options)) {
|
|
734
|
+
this.mismatch(typed.type, expected, value.position, `The argument for ${name}`);
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
const given = typed.type.kind === 'optional' ? typed.type.inner : typed.type;
|
|
738
|
+
return expected.kind === 'enum' && given.kind === 'base' && typed.literal === undefined;
|
|
739
|
+
}
|
|
740
|
+
// ----- Expressions -----
|
|
741
|
+
/**
|
|
742
|
+
* An expression's type when its value is used. A result that may hold an error is used
|
|
743
|
+
* unchecked, which panics if it holds one, and gives its value (docs/adr/0023-error-handling.md).
|
|
744
|
+
*/
|
|
745
|
+
value(expression, facts) {
|
|
746
|
+
const typed = this.expression(expression, facts);
|
|
747
|
+
if (typed.type.kind !== 'result')
|
|
748
|
+
return typed;
|
|
749
|
+
this.usedResults.add(expression);
|
|
750
|
+
this.shared.unwrapped.add(expression);
|
|
751
|
+
this.markHandled(expression);
|
|
752
|
+
if (typed.type.value.kind === 'void') {
|
|
753
|
+
this.report(error('void-value', "This result never holds a value, so it can't be used as one. Check it with `instanceOf`, `pass` it or `ignore` it.", expression.position));
|
|
754
|
+
return { type: UNCHECKED };
|
|
755
|
+
}
|
|
756
|
+
return { type: typed.type.value };
|
|
757
|
+
}
|
|
758
|
+
/** An expression's type, without using a result it may give. */
|
|
759
|
+
expression(expression, facts) {
|
|
760
|
+
const typed = this.typeExpression(expression, facts);
|
|
761
|
+
this.shared.types.set(expression, typed.type);
|
|
762
|
+
return typed;
|
|
763
|
+
}
|
|
764
|
+
typeExpression(expression, facts) {
|
|
765
|
+
switch (expression.type) {
|
|
766
|
+
case 'identifier':
|
|
767
|
+
case 'qualifiedIdentifier':
|
|
768
|
+
return { type: this.nameType(expression, facts) };
|
|
769
|
+
case 'builtIn':
|
|
770
|
+
return { type: facts.get(`@${expression.name}`) ?? this.builtInType(expression) };
|
|
771
|
+
case 'string': {
|
|
772
|
+
for (const part of expression.parts) {
|
|
773
|
+
if (part.type === 'interpolation')
|
|
774
|
+
this.interpolated(part.expression, facts);
|
|
775
|
+
}
|
|
776
|
+
const constant = expression.parts.every((part) => part.type === 'text');
|
|
777
|
+
return constant ? { type: TEXT, literal: constantKey(expression).key } : { type: TEXT };
|
|
778
|
+
}
|
|
779
|
+
case 'number':
|
|
780
|
+
case 'boolean':
|
|
781
|
+
case 'dateTime':
|
|
782
|
+
case 'duration': {
|
|
783
|
+
const { key, type } = constantKey(expression);
|
|
784
|
+
return { type: { kind: 'base', name: type }, literal: key };
|
|
785
|
+
}
|
|
786
|
+
case 'array':
|
|
787
|
+
case 'set':
|
|
788
|
+
return { type: { kind: expression.type, element: this.elements(expression.elements, facts) } };
|
|
789
|
+
case 'object': {
|
|
790
|
+
const fields = new Map();
|
|
791
|
+
for (const item of expression.items) {
|
|
792
|
+
if (item.name.type === 'builtIn')
|
|
793
|
+
continue;
|
|
794
|
+
const type = this.value(item.value, facts).type;
|
|
795
|
+
if (type.kind === 'optional')
|
|
796
|
+
this.uncheckedOptional(type, item.value.position);
|
|
797
|
+
fields.set(item.name.type === 'string' ? textOf(item.name) : item.name.value, type);
|
|
798
|
+
}
|
|
799
|
+
return { type: { kind: 'anonymous', fields } };
|
|
800
|
+
}
|
|
801
|
+
case 'member':
|
|
802
|
+
return { type: this.member(expression, facts) };
|
|
803
|
+
case 'index':
|
|
804
|
+
return { type: this.index(expression, facts) };
|
|
805
|
+
case 'call':
|
|
806
|
+
return { type: this.call(expression, facts) };
|
|
807
|
+
case 'unary':
|
|
808
|
+
return { type: this.unary(expression, facts) };
|
|
809
|
+
case 'binary':
|
|
810
|
+
return { type: this.binary(expression, facts) };
|
|
811
|
+
case 'instanceOf': {
|
|
812
|
+
// Checking a result doesn't use it (docs/adr/0023-error-handling.md).
|
|
813
|
+
this.expression(expression.value, facts);
|
|
814
|
+
this.recordCheck(expression);
|
|
815
|
+
return { type: BOOLEAN };
|
|
816
|
+
}
|
|
817
|
+
case 'canPredicate':
|
|
818
|
+
if (expression.group !== undefined)
|
|
819
|
+
this.group(expression.group, facts);
|
|
820
|
+
return { type: BOOLEAN };
|
|
821
|
+
case 'rolePredicate':
|
|
822
|
+
this.group(expression.group, facts);
|
|
823
|
+
return { type: BOOLEAN };
|
|
824
|
+
case 'isPredicate':
|
|
825
|
+
return { type: BOOLEAN };
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
nameType(name, facts) {
|
|
829
|
+
if (name.type === 'identifier') {
|
|
830
|
+
const local = this.locals.get(name);
|
|
831
|
+
if (local !== undefined)
|
|
832
|
+
return facts.get(local.declaration) ?? this.bindings.get(local.declaration) ?? UNCHECKED;
|
|
833
|
+
}
|
|
834
|
+
const symbol = this.references.get(name);
|
|
835
|
+
if (symbol?.declaration?.type === 'const')
|
|
836
|
+
return this.constantType(symbol.declaration);
|
|
837
|
+
return UNCHECKED;
|
|
838
|
+
}
|
|
839
|
+
/** The types of the built-in values (docs/adr/0024-built-in-values.md, 0026, 0037). */
|
|
840
|
+
builtInType(builtIn, quiet = false) {
|
|
841
|
+
switch (builtIn.name) {
|
|
842
|
+
case 'now':
|
|
843
|
+
return DATE_TIME;
|
|
844
|
+
case 'subject':
|
|
845
|
+
return { kind: 'subject' };
|
|
846
|
+
case 'result': {
|
|
847
|
+
const action = this.triggerAction();
|
|
848
|
+
return action === undefined ? UNCHECKED : this.actionResult(action);
|
|
849
|
+
}
|
|
850
|
+
case 'error': {
|
|
851
|
+
// Outside a test case, @error has already been reported as misplaced.
|
|
852
|
+
const action = this.trigger?.source.value === 'TestCase' ? this.triggerAction() : undefined;
|
|
853
|
+
if (action === undefined)
|
|
854
|
+
return UNCHECKED;
|
|
855
|
+
const actionResult = this.actionResult(action);
|
|
856
|
+
if (actionResult.kind !== 'result') {
|
|
857
|
+
if (!quiet)
|
|
858
|
+
this.report(error('no-errors', `${action.name.value} can't give an error, so @error would never hold one.`, builtIn.position));
|
|
859
|
+
return UNCHECKED;
|
|
860
|
+
}
|
|
861
|
+
return optional(actionResult.anyError ? { kind: 'error' } : { kind: 'error', among: actionResult.errors });
|
|
862
|
+
}
|
|
863
|
+
default:
|
|
864
|
+
// `@request` and `@message` are typed by their sources, which aren't defined yet.
|
|
865
|
+
return UNCHECKED;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
triggerAction() {
|
|
869
|
+
const declaration = this.trigger === undefined ? undefined : this.references.get(this.trigger.action)?.declaration;
|
|
870
|
+
return declaration?.type === 'action' ? declaration : undefined;
|
|
871
|
+
}
|
|
872
|
+
/** A value interpolated into text must be one that converts to text (docs/adr/0011-converting-values-to-text.md). */
|
|
873
|
+
interpolated(expression, facts) {
|
|
874
|
+
const type = this.value(expression, facts).type;
|
|
875
|
+
if (type.kind === 'optional')
|
|
876
|
+
this.uncheckedOptional(type, expression.position);
|
|
877
|
+
else if (type.kind === 'void' || type.kind === 'unknown' || type.kind === 'nothing' || (type.kind === 'error' && type.symbol === undefined && type.among !== undefined)) {
|
|
878
|
+
this.report(error('not-text', type.kind === 'unknown'
|
|
879
|
+
? "An UNKNOWN can't be interpolated. Narrow it with `instanceOf` first."
|
|
880
|
+
: type.kind === 'error'
|
|
881
|
+
? 'An error can only be converted to text once it has been narrowed to a specific error, or to ERROR.'
|
|
882
|
+
: `${describe(type)} has no value, so it can't be interpolated.`, expression.position));
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
/** The element type of an array or set value, all of whose elements must have one type (docs/adr/0021-collections.md). */
|
|
886
|
+
elements(elements, facts) {
|
|
887
|
+
let element = NOTHING;
|
|
888
|
+
for (const expression of elements) {
|
|
889
|
+
const type = this.value(expression, facts).type;
|
|
890
|
+
if (type.kind === 'optional') {
|
|
891
|
+
this.uncheckedOptional(type, expression.position);
|
|
892
|
+
continue;
|
|
893
|
+
}
|
|
894
|
+
if (element.kind === 'nothing')
|
|
895
|
+
element = type;
|
|
896
|
+
else if (!this.assignable(type, element)) {
|
|
897
|
+
if (this.assignable(element, type))
|
|
898
|
+
element = type;
|
|
899
|
+
else {
|
|
900
|
+
this.report(error('type-mismatch', `A collection's elements must all have one type, but this is ${describe(type)} and an earlier one is ${describe(element)}.`, expression.position));
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return element;
|
|
905
|
+
}
|
|
906
|
+
member(expression, facts) {
|
|
907
|
+
// `Status.Open`: the enum is resolved, and the member checked, by the name resolver.
|
|
908
|
+
const enumSymbol = expression.object.type === 'identifier' || expression.object.type === 'qualifiedIdentifier' ? this.references.get(expression.object) : undefined;
|
|
909
|
+
if (enumSymbol?.typeKind === 'enum' && (expression.object.type !== 'identifier' || this.locals.get(expression.object) === undefined)) {
|
|
910
|
+
return typeOfSymbol(enumSymbol);
|
|
911
|
+
}
|
|
912
|
+
const object = this.value(expression.object, facts).type;
|
|
913
|
+
return this.memberOf(object, expression.property, expression.object.position);
|
|
914
|
+
}
|
|
915
|
+
/** The type of `object.name`. */
|
|
916
|
+
memberOf(object, property, position) {
|
|
917
|
+
const name = property.value;
|
|
918
|
+
const missing = (owner) => {
|
|
919
|
+
this.report(error('unknown-field', `${owner} has no field called ${name}.`, property.position));
|
|
920
|
+
return UNCHECKED;
|
|
921
|
+
};
|
|
922
|
+
switch (object.kind) {
|
|
923
|
+
case 'unchecked':
|
|
924
|
+
return UNCHECKED;
|
|
925
|
+
case 'model': {
|
|
926
|
+
const field = this.shared.modelFields(object.declaration).get(name);
|
|
927
|
+
return field === undefined ? missing(object.symbol.name) : field.type;
|
|
928
|
+
}
|
|
929
|
+
case 'anonymous':
|
|
930
|
+
return object.fields.get(name) ?? missing('This object');
|
|
931
|
+
case 'entity': {
|
|
932
|
+
const field = this.shared.entityFields(object.symbol, object.declaration).get(name);
|
|
933
|
+
return field ?? missing(`${object.symbol.name}, as exposed,`);
|
|
934
|
+
}
|
|
935
|
+
case 'map':
|
|
936
|
+
// A map's type doesn't say which members it has (docs/adr/0021-collections.md).
|
|
937
|
+
return optional(object.value);
|
|
938
|
+
case 'subject':
|
|
939
|
+
if (name === 'entityName')
|
|
940
|
+
return TEXT;
|
|
941
|
+
if (name === 'identityFields')
|
|
942
|
+
return { kind: 'array', element: TEXT };
|
|
943
|
+
this.report(error('unknown-field', `@subject has no field called ${name} until it is narrowed with \`@subject is <entity>\`.`, property.position));
|
|
944
|
+
return UNCHECKED;
|
|
945
|
+
case 'error':
|
|
946
|
+
return this.errorMember(object, property);
|
|
947
|
+
case 'optional':
|
|
948
|
+
this.uncheckedOptional(object, position);
|
|
949
|
+
return UNCHECKED;
|
|
950
|
+
case 'unknown':
|
|
951
|
+
this.report(error('unknown-value', "An UNKNOWN's fields can't be read. Narrow it with `instanceOf` first.", position));
|
|
952
|
+
return UNCHECKED;
|
|
953
|
+
default:
|
|
954
|
+
this.report(error('no-fields', `${describe(object)} has no fields.`, property.position));
|
|
955
|
+
return UNCHECKED;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
/** An error's only field is `data`, once it is narrowed to an error that has one (docs/adr/0023-error-handling.md). */
|
|
959
|
+
errorMember(object, property) {
|
|
960
|
+
if (property.value !== 'data') {
|
|
961
|
+
this.report(error('unknown-field', `An error's only field is data. Use \`instanceOf\` to find out which error it is.`, property.position));
|
|
962
|
+
return UNCHECKED;
|
|
963
|
+
}
|
|
964
|
+
if (object.symbol === undefined) {
|
|
965
|
+
this.report(error('unknown-field', 'Only a specific error has data. Narrow it with `instanceOf` to the error first.', property.position));
|
|
966
|
+
return UNCHECKED;
|
|
967
|
+
}
|
|
968
|
+
const data = object.declaration === undefined ? undefined : this.shared.dataModelOf(object.declaration);
|
|
969
|
+
if (data === undefined) {
|
|
970
|
+
this.report(error('unknown-field', `${object.symbol.name} has no data.`, property.position));
|
|
971
|
+
return UNCHECKED;
|
|
972
|
+
}
|
|
973
|
+
return { kind: 'model', symbol: { kind: 'type', typeKind: 'model', name: data.name.value, module: object.symbol.module }, declaration: data };
|
|
974
|
+
}
|
|
975
|
+
index(expression, facts) {
|
|
976
|
+
const object = this.value(expression.object, facts).type;
|
|
977
|
+
const index = this.value(expression.index, facts);
|
|
978
|
+
switch (object.kind) {
|
|
979
|
+
case 'unchecked':
|
|
980
|
+
return UNCHECKED;
|
|
981
|
+
case 'array':
|
|
982
|
+
if (!this.assignable(index.type, NUMBER))
|
|
983
|
+
this.mismatch(index.type, NUMBER, expression.index.position, 'An index');
|
|
984
|
+
return object.element.kind === 'nothing' ? UNCHECKED : optional(object.element);
|
|
985
|
+
case 'map':
|
|
986
|
+
this.interpolated(expression.index, facts);
|
|
987
|
+
return object.value.kind === 'nothing' ? UNCHECKED : optional(object.value);
|
|
988
|
+
case 'model':
|
|
989
|
+
case 'anonymous':
|
|
990
|
+
case 'entity': {
|
|
991
|
+
const fields = object.kind === 'model'
|
|
992
|
+
? new Map([...this.shared.modelFields(object.declaration)].map(([name, field]) => [name, field.type]))
|
|
993
|
+
: object.kind === 'entity'
|
|
994
|
+
? this.shared.entityFields(object.symbol, object.declaration)
|
|
995
|
+
: object.fields;
|
|
996
|
+
// A literal name works exactly like `.name` (docs/adr/0027-object-shapes.md).
|
|
997
|
+
if (expression.index.type === 'string' && expression.index.parts.every((part) => part.type === 'text')) {
|
|
998
|
+
const name = textOf(expression.index);
|
|
999
|
+
const field = fields.get(name);
|
|
1000
|
+
if (field === undefined) {
|
|
1001
|
+
this.report(error('unknown-field', `${describe(object)} has no field called ${name}.`, expression.index.position));
|
|
1002
|
+
return UNCHECKED;
|
|
1003
|
+
}
|
|
1004
|
+
return field;
|
|
1005
|
+
}
|
|
1006
|
+
const types = [...fields.values()].map((type) => (type.kind === 'optional' ? type.inner : type));
|
|
1007
|
+
const first = types[0];
|
|
1008
|
+
return first !== undefined && types.every((type) => this.assignable(type, first) && this.assignable(first, type))
|
|
1009
|
+
? optional(first)
|
|
1010
|
+
: optional(UNKNOWN);
|
|
1011
|
+
}
|
|
1012
|
+
case 'set':
|
|
1013
|
+
this.report(error('set-index', "A set's elements have no indexes, so they can't be reached with [ ].", expression.position));
|
|
1014
|
+
return UNCHECKED;
|
|
1015
|
+
case 'optional':
|
|
1016
|
+
this.uncheckedOptional(object, expression.object.position);
|
|
1017
|
+
return UNCHECKED;
|
|
1018
|
+
default:
|
|
1019
|
+
this.report(error('no-index', `${describe(object)} can't be indexed.`, expression.object.position));
|
|
1020
|
+
return UNCHECKED;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
call(expression, facts) {
|
|
1024
|
+
const callee = expression.callee;
|
|
1025
|
+
const symbol = callee.type === 'identifier' || callee.type === 'qualifiedIdentifier' ? this.references.get(callee) : undefined;
|
|
1026
|
+
const action = symbol?.declaration;
|
|
1027
|
+
if (action?.type !== 'action') {
|
|
1028
|
+
// A standard library action, whose signature isn't written down yet, or a call the
|
|
1029
|
+
// name resolver has already reported.
|
|
1030
|
+
for (const argument of expression.arguments)
|
|
1031
|
+
this.value(argument.value, facts);
|
|
1032
|
+
if (symbol?.module === 'cant::Collections' && (symbol.name === 'add' || symbol.name === 'remove')) {
|
|
1033
|
+
this.navigationChange(expression, facts);
|
|
1034
|
+
}
|
|
1035
|
+
return UNCHECKED;
|
|
1036
|
+
}
|
|
1037
|
+
let converted = false;
|
|
1038
|
+
for (const [argument, parameter] of this.matchArguments(action, expression.arguments)) {
|
|
1039
|
+
if (parameter === undefined)
|
|
1040
|
+
this.value(argument.value, facts);
|
|
1041
|
+
else if (this.argument(argument.value, parameter.valueType, parameter.optional, facts, parameter.name.value))
|
|
1042
|
+
converted = true;
|
|
1043
|
+
}
|
|
1044
|
+
// An argument converted to an enum when the program runs can fail, as part of the call's
|
|
1045
|
+
// result (docs/adr/0013-enums.md).
|
|
1046
|
+
return this.actionResult(action, converted ? [builtInError('InvalidEnumValue')] : []);
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* `add` or `remove` on a to-many navigation field changes a relationship, so it is treated as
|
|
1050
|
+
* a statement that can raise CardinalityViolation (docs/adr/0035-entity-relationships.md).
|
|
1051
|
+
*/
|
|
1052
|
+
navigationChange(expression, facts) {
|
|
1053
|
+
const set = expression.arguments.find((argument) => argument.name?.value === 'set') ?? expression.arguments[0];
|
|
1054
|
+
const target = set?.value;
|
|
1055
|
+
if (target?.type !== 'member')
|
|
1056
|
+
return;
|
|
1057
|
+
const object = this.peek(target.object, facts);
|
|
1058
|
+
if (object.kind === 'entity' && this.shared.navigationOf(object.symbol).has(target.property.value)) {
|
|
1059
|
+
this.requireRaised('CardinalityViolation', expression.position, this.ignoring, 'changing a relationship');
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
/** Whether the expression being checked is under `ignore`. */
|
|
1063
|
+
ignoring = false;
|
|
1064
|
+
/** Pair each argument with the parameter it is for, with the short forms of docs/adr/0020-action-parameters.md. */
|
|
1065
|
+
matchArguments(action, args) {
|
|
1066
|
+
const byName = new Map(action.parameters.map((parameter) => [parameter.name.value, parameter]));
|
|
1067
|
+
const required = action.parameters.filter((parameter) => !parameter.optional);
|
|
1068
|
+
return args.map((argument) => {
|
|
1069
|
+
if (argument.name !== undefined)
|
|
1070
|
+
return [argument, byName.get(argument.name.value)];
|
|
1071
|
+
if (argument.value.type === 'identifier' && byName.has(argument.value.value))
|
|
1072
|
+
return [argument, byName.get(argument.value.value)];
|
|
1073
|
+
if (args.length === 1 && required.length === 1)
|
|
1074
|
+
return [argument, required[0]];
|
|
1075
|
+
return [argument, undefined];
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
unary(expression, facts) {
|
|
1079
|
+
if (expression.operator === 'ignore') {
|
|
1080
|
+
// `ignore` marks a potential error as not handled on purpose, but doesn't use the value.
|
|
1081
|
+
const ignoring = this.ignoring;
|
|
1082
|
+
this.ignoring = true;
|
|
1083
|
+
const operand = this.expression(expression.argument, facts).type;
|
|
1084
|
+
this.ignoring = ignoring;
|
|
1085
|
+
this.markHandled(expression.argument);
|
|
1086
|
+
if (operand.kind !== 'result' && operand.kind !== 'unchecked') {
|
|
1087
|
+
this.report(error('nothing-to-ignore', `\`ignore\` is for a value that may hold an error, but this is ${describe(operand)}.`, expression.argument.position));
|
|
1088
|
+
}
|
|
1089
|
+
return operand;
|
|
1090
|
+
}
|
|
1091
|
+
const operand = this.value(expression.argument, facts).type;
|
|
1092
|
+
switch (expression.operator) {
|
|
1093
|
+
case '!':
|
|
1094
|
+
if (!this.assignable(operand, BOOLEAN))
|
|
1095
|
+
this.mismatch(operand, BOOLEAN, expression.argument.position, "`!`'s operand");
|
|
1096
|
+
return BOOLEAN;
|
|
1097
|
+
case '-':
|
|
1098
|
+
if (isBase(operand, 'DURATION'))
|
|
1099
|
+
return DURATION;
|
|
1100
|
+
if (!this.assignable(operand, NUMBER))
|
|
1101
|
+
this.mismatch(operand, NUMBER, expression.argument.position, "`-`'s operand");
|
|
1102
|
+
return NUMBER;
|
|
1103
|
+
case 'isEmpty':
|
|
1104
|
+
if (operand.kind !== 'optional' && operand.kind !== 'unchecked') {
|
|
1105
|
+
this.report(error('not-optional', `\`isEmpty\` only applies to an optional, but this is ${describe(operand)}, which always has a value.`, expression.argument.position));
|
|
1106
|
+
}
|
|
1107
|
+
return BOOLEAN;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
binary(expression, facts) {
|
|
1111
|
+
const { operator } = expression;
|
|
1112
|
+
if (operator === 'and' || operator === 'or') {
|
|
1113
|
+
this.condition(expression.left, facts, `\`${operator}\`'s left operand`);
|
|
1114
|
+
// The right operand only runs when the left one didn't decide, so it gets that narrowing.
|
|
1115
|
+
const rightFacts = extend(facts, this.narrow(expression.left, operator === 'and', facts));
|
|
1116
|
+
this.condition(expression.right, rightFacts, `\`${operator}\`'s right operand`);
|
|
1117
|
+
return BOOLEAN;
|
|
1118
|
+
}
|
|
1119
|
+
// `orElse` doesn't use an optional's value unless it has one, but it does use a result.
|
|
1120
|
+
const left = this.value(expression.left, facts).type;
|
|
1121
|
+
const right = this.value(expression.right, facts).type;
|
|
1122
|
+
if (left.kind === 'unchecked' || right.kind === 'unchecked') {
|
|
1123
|
+
return operator === 'orElse' ? (right.kind === 'optional' ? right : UNCHECKED) : isComparison(operator) ? BOOLEAN : UNCHECKED;
|
|
1124
|
+
}
|
|
1125
|
+
if (operator === 'orElse')
|
|
1126
|
+
return this.orElse(expression, left, right);
|
|
1127
|
+
// Operators other than `orElse` use an optional's value, so it must be checked first.
|
|
1128
|
+
for (const [type, side] of [[left, expression.left], [right, expression.right]]) {
|
|
1129
|
+
if (type.kind === 'optional') {
|
|
1130
|
+
this.uncheckedOptional(type, side.position);
|
|
1131
|
+
return isComparison(operator) ? BOOLEAN : UNCHECKED;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
if (operator === '==' || operator === '!=') {
|
|
1135
|
+
if (!this.comparable(left, right)) {
|
|
1136
|
+
this.report(error('type-mismatch', `${describe(left)} and ${describe(right)} can't be compared: they have different types.`, expression.position));
|
|
1137
|
+
}
|
|
1138
|
+
return BOOLEAN;
|
|
1139
|
+
}
|
|
1140
|
+
if (operator === '<' || operator === '<=' || operator === '>' || operator === '>=') {
|
|
1141
|
+
const ordered = orderedType(left);
|
|
1142
|
+
if (ordered === undefined || orderedType(right) !== ordered) {
|
|
1143
|
+
this.report(error('type-mismatch', ordered === undefined
|
|
1144
|
+
? `${describe(left)} values can't be ordered. NUMBER, DATE_TIME, DURATION and TEXT can, and enums of them.`
|
|
1145
|
+
: `${describe(left)} and ${describe(right)} can't be ordered against each other.`, expression.position));
|
|
1146
|
+
}
|
|
1147
|
+
return BOOLEAN;
|
|
1148
|
+
}
|
|
1149
|
+
if (operator === 'in')
|
|
1150
|
+
return this.inOperator(expression, left, right);
|
|
1151
|
+
return this.arithmetic(expression, left, right);
|
|
1152
|
+
}
|
|
1153
|
+
orElse(expression, left, right) {
|
|
1154
|
+
if (left.kind !== 'optional') {
|
|
1155
|
+
this.report(error('not-optional', `\`orElse\` needs an optional on its left, but this is ${describe(left)}, which always has a value.`, expression.left.position));
|
|
1156
|
+
return right;
|
|
1157
|
+
}
|
|
1158
|
+
const fallback = right.kind === 'optional' ? right.inner : right;
|
|
1159
|
+
if (!this.assignable(fallback, left.inner) && !this.assignable(left.inner, fallback)) {
|
|
1160
|
+
this.report(error('type-mismatch', `The fallback must be ${describe(left.inner)} or ${describe(left)}, but this is ${describe(right)}.`, expression.right.position));
|
|
1161
|
+
}
|
|
1162
|
+
// The result has the fallback's type (docs/adr/0016-handling-optional-values.md).
|
|
1163
|
+
return right.kind === 'optional' ? optional(left.inner) : this.assignable(left.inner, fallback) ? fallback : left.inner;
|
|
1164
|
+
}
|
|
1165
|
+
/** `==` and `!=` need operands of one type, except enums, compared by value, and UNKNOWN (docs/adr/0019-operators.md). */
|
|
1166
|
+
comparable(left, right) {
|
|
1167
|
+
if (left.kind === 'unknown' || right.kind === 'unknown')
|
|
1168
|
+
return true;
|
|
1169
|
+
if (left.kind === 'enum' && right.kind === 'enum')
|
|
1170
|
+
return left.valueType === right.valueType;
|
|
1171
|
+
if (left.kind === 'enum')
|
|
1172
|
+
return right.kind === 'base' && right.name === left.valueType;
|
|
1173
|
+
if (right.kind === 'enum')
|
|
1174
|
+
return left.kind === 'base' && left.name === right.valueType;
|
|
1175
|
+
if (left.kind === 'error' || right.kind === 'error')
|
|
1176
|
+
return false;
|
|
1177
|
+
return this.assignable(left, right) || this.assignable(right, left);
|
|
1178
|
+
}
|
|
1179
|
+
inOperator(expression, value, collection) {
|
|
1180
|
+
switch (collection.kind) {
|
|
1181
|
+
case 'array':
|
|
1182
|
+
case 'set':
|
|
1183
|
+
if (collection.element.kind !== 'nothing' && !this.comparable(value, collection.element)) {
|
|
1184
|
+
this.report(error('type-mismatch', `${describe(value)} can't be in ${describe(collection)}: its elements are ${describe(collection.element)}.`, expression.left.position));
|
|
1185
|
+
}
|
|
1186
|
+
return BOOLEAN;
|
|
1187
|
+
case 'map':
|
|
1188
|
+
// `in` checks a map's member names, which can be any value that converts to text.
|
|
1189
|
+
this.interpolated(expression.left, NO_FACTS);
|
|
1190
|
+
return BOOLEAN;
|
|
1191
|
+
default:
|
|
1192
|
+
this.report(error('type-mismatch', `\`in\` needs an array, a map or a set on its right, but this is ${describe(collection)}.`, expression.right.position));
|
|
1193
|
+
return BOOLEAN;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
/** The arithmetic operators, on numbers, and on dates and durations (docs/adr/0019-operators.md, 0026). */
|
|
1197
|
+
arithmetic(expression, left, right) {
|
|
1198
|
+
const { operator } = expression;
|
|
1199
|
+
const is = (type, name) => isBase(type, name) || (type.kind === 'enum' && type.valueType === name);
|
|
1200
|
+
let type;
|
|
1201
|
+
if (is(left, 'NUMBER') && is(right, 'NUMBER'))
|
|
1202
|
+
type = NUMBER;
|
|
1203
|
+
else if (operator === '+' && ((is(left, 'DATE_TIME') && is(right, 'DURATION')) || (is(left, 'DURATION') && is(right, 'DATE_TIME'))))
|
|
1204
|
+
type = DATE_TIME;
|
|
1205
|
+
else if (operator === '-' && is(left, 'DATE_TIME') && is(right, 'DURATION'))
|
|
1206
|
+
type = DATE_TIME;
|
|
1207
|
+
else if (operator === '-' && is(left, 'DATE_TIME') && is(right, 'DATE_TIME'))
|
|
1208
|
+
type = DURATION;
|
|
1209
|
+
else if ((operator === '+' || operator === '-') && is(left, 'DURATION') && is(right, 'DURATION'))
|
|
1210
|
+
type = DURATION;
|
|
1211
|
+
else if (operator === '*' && ((is(left, 'DURATION') && is(right, 'NUMBER')) || (is(left, 'NUMBER') && is(right, 'DURATION'))))
|
|
1212
|
+
type = DURATION;
|
|
1213
|
+
else if (operator === '/' && is(left, 'DURATION') && is(right, 'NUMBER'))
|
|
1214
|
+
type = DURATION;
|
|
1215
|
+
else if (operator === '/' && is(left, 'DURATION') && is(right, 'DURATION'))
|
|
1216
|
+
type = NUMBER;
|
|
1217
|
+
if (type === undefined) {
|
|
1218
|
+
const hint = operator === '+' && (isBase(left, 'TEXT') || isBase(right, 'TEXT')) ? ' `+` doesn\'t join text: interpolate instead, as in "${a}${b}".' : '';
|
|
1219
|
+
this.report(error('type-mismatch', `\`${operator}\` can't be used on ${describe(left)} and ${describe(right)}.${hint}`, expression.position));
|
|
1220
|
+
return UNCHECKED;
|
|
1221
|
+
}
|
|
1222
|
+
return operator === '/' || operator === '%' ? this.division(expression, type) : type;
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* `/` and `%` raise DivisionByZero, and `%` FractionalModulo, unless the compiler can tell they
|
|
1226
|
+
* can't, from constant operands (docs/adr/0019-operators.md, 0023).
|
|
1227
|
+
*/
|
|
1228
|
+
division(expression, type) {
|
|
1229
|
+
const divisor = this.constant(expression.right);
|
|
1230
|
+
if ((divisor?.kind === 'number' || divisor?.kind === 'duration') && divisor.value === 0) {
|
|
1231
|
+
this.report(error('division-by-zero', 'This divides by zero.', expression.right.position));
|
|
1232
|
+
return type;
|
|
1233
|
+
}
|
|
1234
|
+
const errors = divisor === undefined ? [builtInError('DivisionByZero')] : [];
|
|
1235
|
+
if (expression.operator === '%') {
|
|
1236
|
+
const dividend = this.constant(expression.left);
|
|
1237
|
+
const fractional = [dividend, divisor].some((value) => value?.kind === 'number' && !Number.isInteger(value.value));
|
|
1238
|
+
if (fractional) {
|
|
1239
|
+
this.report(error('fractional-modulo', '`%` only works on whole numbers.', expression.position));
|
|
1240
|
+
}
|
|
1241
|
+
else if (dividend === undefined || divisor === undefined) {
|
|
1242
|
+
errors.push(builtInError('FractionalModulo'));
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
return result(type, errors);
|
|
1246
|
+
}
|
|
1247
|
+
/** The value of a constant expression, if it has one (docs/adr/0023-error-handling.md). */
|
|
1248
|
+
constant(expression) {
|
|
1249
|
+
return this.shared.constantValues.value(expression, this.locals, this.localValues);
|
|
1250
|
+
}
|
|
1251
|
+
/** The value of a `NUMBER` constant expression, if it has one. */
|
|
1252
|
+
constantNumber(expression) {
|
|
1253
|
+
const value = this.constant(expression);
|
|
1254
|
+
return value?.kind === 'number' ? value.value : undefined;
|
|
1255
|
+
}
|
|
1256
|
+
/** The group in `within` must be a group entity (docs/adr/0010-security.md). */
|
|
1257
|
+
group(expression, facts) {
|
|
1258
|
+
const type = this.value(expression, facts).type;
|
|
1259
|
+
if (type.kind === 'unchecked')
|
|
1260
|
+
return;
|
|
1261
|
+
if (type.kind === 'optional') {
|
|
1262
|
+
this.uncheckedOptional(type, expression.position);
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
if (type.kind !== 'entity' || !type.declaration.lines.some((line) => line.type === 'groupLine')) {
|
|
1266
|
+
this.report(error('not-a-group', `\`within\` needs a group entity, but this is ${describe(type)}.`, expression.position));
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
// ----- Narrowing: docs/adr/0016-handling-optional-values.md, 0023, 0033, 0010 -----
|
|
1270
|
+
/** The type a name has now, without checking the expression again. */
|
|
1271
|
+
peek(expression, facts) {
|
|
1272
|
+
const key = this.key(expression);
|
|
1273
|
+
if (key === undefined)
|
|
1274
|
+
return UNCHECKED;
|
|
1275
|
+
if (typeof key === 'string')
|
|
1276
|
+
return facts.get(key) ?? UNCHECKED;
|
|
1277
|
+
const type = facts.get(key) ?? this.bindings.get(key) ?? UNCHECKED;
|
|
1278
|
+
return type.kind === 'result' ? type.value : type;
|
|
1279
|
+
}
|
|
1280
|
+
/** The type a key has now: a local's binding, or a built-in's. */
|
|
1281
|
+
typeOfKey(key, facts) {
|
|
1282
|
+
const known = facts.get(key);
|
|
1283
|
+
if (known !== undefined)
|
|
1284
|
+
return known;
|
|
1285
|
+
if (typeof key !== 'string')
|
|
1286
|
+
return this.bindings.get(key) ?? UNCHECKED;
|
|
1287
|
+
return UNCHECKED;
|
|
1288
|
+
}
|
|
1289
|
+
/** Record which errors an `instanceOf` checks a held result for. */
|
|
1290
|
+
recordCheck(expression) {
|
|
1291
|
+
if (expression.value.type !== 'identifier')
|
|
1292
|
+
return;
|
|
1293
|
+
const declaration = this.locals.get(expression.value)?.declaration;
|
|
1294
|
+
const held = declaration === undefined ? undefined : this.held.get(declaration);
|
|
1295
|
+
if (held === undefined)
|
|
1296
|
+
return;
|
|
1297
|
+
if (expression.target.type === 'errorType') {
|
|
1298
|
+
held.handled = true;
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
const symbol = expression.target.type === 'identifier' || expression.target.type === 'qualifiedIdentifier' ? this.references.get(expression.target) : undefined;
|
|
1302
|
+
if (symbol?.typeKind === 'error')
|
|
1303
|
+
held.checked.add(symbolKey(symbol));
|
|
1304
|
+
}
|
|
1305
|
+
/** What a condition being `truth` says about the names in it. */
|
|
1306
|
+
narrow(condition, truth, facts) {
|
|
1307
|
+
switch (condition.type) {
|
|
1308
|
+
case 'unary': {
|
|
1309
|
+
if (condition.operator === '!')
|
|
1310
|
+
return this.narrow(condition.argument, !truth, facts);
|
|
1311
|
+
if (condition.operator !== 'isEmpty')
|
|
1312
|
+
return NO_FACTS;
|
|
1313
|
+
const key = this.key(condition.argument);
|
|
1314
|
+
if (key === undefined || truth)
|
|
1315
|
+
return NO_FACTS;
|
|
1316
|
+
const type = key === '@error' ? this.typeOfKeyOrBuiltIn(key, condition.argument, facts) : this.typeOfKey(key, facts);
|
|
1317
|
+
return type.kind === 'optional' ? new Map([[key, type.inner]]) : NO_FACTS;
|
|
1318
|
+
}
|
|
1319
|
+
case 'binary': {
|
|
1320
|
+
if (condition.operator === 'and' && truth) {
|
|
1321
|
+
const left = this.narrow(condition.left, true, facts);
|
|
1322
|
+
return extend(left, this.narrow(condition.right, true, extend(facts, left)));
|
|
1323
|
+
}
|
|
1324
|
+
if (condition.operator === 'or' && !truth) {
|
|
1325
|
+
const left = this.narrow(condition.left, false, facts);
|
|
1326
|
+
return extend(left, this.narrow(condition.right, false, extend(facts, left)));
|
|
1327
|
+
}
|
|
1328
|
+
return NO_FACTS;
|
|
1329
|
+
}
|
|
1330
|
+
case 'instanceOf':
|
|
1331
|
+
return this.narrowInstanceOf(condition, truth, facts);
|
|
1332
|
+
case 'isPredicate': {
|
|
1333
|
+
if (!truth || condition.subjectType.type !== 'identifier')
|
|
1334
|
+
return NO_FACTS;
|
|
1335
|
+
const symbol = this.references.get(condition.subjectType);
|
|
1336
|
+
return symbol === undefined ? NO_FACTS : new Map([['@subject', typeOfSymbol(symbol)]]);
|
|
1337
|
+
}
|
|
1338
|
+
default:
|
|
1339
|
+
return NO_FACTS;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
typeOfKeyOrBuiltIn(key, expression, facts) {
|
|
1343
|
+
const known = facts.get(key);
|
|
1344
|
+
if (known !== undefined)
|
|
1345
|
+
return known;
|
|
1346
|
+
return expression.type === 'builtIn' ? this.builtInType(expression, true) : UNCHECKED;
|
|
1347
|
+
}
|
|
1348
|
+
/**
|
|
1349
|
+
* `instanceOf` narrows a value to the type it tests when it is `true`. On a result, or an
|
|
1350
|
+
* error, it also narrows when it is `false`: the error it tests is ruled out, and once every
|
|
1351
|
+
* error is, a result is narrowed to its value (docs/adr/0023-error-handling.md, 0033).
|
|
1352
|
+
*/
|
|
1353
|
+
narrowInstanceOf(condition, truth, facts) {
|
|
1354
|
+
const key = this.key(condition.value);
|
|
1355
|
+
if (key === undefined)
|
|
1356
|
+
return NO_FACTS;
|
|
1357
|
+
const current = typeof key === 'string' && condition.value.type === 'builtIn' ? this.typeOfKeyOrBuiltIn(key, condition.value, facts) : this.typeOfKey(key, facts);
|
|
1358
|
+
const target = condition.target.type === 'errorType' ? { kind: 'error' } : typeOf(condition.target, this.references);
|
|
1359
|
+
if (target.kind === 'error') {
|
|
1360
|
+
const errors = current.kind === 'result' ? current.errors : current.kind === 'error' && current.symbol === undefined ? (current.among ?? []) : current.kind === 'error' ? [current.symbol].filter((symbol) => symbol !== undefined) : [];
|
|
1361
|
+
const anyError = current.kind === 'result' ? current.anyError : current.kind === 'error' && current.symbol === undefined && current.among === undefined;
|
|
1362
|
+
if (current.kind !== 'result' && current.kind !== 'error')
|
|
1363
|
+
return NO_FACTS;
|
|
1364
|
+
if (truth) {
|
|
1365
|
+
if (target.symbol === undefined)
|
|
1366
|
+
return new Map([[key, anyError ? { kind: 'error' } : { kind: 'error', among: errors }]]);
|
|
1367
|
+
return new Map([[key, target]]);
|
|
1368
|
+
}
|
|
1369
|
+
// `false`: the tested error is ruled out, or every error, for `ERROR`.
|
|
1370
|
+
if (current.kind !== 'result')
|
|
1371
|
+
return NO_FACTS;
|
|
1372
|
+
if (target.symbol === undefined)
|
|
1373
|
+
return new Map([[key, current.value]]);
|
|
1374
|
+
const symbol = target.symbol;
|
|
1375
|
+
const left = errors.filter((other) => !sameSymbol(other, symbol));
|
|
1376
|
+
return new Map([[key, result(current.value, left, anyError)]]);
|
|
1377
|
+
}
|
|
1378
|
+
if (!truth)
|
|
1379
|
+
return NO_FACTS;
|
|
1380
|
+
return new Map([[key, condition.optional ? optional(target) : target]]);
|
|
1381
|
+
}
|
|
1382
|
+
/** The key narrowing records a name under: its declaration, for a local, or `@name`. */
|
|
1383
|
+
key(expression) {
|
|
1384
|
+
if (expression.type === 'builtIn')
|
|
1385
|
+
return `@${expression.name}`;
|
|
1386
|
+
if (expression.type === 'identifier')
|
|
1387
|
+
return this.locals.get(expression)?.declaration;
|
|
1388
|
+
return undefined;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
const isComparison = (operator) => ['==', '!=', '<', '<=', '>', '>=', 'in'].includes(operator);
|
|
1392
|
+
/** The type a value is ordered as, if it can be ordered (docs/adr/0019-operators.md). */
|
|
1393
|
+
function orderedType(type) {
|
|
1394
|
+
const name = type.kind === 'base' ? type.name : type.kind === 'enum' ? type.valueType : undefined;
|
|
1395
|
+
return name === 'NUMBER' || name === 'DATE_TIME' || name === 'DURATION' || name === 'TEXT' ? name : undefined;
|
|
1396
|
+
}
|
|
1397
|
+
const textOf = (literal) => literal.parts.map((part) => (part.type === 'text' && typeof part.value === 'string' ? part.value : '')).join('');
|
|
1398
|
+
/** Whether a loop body can `break` out of its own loop. */
|
|
1399
|
+
function breaks(statements) {
|
|
1400
|
+
return statements.some((statement) => {
|
|
1401
|
+
if (statement.type === 'break')
|
|
1402
|
+
return true;
|
|
1403
|
+
if (statement.type === 'if')
|
|
1404
|
+
return statement.children.some((branch) => (branch.type === 'if' ? breaks([branch]) : breaks(branch.children)));
|
|
1405
|
+
return false;
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
//# sourceMappingURL=typecheck.js.map
|