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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user