added: better error messages with interface errors
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 4 Apr 2023 17:13:00 +0000 (12:13 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 4 Apr 2023 17:13:00 +0000 (12:13 -0500)
compiler/include/astnodes.h
compiler/src/checker.c

index ab95741dbbc5f4cba280d532ea59f1bb0ab0e112..c0ec84f0a168463f8e87cce084afad3d087abcbf 100644 (file)
@@ -1123,6 +1123,8 @@ typedef struct InterfaceConstraint {
     AstType  *expected_type_expr;
     Type     *expected_type;
 
+    char *error_msg;
+
     b32 invert_condition: 1;
 } InterfaceConstraint;
 
index be9c4f2cfaac6b44e3d300de193091f75d3f001a..086e2dab86e0b02f4b0954adbce99bf36a2c4dab 100644 (file)
@@ -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.");