Type checking unannotated expressions

This commit is contained in:
Dustin Swan 2026-03-26 20:04:46 -06:00
parent de83eb6fcd
commit a4daf88085
No known key found for this signature in database
GPG key ID: 30D46587E2100467
2 changed files with 9 additions and 4 deletions

View file

@ -1,7 +1,7 @@
point : Int \ Int \ { x : Int, y : Int } = x y \ { x = x, y = y }; point : Int \ Int \ { x : Int, y : Int } = x y \ { x = x, y = y };
getX : { x : Int, y : Int } \ Int = p \ p.x; getX : { x : Int, y : Int } \ Int = p \ p.x;
setX : Int \ { x : Int, y : Int } \ { x : Int, y : Int } = newX p \ p.{ x = newX }; setX : Int \ { x : Int, y : Int } \ { x : Int, y : Int } = newX p \ p.{ x = newX };
myPoint = point 3 "a"; myPoint = point 3 Blah;
# builtins # builtins
# TODO: once we get typeclasses, make these the actual types # TODO: once we get typeclasses, make these the actual types

View file

@ -340,10 +340,15 @@ export function typecheck(defs: Definition[], typeDefs: TypeDefinition[] = []) {
// Check each annotated def // Check each annotated def
for (const def of defs) { for (const def of defs) {
if (def.annotation && def.body) { if (def.body) {
const subst: Subst = new Map(); const subst: Subst = new Map();
const err = check(def.body, def.annotation.type, env, subst); if (def.annotation) {
if (err) warn(err, def.body); const err = check(def.body, def.annotation.type, env, subst);
if (err) warn(err, def.body);
} else {
const t = infer(def.body, env, subst);
if (t) env.set(def.name, t);
}
} }
} }
} }