mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-08 08:38:51 +00:00
Improve error messages for try expressions
This commit is contained in:
+34
-3
@@ -6263,6 +6263,34 @@ void check_try_split_types(CheckerContext *c, Operand *x, String const &name, Ty
|
||||
}
|
||||
|
||||
|
||||
void check_try_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) {
|
||||
// TODO(bill): better error message
|
||||
gbString t = type_to_string(x.type);
|
||||
error(x.expr, "'%.*s' does not return a value, value is of type %s", LIT(name), t);
|
||||
if (is_type_union(type_deref(x.type))) {
|
||||
Type *bsrc = base_type(type_deref(x.type));
|
||||
gbString th = nullptr;
|
||||
if (type_hint != nullptr) {
|
||||
GB_ASSERT(bsrc->kind == Type_Union);
|
||||
for_array(i, bsrc->Union.variants) {
|
||||
Type *vt = bsrc->Union.variants[i];
|
||||
if (are_types_identical(vt, type_hint)) {
|
||||
th = type_to_string(type_hint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
gbString expr_str = expr_to_string(x.expr);
|
||||
if (th != nullptr) {
|
||||
error_line("\tSuggestion: was a type assertion such as %s.(%s) or %s.? wanted?\n", expr_str, th, expr_str);
|
||||
} else {
|
||||
error_line("\tSuggestion: was a type assertion such as %s.(T) or %s.? wanted?\n", expr_str, expr_str);
|
||||
}
|
||||
gb_string_free(th);
|
||||
gb_string_free(expr_str);
|
||||
}
|
||||
gb_string_free(t);
|
||||
}
|
||||
ExprKind check_try_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
|
||||
String name = str_lit("try");
|
||||
|
||||
@@ -6288,6 +6316,10 @@ ExprKind check_try_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hin
|
||||
if (c->curr_proc_sig == nullptr) {
|
||||
error(node, "'%.*s' can only be used within a procedure", LIT(name));
|
||||
}
|
||||
|
||||
if (right_type == nullptr) {
|
||||
check_try_expr_no_value_error(c, name, x, type_hint);
|
||||
} else {
|
||||
Type *proc_type = base_type(c->curr_proc_sig);
|
||||
GB_ASSERT(proc_type->kind == Type_Proc);
|
||||
Type *result_type = proc_type->Proc.results;
|
||||
@@ -6325,7 +6357,7 @@ ExprKind check_try_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hin
|
||||
gb_string_free(a);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (left_type != nullptr) {
|
||||
o->mode = Addressing_Value;
|
||||
@@ -6367,8 +6399,7 @@ ExprKind check_try_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *typ
|
||||
if (left_type != nullptr) {
|
||||
check_assignment(c, &y, left_type, name);
|
||||
} else {
|
||||
// TODO(bill): better error message
|
||||
error(node, "'%.*s' does not return a value", LIT(name));
|
||||
check_try_expr_no_value_error(c, name, x, type_hint);
|
||||
}
|
||||
|
||||
if (left_type == nullptr) {
|
||||
|
||||
Reference in New Issue
Block a user