harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 4a97bd2bafbd6e89f692c18477a08d8112dbd6e1
parent 273bc207f596c169d875dc95b3007983daae8932
Author: Nixon Enraght-Moony <nixon.emoony@gmail.com>
Date:   Fri,  6 May 2022 22:00:32 +0100

Improve Error Messages.

This is random and non-systematic, because it's the errors I happened to
run into.

On code
    type point = struct {x: int, y: int};
    fn add_point(p: point) int = append(p, 0);
Old:
    Error bad_index.ha:2:38: expression must operate on a slice
New:
    Error bad_index.ha:2:38: append expression must operate on a slice, but got point

On code
    fn varargs(ints: int...) void = void;
    fn arr() void = {
        [false, "x"];
        varargs("why");
    };
Old:
    Error ./bad_array.ha:3:15: Array members must be of a uniform type
    Error ./bad_array.ha:4:17: Array members must be of a uniform type
    Error ./bad_array.ha:4:17: Argument is not assignable to variadic parameter type
New:
    Error ./bad_array.ha:3:15: Array members must be of a uniform type, previously seen bool, but now see str
    Error ./bad_array.ha:4:17: Array members must be of a uniform type, previously seen int, but now see str
    Error ./bad_array.ha:4:17: Argument is not assignable to variadic parameter type

On code
    fn demo() void = {
        "does this work"?;
    };
Old:
    Error bad_err.ha:2:21: Cannot use error propagation with non-tagged type
New:
    Error bad_err.ha:2:21: Cannot use error propagation with non-tagged type str

Slightly clairify error for indexing invalid types.

It's marginal but putting the actual type not right next to the valid
types, and not using a double negation makes the error easier (at least
for me) to understand at a glance.

On code
    fn first_char(s: str) rune = s[0];
Old:
    Error bad_index.ha:1:31: Cannot index non-array, non-slice str object
New:
    Error bad_index.ha:1:31: Can only index into array or slice object, but got str

Signed-off-by: Nixon Enraght-Moony <nixon.emoony@gmail.com>

Diffstat:
Msrc/check.c | 19+++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -192,7 +192,7 @@ check_expr_access(struct context *ctx, if (atype->storage != STORAGE_ARRAY && atype->storage != STORAGE_SLICE) { error(ctx, aexpr->access.array->loc, expr, - "Cannot index non-array, non-slice %s object", + "Can only index into array or slice object, but got %s", type_storage_unparse(atype->storage)); return; } @@ -434,26 +434,27 @@ check_expr_append_insert(struct context *ctx, }; const struct type *sltype; + const struct type *sltypename; const char *exprtype_name; switch (expr->type) { case EXPR_APPEND: - sltype = type_dereference(expr->append.object->result); - sltype = type_dealias(sltype); + sltypename = expr->append.object->result; exprtype_name = "append"; break; case EXPR_INSERT: assert(expr->append.object->access.type == ACCESS_INDEX); - sltype = expr->append.object->access.array->result; - sltype = type_dealias(type_dereference(sltype)); + sltypename = expr->append.object->access.array->result; exprtype_name = "insert"; break; default: abort(); // Invariant } + sltype = type_dealias(type_dereference(sltypename)); if (sltype->storage != STORAGE_SLICE) { error(ctx, aexpr->append.object->loc, expr, - "expression must operate on a slice"); + "%s expression must operate on a slice, but got %s", + exprtype_name, gen_typename(sltypename)); return; } if (sltype->flags & TYPE_CONST) { @@ -1321,7 +1322,8 @@ check_expr_array(struct context *ctx, } if (!type_is_assignable(type, value->result)) { error(ctx, item->value->loc, expr, - "Array members must be of a uniform type"); + "Array members must be of a uniform type, previously seen %s, but now see %s", + gen_typename(type), gen_typename(value->result)); return; } if (!hint) { @@ -1956,7 +1958,8 @@ check_expr_propagate(struct context *ctx, const struct type *intype = lvalue->result; if (type_dealias(intype)->storage != STORAGE_TAGGED) { error(ctx, aexpr->loc, expr, - "Cannot use error propagation with non-tagged type"); + "Cannot use error propagation on non-tagged type %s", + gen_typename(intype)); return; } if (!aexpr->propagate.abort) {