fix: display function type on incorrect number of args

This commit is contained in:
Gleb Koval 2025-02-07 18:09:18 +00:00
parent 4d25d7a730
commit 5ea3ca5a03
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
2 changed files with 9 additions and 6 deletions

View File

@ -10,7 +10,7 @@ enum Error {
case UndeclaredVariable(ident: ast.Ident)
case UndefinedFunction(ident: ast.Ident)
case FunctionParamsMismatch(pos: Position, expected: Int, got: Int)
case FunctionParamsMismatch(ident: ast.Ident, expected: Int, got: Int, funcType: FuncType)
case SemanticError(pos: Position, msg: String)
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
case InternalError(pos: Position, msg: String)
@ -38,10 +38,13 @@ def printError(error: Error)(using errorContent: String): Unit = {
printPosition(ident.pos)
println(s"Undefined function ${ident.v}")
highlight(ident.pos, ident.v.length)
case Error.FunctionParamsMismatch(pos, expected, got) =>
printPosition(pos)
case Error.FunctionParamsMismatch(id, expected, got, funcType) =>
printPosition(id.pos)
println(s"Function expects $expected parameters, got $got")
highlight(pos, 1)
println(
s"(function ${id.v} has type (${funcType.params.mkString(", ")}) -> ${funcType.returnType})"
)
highlight(id.pos, 1)
case Error.TypeMismatch(pos, expected, got, msg) =>
printPosition(pos)
println(s"Type mismatch: $msg\nExpected: $expected\nGot: $got")

View File

@ -189,9 +189,9 @@ object typeChecker {
)
.satisfies(constraint, l.pos)
case Call(id, args) =>
val FuncType(retTy, paramTys) = ctx.funcType(id)
val funcTy @ FuncType(retTy, paramTys) = ctx.funcType(id)
if (args.length != paramTys.length) {
ctx.error(Error.FunctionParamsMismatch(id.pos, paramTys.length, args.length))
ctx.error(Error.FunctionParamsMismatch(id, paramTys.length, args.length, funcTy))
}
args.zip(paramTys).foreach { case (arg, paramTy) =>
checkValue(arg, Constraint.Is(paramTy, s"argument type mismatch in function ${id.v}"))