fix: return proper type in non-array index error (instead of ?)

This commit is contained in:
Gleb Koval 2025-02-07 14:32:15 +00:00
parent 88ddca2b98
commit ba1b7d67c7
Signed by: cyclane
GPG Key ID: 15E168A8B332382C

View File

@ -159,17 +159,18 @@ object typeChecker {
ctx.typeOf(id).satisfies(constraint, id.pos) ctx.typeOf(id).satisfies(constraint, id.pos)
case ArrayElem(id, indices) => case ArrayElem(id, indices) =>
val arrayTy = ctx.typeOf(id) val arrayTy = ctx.typeOf(id)
val elemTy = indices.foldLeft(arrayTy) { (acc, elem) => val elemTy = indices.foldLeftM(arrayTy) { (acc, elem) =>
checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int")) checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int"))
acc match { acc match {
case KnownType.Array(innerTy) => innerTy case KnownType.Array(innerTy) => Some(innerTy)
case _ => case nonArrayTy =>
ctx.error( ctx.error(
Error.TypeMismatch(elem.pos, KnownType.Array(?), acc, "cannot index into a non-array") Error.TypeMismatch(elem.pos, KnownType.Array(?), acc, "cannot index into a non-array")
) )
None
} }
} }
elemTy.satisfies(constraint, id.pos) elemTy.getOrElse(?).satisfies(constraint, id.pos)
case Parens(expr) => checkValue(expr, constraint) case Parens(expr) => checkValue(expr, constraint)
case l @ ArrayLiter(elems) => case l @ ArrayLiter(elems) =>
KnownType KnownType