tla-ts 0.0.3-dev.bffbba3 → 0.0.3-dev.c8c988d

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/src/liveness.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isLocallyResolved, type Definition, type Expression } from "./ast";
2
- import { TlaConfigError, TlaEvaluationError } from "./errors";
3
- import { EXIT_STATUS, setExitCode } from "./exit-codes";
2
+ import { STACK_OVERFLOW, TlaEvaluationError, TlaModuleError, UNEXPECTED_EXCEPTION } from "./errors";
3
+ import { EXIT_STATUS, getOwnExitCode, setExitCode } from "./exit-codes";
4
4
  import {
5
5
  describeLocation,
6
6
  describeRange,
@@ -61,6 +61,23 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
61
61
  next: undefined,
62
62
  });
63
63
 
64
+ // TLC evaluates the constant parts of a property while it processes the spec. Its top-level handler
65
+ // prints a failure without a TLC error code of its own as an unexpected exception.
66
+ const evaluateConstant = (expression: Expression, context: Context) => {
67
+ try {
68
+ return evaluate(expression, context);
69
+ } catch (error) {
70
+ if (!(error instanceof TlaEvaluationError) || error instanceof TlaModuleError) throw error;
71
+ const code = getOwnExitCode(error);
72
+ throw code === undefined
73
+ ? setExitCode(
74
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
75
+ EXIT_STATUS.error,
76
+ )
77
+ : setExitCode(error, code);
78
+ }
79
+ };
80
+
64
81
  const createCannotHandleError = (expression: Expression) =>
