extract and test inference context

This commit is contained in:
nora 2023-07-27 22:10:21 +02:00
parent 39a995b765
commit 84cd8eec90
3 changed files with 176 additions and 133 deletions

36
src/typeck.test.ts Normal file
View file

@ -0,0 +1,36 @@
import { TY_INT, TY_STRING } from "./ast";
import { DUMMY_SPAN as SPAN } from "./error";
import { InferContext } from "./typeck";
it("should infer types across assignments", () => {
const infcx = new InferContext();
const a = infcx.newVar();
const b = infcx.newVar();
const c = infcx.newVar();
infcx.assign(a, b, SPAN);
infcx.assign(b, c, SPAN);
infcx.assign(a, TY_INT, SPAN);
const aTy = infcx.resolveIfPossible(c);
const bTy = infcx.resolveIfPossible(c);
const cTy = infcx.resolveIfPossible(c);
expect(aTy.kind).toEqual("int");
expect(bTy.kind).toEqual("int");
expect(cTy.kind).toEqual("int");
});
it("should conflict assignments to resolvable type vars", () => {
const infcx = new InferContext();
const a = infcx.newVar();
const b = infcx.newVar();
infcx.assign(a, b, SPAN);
infcx.assign(b, TY_INT, SPAN);
expect(() => infcx.assign(a, TY_STRING, SPAN)).toThrow();
});