Refactor Expected Types During Compilation

By adding an expected type to the compilation context, it is
now possible for type checking to occur on keyset expressions
and return statements at the moment that they are being compiled.

Previously, it was necessary to tentatively compile them and then
typecheck afterward.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-10 00:59:11 -04:00
parent ad7e5a6f6d
commit b687454389
8 changed files with 112 additions and 115 deletions
+11 -1
View File
@@ -310,7 +310,17 @@ extension ReturnStatement: CompilableStatement {
let expression_node = node.child(at: 1)!
return switch Expression.Compile(node: expression_node, withContext: context) {
case .Ok(let result): .Ok((ReturnStatement(result), context))
case .Ok(let result):
if result.type().eq(rhs: context.expected_type!) {
.Ok((ReturnStatement(result), context))
} else {
.Error(
ErrorOnNode(
node: node,
withError:
"Type of expression in return statement (\(result.type())) is not compatible with function return type (\(context.expected_type!))"
))
}
case .Error(let e): .Error(e)
}
}