65
82
  new TlaEvaluationError(
66
83
  `TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
@@ -191,14 +208,18 @@ export const expandTemporal = (
191
208
  getDefinitionLevel(definition, environment) > 0
192
209
  ) {
193
210
  if (expanding.has(definition)) {
194
- throw new TlaConfigError(`The definition of ${expression.name} refers to itself`);
211
+ throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
195
212
  }
196
- return expand(
213
+ const expanded = expand(
197
214
  definition.body,
198
215
  undefined,
199
216
  expression.name,
200
217
  new Set([...expanding, definition]),
201
218
  );
219
+ // TLC takes the body of a state-level definition in a specification as one initial predicate.
220
+ return !isProperty && getDefinitionLevel(definition, environment) < ACTION_LEVEL
221
+ ? [{ expression: definition.body, scope: undefined, name: expression.name }]
222
+ : expanded;
202
223
  }
203
224
  }
204
225
  const context = getConstantContext(environment, scope);
@@ -278,7 +299,7 @@ export const toLiveExpression = (
278
299
  ): LiveExpression => {
279
300
  const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
280
301
  if (atomLevel === 0) {
281
- const value = evaluate(expression, getConstantContext(environment, scope));
302
+ const value = evaluateConstant(expression, getConstantContext(environment, scope));
282
303
  if (typeof value !== "boolean") {
283
304
  throw new TlaEvaluationError(
284
305
  `TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
@@ -299,9 +320,7 @@ export const toLiveExpression = (
299
320
  scope: Scope | undefined,
300
321
  ) => {
301
322
  if (expansion.depth >= MAXIMUM_EXPANSION_DEPTH) {
302
- throw new TlaEvaluationError(
303
- `TLC overflowed the stack while expanding the temporal formula at ${describeLocation(expression)}`,
304
- );
323
+ throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
305
324
  }
306
325
  expansion.depth++;
307
326
  const result = convert(body, bodyScope);
@@ -356,9 +375,45 @@ export const toLiveExpression = (
356
375
  return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
357
376
  };
358
377
 
378
+ // TLC translates the operands of a connective even when the whole formula is a state or action
379
+ // predicate, so it fails on a constant operand before it checks any state.
380
+ const convertOperands = (expression: Expression, scope: Scope | undefined) => {
381
+ switch (expression.kind) {
382
+ case "Label":
383
+ convert(expression.body, scope);
384
+ return;
385
+ case "Let":
386
+ convert(expression.body, bindLetDefinitions(expression, scope, environment));
387
+ return;
388
+ case "And":
389
+ case "Or":
390
+ for (const operand of expression.operands) convert(operand, scope);
391
+ return;
392
+ case "Apply":
393
+ if (
394
+ expression.operator === "=>" ||
395
+ (expression.operator === "~" && !isLocallyResolved(expression))
396
+ ) {
397
+ for (const operand of expression.args) convert(operand, scope);
398
+ }
399
+ return;
400
+ case "If": {
401
+ const condition = convert(expression.condition, scope);
402
+ const branches =
403
+ condition.kind === "Boolean"
404
+ ? [condition.value ? expression.consequent : expression.alternative]
405
+ : [expression.consequent, expression.alternative];
406
+ for (const branch of branches) convert(branch, scope);
407
+ }
408
+ }
409
+ };
410
+
359
411
  const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
360
412
  const level = getLevel(expression, scope, environment);
361
- if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
413
+ if (level <= ACTION_LEVEL) {
414
+ convertOperands(expression, scope);
415
+ return toAtom(expression, scope, level);
416
+ }
362
417
  switch (expression.kind) {
363
418
  case "Label":
364
419
  return convert(expression.body, scope);
@@ -401,7 +456,10 @@ export const toLiveExpression = (
401
456
  return convertQuantifier(expression, scope);
402
457
  case "If": {
403
458
  if (getLevel(expression.condition, scope, environment) === 0) {
404
- const condition = evaluate(expression.condition, getConstantContext(environment, scope));
459
+ const condition = evaluateConstant(
460
+ expression.condition,
461
+ getConstantContext(environment, scope),
462
+ );
405
463
  if (typeof condition === "boolean") {
406
464
  return convert(condition ? expression.consequent : expression.alternative, scope);
407
465
  }
@@ -15,7 +15,7 @@ import type {
15
15
  WorkerMessage,
16
16
  } from "./parallel";
17
17
  import { getSuccessors, type Successor } from "./successors";
18
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
18
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
19
19
  import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
20
20
  import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
21
21
  import { getRankedStringCount, setStringOrder, type Value } from "./values";
@@ -61,7 +61,7 @@ const run = (start: StartMessage, parent: Port) => {
61
61
  });
62
62
  const environment = specification.environment;
63
63
  const search = createSearch(start.config, environment, () =>
64
- getTlcVariableOrder(start.source, loadModule),
64
+ getTlcDeclarationOrder(start.source, loadModule, "variable"),
65
65
  );
66
66
  const nextAction = search?.spec.next;
67
67
  if (!search || nextAction === undefined)
package/src/parallel.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  type SharedFingerprints,
21
21
  } from "./fingerprints";
22
22
  import { getInitialStates } from "./successors";
23
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
23
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
24
24
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
25
25
  import { createStateEncoder, type EncodedStates } from "./transfer";
26
26
  import { getRankedStringCount, setStringOrder, type Value } from "./values";
@@ -150,7 +150,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
150
150
  });
151
151
  const environment = specification.environment;
152
152
  const search = createSearch(config, environment, () =>
153
- getTlcVariableOrder(options.source, loadModule),
153
+ getTlcDeclarationOrder(options.source, loadModule, "variable"),
154
154
  );
155
155
  if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
156
156
  if (
package/src/parser.ts CHANGED
@@ -22,7 +22,7 @@ import type {
22
22
  StepBody,
23
23
  UseOrHide,
24
24
  } from "./ast";
25
- import { TlaSyntaxError } from "./errors";
25
+ import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
26
26
  import { getNumeralValue, tokenize, type Token } from "./lexer";
27
27
 
28
28
  interface InfixOperator {
@@ -628,14 +628,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
628
628
  const body = parseExpression();
629
629
  return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
630
630
  }
631
- return parseExpression();
631
+ const argument = parseExpression();
632
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
633
+ // I!Op, as that operator, which takes the arguments of the instance too.
634
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
635
+ return argument;
632
636
  };
633
637
 
638
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
639
+
640
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
641
+
634
642
  const parseArguments = () =>
635
643
  withoutOffside(() => {
644
+ const start = cursor.index;
636
645
  expect("(");
637
646
  const args = parseSeparated(parseArgument);
638
647
  expect(")");
648
+ lastArguments.range = getRange(start);
639
649
  return args;
640
650
  });
641
651
 
@@ -662,9 +672,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
662
672
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
663
673
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
664
674
  const fields = parseSeparated(() => {
675
+ const fieldStart = cursor.index;
676
+ const fieldLocation = getLocation(peek());
665
677
  const name = expectIdentifier();
678
+ fieldLocation.range = getRange(fieldStart);
666
679
  expect(kind === "Record" ? "|->" : ":");
667
- return { name, value: parseExpression() };
680
+ return { name, value: parseExpression(), location: fieldLocation };
668
681
  });
669
682
  expect("]");
670
683
  return { kind, fields, location };
@@ -823,7 +836,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
823
836
  };
824
837
 
825
838
  const parsePrimary = (): Expression => {
826
- const isFactStart = cursor.index === factStart.index;
839
+ const start = cursor.index;
840
+ const isFactStart = start === factStart.index;
827
841
  const token = advance();
828
842
  const location = getLocation(token);
829
843
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -856,13 +870,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
856
870
  const args = hasArguments ? parseArguments() : [];
857
871
  if (!isSelectorAhead()) {
858
872
  return hasArguments
859
- ? { kind: "Apply", operator: name, args, location }
873
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
860
874
  : { kind: "Identifier", name, location };
861
875
  }
862
876
  const instance = openModules
863
877
  .flatMap((module) => module.instances)
864
878
  .find((candidate) => candidate.name === name);
865
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
879
+ const isMissingArguments =
880
+ instance !== undefined &&
881
+ !isFactStart &&
882
+ !isTlapm &&
883
+ instance.params.length !== args.length;
884
+ if (isMissingArguments && args.length > 0) {
866
885
  throw createError(
867
886
  `The operator ${name} requires ${instance.params.length} arguments`,
868
887
  token,
@@ -876,18 +895,36 @@ const parse = (source: string, isTlapm: boolean): Module => {
876
895
  const names = selectors.flatMap((selector) =>
877
896
  selector.kind === "Name" ? [selector.name] : [],
878
897
  );
879
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
880
898
  const operator = names.join("!");
881
899
  const qualifiedArgs = selectors.flatMap((selector) =>
882
900
  "args" in selector ? selector.args : [],
883
901
  );
884
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
885
902
  const argumentCounts = selectors.map((selector) =>
886
903
  "args" in selector ? selector.args.length : 0,
887
904
  );
888
- return argumentCounts.slice(0, -1).some((count) => count > 0)
889
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
890
- : { kind: "Apply", operator, args: qualifiedArgs, location };
905
+ const argumentsRange = lastArguments.range;
906
+ const reference: Expression =
907
+ names.length < selectors.length
908
+ ? { kind: "Subexpression", selectors, location }
909
+ : qualifiedArgs.length === 0
910
+ ? { kind: "Identifier", name: operator, location }
911
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
912
+ ? {
913
+ kind: "Apply",
914
+ operator,
915
+ args: qualifiedArgs,
916
+ argumentCounts,
917
+ argumentsRange,
918
+ location,
919
+ }
920
+ : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
921
+ if (isMissingArguments) {
922
+ missingArguments.set(reference, {
923
+ location: formatRange(getRange(start)),
924
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
925
+ });
926
+ }
927
+ return reference;
891
928
  }
892
929
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
893
930
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -1423,11 +1460,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1460
  location: getLocation(keyword),
1424
1461
  };
1425
1462
  const invalid =
1426
- use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1427
- if (invalid) {
1428
- throw createError(
1429
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1430
- invalid.location,
1463
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1464
+ if (invalid.length > 0) {
1465
+ throw createSanyError(
1466
+ invalid.map((fact) => ({
1467
+ location: formatRange(fact.location.range ?? getRange(start)),
1468
+ message:
1469
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1470
+ })),
1431
1471
  );
1432
1472
  }
1433
1473
  return withRange(use, start);
@@ -1563,10 +1603,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1603
  const parseStep = (): ProofStep => {
1564
1604
  const start = cursor.index;
1565
1605
  const step = parseStepStart(isAt("QED", 1));
1606
+ const statementStart = cursor.index;
1566
1607
  const scope = new Set<string>();
1567
1608
  proofScopes.push(scope);
1568
1609
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
1610
  const head = getRange(start);
1611
+ const statement = getRange(statementStart);
1570
1612
  const hasProof = isProofStart(peek());
1571
1613
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
1614
  throw createError("Proof of a step that does not take a proof");
@@ -1574,7 +1616,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1616
  const proof = hasProof ? parseProof() : undefined;
1575
1617
  proofScopes.pop();
1576
1618
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
- return withRange({ ...step, body, head, proof }, start);
1619
+ return withRange({ ...step, body, head, statement, proof }, start);
1578
1620
  };
1579
1621
 
1580
1622
  const parseProofBody = (): Proof => {
@@ -1705,6 +1747,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1705
1747
  end: { line: token.line, column: token.endColumn },
1706
1748
  };
1707
1749
  }
1750
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1708
1751
  openModules.pop();
1709
1752
  recursiveScopes.pop();
1710
1753
  return module;
package/src/resolve.ts CHANGED
@@ -13,9 +13,16 @@ import {
13
13
  type Proof,
14
14
  type ProofStep,
15
15
  type SourceLocation,
16
+ type SourceRange,
16
17
  type Theorem,
17
18
  } from "./ast";
18
- import { TlaSyntaxError } from "./errors";
19
+ import {
20
+ createSanyError,
21
+ createSemanticError,
22
+ formatRange,
23
+ TlaSyntaxError,
24
+ type SanyError,
25
+ } from "./errors";
19
26
 
20
27
  export interface ModuleSymbol {
21
28
  key: string;
@@ -103,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
103
110
  : unit.imported.get(name);
104
111
  };
105
112
 
113
+ // SANY reports a name at the range of its text.
114
+ const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
115
+ module: location.range?.module ?? "",
116
+ start: location,
117
+ end: { line: location.line, column: location.column + name.length - 1 },
118
+ });
119
+
120
+ // SANY reports an operator symbol applied to operands as one it cannot resolve.
121
+ const getFixity = (expression: Expression) => {
122
+ if (expression.kind !== "Apply") return undefined;
123
+ if (expression.args.length === 2) return "infix";
124
+ if (expression.args.length !== 1) return undefined;
125
+ return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
126
+ };
127
+
106
128
  // SANY names an undefined operator symbol such as + differently from an undefined identifier.
107
129
  const isOperatorSymbol = (name: string) =>
108
130
  name[0].toLowerCase() === name[0].toUpperCase() &&
@@ -135,7 +157,7 @@ const getSymbolArity = (
135
157
  }
136
158
  // A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
137
159
  // arguments.
138
- const prefix = name.slice(0, name.indexOf("!"));
160
+ const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
139
161
  const isSelected =
140
162
  prefix !== "" &&
141
163
  (lookupLocal(scope, prefix) !== undefined ||
@@ -144,11 +166,23 @@ const getSymbolArity = (
144
166
  if (isSelected) {
145
167
  return expression.kind === "Apply" ? expression.args.length : 0;
146
168
  }
147
- const position = `at line ${location.line}, column ${location.column}`;
148
- throw new TlaSyntaxError(
149
- isOperatorSymbol(name)
150
- ? `Could not find declaration or definition of symbol '${name}' ${position}`
151
- : `Unknown operator: \`${name}' ${position}`,
169
+ const range = getNameRange(location, name === "-." ? "-" : name);
170
+ if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
171
+ const missing = {
172
+ location: formatRange(range),
173
+ message: `Could not find declaration or definition of symbol '${name}'.`,
174
+ };
175
+ const fixity = getFixity(expression);
176
+ throw createSanyError(
177
+ fixity && location.range
178
+ ? [
179
+ missing,
180
+ {
181
+ location: formatRange(location.range),
182
+ message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
183
+ },
184
+ ]
185
+ : [missing],
152
186
  );
153
187
  };
154
188
 
@@ -235,13 +269,13 @@ const resolveApply = (
235
269
  scope: LocalName | undefined,
236
270
  unit: Unit,
237
271
  ) => {
238
- const callee = lookupLocal(scope, expression.operator)
239
- ? undefined
240
- : getVisibleDefinition(expression.operator, unit);
241
- const arity = getSymbolArity(expression, expression.operator, scope, unit);
272
+ const operator = expression.operator;
273
+ const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
274
+ const arity = getSymbolArity(expression, operator, scope, unit);
242
275
  if (arity !== expression.args.length) {
243
- throw new TlaSyntaxError(
244
- `The operator ${expression.operator} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
276
+ throw createSemanticError(
277
+ `The operator ${operator} requires ${arity} arguments.`,
278
+ expression.argumentsRange ?? getNameRange(expression.location, operator),
245
279
  );
246
280
  }
247
281
  expression.args.forEach((arg, index) => {
@@ -275,10 +309,12 @@ const resolveExpression = (
275
309
  case "ExceptAt":
276
310
  return;
277
311
  case "Identifier": {
278
- const arity = getSymbolArity(expression, expression.name, scope, unit);
312
+ const name = expression.name;
313
+ const arity = getSymbolArity(expression, name, scope, unit);
279
314
  if (arity > 0) {
280
- throw new TlaSyntaxError(
281
- `The operator ${expression.name} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
315
+ throw createSemanticError(
316
+ `The operator ${name} requires ${arity} arguments.`,
317
+ getNameRange(expression.location, name),
282
318
  );
283
319
  }
284
320
  return;
@@ -347,14 +383,21 @@ const resolveExpression = (
347
383
  resolveAll([expression.domain, expression.range]);
348
384
  return;
349
385
  case "Record":
350
- case "RecordSet":
351
- if (new Set(expression.fields.map((field) => field.name)).size < expression.fields.length) {
352
- throw new TlaSyntaxError(
353
- `Non-unique fields in constructor at line ${expression.location.line}, column ${expression.location.column}`,
386
+ case "RecordSet": {
387
+ const repeated = expression.fields.filter((field, index) =>
388
+ expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
389
+ );
390
+ if (repeated.length > 0) {
391
+ throw createSanyError(
392
+ repeated.map((field) => ({
393
+ location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
394
+ message: "Non-unique fields in constructor.",
395
+ })),
354
396
  );
355
397
  }
356
398
  resolveAll(expression.fields.map((field) => field.value));
357
399
  return;
400
+ }
358
401
  case "Except":
359
402
  resolveAll([
360
403
  expression.function,
@@ -769,6 +812,31 @@ export const resolveSubstitutions = (
769
812
  );
770
813
  };
771
814
 
815
+ // SANY reports the errors of every unit it checks, in the order of the units in the module.
816
+ export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
817
+ const errors: SanyError[] = [];
818
+ units
819
+ .toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
820
+ .forEach((unit) => {
821
+ try {
822
+ unit.check();
823
+ } catch (error) {
824
+ if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
825
+ error.sany.errors.forEach((item) => {
826
+ if (
827
+ errors.every(
828
+ (existing) =>
829
+ existing.location !== item.location || existing.message !== item.message,
830
+ )
831
+ ) {
832
+ errors.push(item);
833
+ }
834
+ });
835
+ }
836
+ });
837
+ if (errors.length > 0) throw createSanyError(errors);
838
+ };
839
+
772
840
  export const resolveModule = (module: Module, unit: ModuleUnit) => {
773
841
  const symbols = createSymbols(module, unit.imported);
774
842
  const facts = new Set(
@@ -782,13 +850,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
782
850
  self,
783
851
  facts,
784
852
  });
785
- module.definitions.forEach((definition) =>
786
- resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
787
- );
788
- module.assumptions.forEach((assumption) =>
789
- resolveExpression(assumption.expression, undefined, createUnit(assumption.location, undefined)),
790
- );
791
- module.theorems.forEach((theorem) =>
792
- resolveTheorem(theorem, createUnit(theorem.location, undefined)),
793
- );
853
+ collectSemanticErrors([
854
+ ...module.definitions.map((definition) => ({
855
+ location: definition.location,
856
+ check: () =>
857
+ resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
858
+ })),
859
+ ...module.assumptions.map((assumption) => ({
860
+ location: assumption.location,
861
+ check: () =>
862
+ resolveExpression(
863
+ assumption.expression,
864
+ undefined,
865
+ createUnit(assumption.location, undefined),
866
+ ),
867
+ })),
868
+ ...module.theorems.map((theorem) => ({
869
+ location: theorem.location,
870
+ check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
871
+ })),
872
+ ]);
794
873
  };
package/src/run-cli.ts CHANGED
@@ -10,16 +10,18 @@ import {
10
10
  import { spawnSync } from "node:child_process";
11
11
  import { basename, delimiter, dirname, join, resolve } from "node:path";
12
12
  import { availableParallelism } from "node:os";
13
- import type { CheckResult, CheckStatus, TraceStep } from "./check";
13
+ import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
14
14
  import { defineCommand, type ParsedArgs } from "./cli-options";
15
15
  import { parseConfig } from "./config";
16
16
  import {
17
+ createSanyError,
17
18
  getErrorExitCode,
18
19
  TlaConfigError,
19
20
  TlaError,
20
21
  TlaEvaluationError,
21
22
  TlaProofError,
22
23
  TlaSyntaxError,
24
+ type SanyReport,
23
25
  } from "./errors";
24
26
  import { EXIT_STATUS } from "./exit-codes";
25
27
  import type { LineRange } from "./obligation-generate";
@@ -135,14 +137,42 @@ const getTemporalHeadline = (names: string[]) =>
135
137
  ? `Error: Temporal property ${names[0]} was violated.\n`
136
138
  : `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
137
139
 
140
+ const getFrames = (callStack: string[]) =>
141
+ callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
142
+
143
+ const printCallStack = (frames: string) =>
144
+ console.log(
145
+ `Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
146
+ );
147
+
148
+ const printReplay = (replay: Replay) => {
149
+ replay.output.forEach((line) => console.log(line));
150
+ const frames = getFrames(replay.callStack ?? []);
151
+ const fingerprint = replay.fingerprint;
152
+ if (replay.isBeforeError) {
153
+ if (frames) printCallStack(frames);
154
+ } else if (replay.callStack && fingerprint) {
155
+ const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
156
+ console.log(
157
+ `Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
158
+ );
159
+ } else if (replay.callStack) {
160
+ printCallStack(frames);
161
+ }
162
+ };
163
+
138
164
  const printResult = (result: CheckResult) => {
139
165
  if (result.failure) {
166
+ const replay = result.failure.replay;
167
+ if (replay?.isBeforeError) printReplay(replay);
140
168
  console.log(`Error: ${result.failure.message}`);
141
169
  if (result.failure.initialState) {
142
170
  console.log("While working on the initial state:");
143
171
  printState(result.failure.initialState);
144
172
  }
145
173
  printTrace(result.failure.trace);
174
+ if (replay && !replay.isBeforeError) printReplay(replay);
175
+ if (result.failure.isWithoutStatistics) return;
146
176
  } else if (result.violation) {
147
177
  const violation = result.violation;
148
178
  const name = violation.name;
@@ -219,8 +249,14 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
219
249
  const fileName = basename(specPath, ".tla");
220
250
  const moduleName = parseModule(source).name;
221
251
  if (moduleName !== fileName) {
222
- throw new TlaSyntaxError(
223
- `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
252
+ throw createSanyError(
253
+ [
254
+ {
255
+ location: "Unknown location",
256
+ message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
257
+ },
258
+ ],
259
+ true,
224
260
  );
225
261
  }
226
262
  const configPath =
@@ -286,21 +322,32 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
286
322
  return result.failure?.exitCode ?? EXIT_CODES[result.status];
287
323
  };
288
324
 
289
- // TLC prints the errors it raises itself. tla-ts names the others, such as a module SANY rejects.
290
- const printError = (error: TlaError) => {
325
+ const formatSanyErrors = (report: SanyReport) =>
326
+ `*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
327
+
328
+ // TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
329
+ // others, such as a module it cannot parse.
330
+ const printError = (error: TlaError, path?: string) => {
291
331
  if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
292
332
  return console.log(`Error: ${error.message}`);
293
333
  }
294
- console.error(`${error.name}: ${error.message}`);
334
+ const report = error instanceof TlaSyntaxError ? error.sany : undefined;
335
+ if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
336
+ console.log(
337
+ report.isFatal
338
+ ? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
339
+ : `Semantic errors:\n\n${formatSanyErrors(report)}`,
340
+ );
341
+ console.log("Error: Parsing or semantic analysis failed.");
295
342
  };
296
343
 
297
344
  // Prints a TlaError and returns its exit code.
298
- const reportErrors = async (run: () => number | Promise<number>) => {
345
+ const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
299
346
  try {
300
347
  return await run();
301
348
  } catch (error) {
302
349
  if (!(error instanceof TlaError)) throw error;
303
- printError(error);
350
+ printError(error, path);
304
351
  return getErrorExitCode(error);
305
352
  }
306
353
  };
@@ -311,7 +358,7 @@ const runEach = async (paths: string[], runPath: (path: string) => number | Prom
311
358
  const exitCodes: number[] = [];
312
359
  for (const [index, path] of paths.entries()) {
313
360
  if (index > 0) console.log("");
314
- exitCodes.push(await reportErrors(() => runPath(path)));
361
+ exitCodes.push(await reportErrors(() => runPath(path), path));
315
362
  }
316
363
  return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
317
364
  };