Files
scriptc/tests/diagnostics/contract-int32.ts
T
Chris Tate a711d74ecf Keep copied array callbacks exact with local classes, switches and contracts
- A binding that a local class captures counts as written: the class's
  methods may assign it while the loop runs, so the call keeps its helper.
- Copied bodies rename the labels of switch and for-of statements and the
  binding of for-of loops.
- The known-union and record-temporary cleanups rewrite switch cases and
  for-of bodies, where they left declarations of removed locals.
- Copied locals keep their source bindings. In programs that write
  contract types, a parameter with a source binding keeps its own local,
  so an Int32 parameter is checked against the elements the loop passes.
2026-10-24 17:35:51 -05:00

39 lines
776 B
TypeScript

// Int32 contracts that do not hold: each write that is not an int32 by
// construction is reported with the expression and the `| 0` fix.
import type { Int32 } from "@scriptc/types";
class Span {
pos: Int32 = 0;
end: Int32 = 0;
readonly id: Int32;
constructor(id: number) {
this.id = id;
}
advance(): void {
this.pos++;
}
}
function width(span: Span): Int32 {
return span.end - span.pos;
}
function half(x: Int32): Int32 {
return x / 2;
}
let count: Int32 = 0;
function bump(): void {
count += 1;
}
const sizes = [1, 2, 3];
const len: Int32 = sizes.length;
const weights = [1.5, 2];
const shifted = weights.map((w: Int32) => w + 1);
const span = new Span(1.5);
span.advance();
bump();
console.log(width(span), half(7), count, len, shifted);