From 6fdf2a3f96d4fad0949a69b61741f55eb922455c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 4 Apr 2023 12:13:00 -0500 Subject: [PATCH] added: better error messages with interface errors --- compiler/include/astnodes.h | 2 ++ compiler/src/checker.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index ab95741d..c0ec84f0 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1123,6 +1123,8 @@ typedef struct InterfaceConstraint { AstType *expected_type_expr; Type *expected_type; + char *error_msg; + b32 invert_condition: 1; } InterfaceConstraint; diff --git a/compiler/src/checker.c b/compiler/src/checker.c index be9c4f2c..086e2dab 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -3322,8 +3322,11 @@ CheckStatus check_constraint(AstConstraint *constraint) { } TYPE_CHECK(&ic->expr, ic->expected_type) { - if (!ic->invert_condition) + if (!ic->invert_condition) { + ic->error_msg = bh_aprintf(global_heap_allocator, "Expected expression to be of type %s, got expression of type %s.", + type_get_name(ic->expected_type), type_get_name(ic->expr->type)); goto constraint_error; + } } } @@ -3370,13 +3373,16 @@ CheckStatus check_constraint_context(ConstraintContext *cc, Scope *scope, OnyxFi } OnyxFilePos error_pos; + char *error_msg = NULL; if (constraint->exprs) { error_pos = constraint->exprs[constraint->expr_idx].expr->token->pos; + error_msg = constraint->exprs[constraint->expr_idx].error_msg; } else { error_pos = constraint->interface->token->pos; } onyx_report_error(error_pos, Error_Critical, "Failed to satisfy constraint where %s.", constraint_map); + if (error_msg) onyx_report_error(error_pos, Error_Critical, error_msg); onyx_report_error(constraint->token->pos, Error_Critical, "Here is where the interface was used."); onyx_report_error(pos, Error_Critical, "Here is the code that caused this constraint to be checked."); -- 2.25.1