Remove try; Replace try x else y with or_else(x, y)

This commit is contained in:
gingerBill
2021-07-05 16:23:13 +01:00
parent c6b9b3b9a4
commit a98eee145d
9 changed files with 159 additions and 385 deletions

View File

@@ -6233,184 +6233,6 @@ void check_promote_optional_ok(CheckerContext *c, Operand *x, Type **val_type_,
GB_ASSERT(is_type_tuple(type_of_expr(x->expr)));
}
void check_try_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
Type *left_type = nullptr;
Type *right_type = nullptr;
if (x->type->kind == Type_Tuple) {
auto const &vars = x->type->Tuple.variables;
auto lhs = array_slice(vars, 0, vars.count-1);
auto rhs = vars[vars.count-1];
if (lhs.count == 1) {
left_type = lhs[0]->type;
} else if (lhs.count != 0) {
left_type = alloc_type_tuple();
left_type->Tuple.variables = array_make_from_ptr(lhs.data, lhs.count, lhs.count);
}
right_type = rhs->type;
} else {
check_promote_optional_ok(c, x, &left_type, &right_type);
}
if (left_type_) *left_type_ = left_type;
if (right_type_) *right_type_ = right_type;
if (!type_has_nil(right_type) && !is_type_boolean(right_type)) {
gbString str = type_to_string(right_type);
error(x->expr, "'%.*s' expects an \"optional ok\" like value, or an n-valued expression where the last value is either a boolean or can be compared against 'nil', got %s", LIT(name), str);
gb_string_free(str);
}
}
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");
ast_node(te, TryExpr, node);
Operand x = {};
check_multi_expr_with_type_hint(c, &x, te->expr, type_hint);
if (x.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
o->type = t_invalid;
return Expr_Expr;
}
if (c->in_defer) {
error(node, "'%.*s' cannot be used within a defer statement", LIT(name));
}
Type *left_type = nullptr;
Type *right_type = nullptr;
check_try_split_types(c, &x, name, &left_type, &right_type);
add_type_and_value(&c->checker->info, te->expr, x.mode, x.type, x.value);
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;
if (result_type == nullptr) {
error(node, "'%.*s' requires the current procedure to have at least one return value", LIT(name));
} else {
GB_ASSERT(result_type->kind == Type_Tuple);
auto const &vars = result_type->Tuple.variables;
Type *end_type = vars[vars.count-1]->type;
if (vars.count > 1) {
if (!proc_type->Proc.has_named_results) {
error(node, "'%.*s' within a procedure with more than 1 return value requires that the return values are named, allowing for early return", LIT(name));
}
}
Operand rhs = {};
rhs.type = right_type;
rhs.mode = Addressing_Value;
// TODO(bill): better error message
if (!check_is_assignable_to(c, &rhs, end_type)) {
gbString a = type_to_string(right_type);
gbString b = type_to_string(end_type);
gbString ret_type = type_to_string(result_type);
error(node, "Cannot assign end value of type '%s' to '%s' in '%.*s'", a, b, LIT(name));
if (vars.count == 1) {
error_line("\tProcedure return value type: %s\n", ret_type);
} else {
error_line("\tProcedure return value types: (%s)\n", ret_type);
}
gb_string_free(ret_type);
gb_string_free(b);
gb_string_free(a);
}
}
}
if (left_type != nullptr) {
o->mode = Addressing_Value;
o->type = left_type;
} else {
o->mode = Addressing_NoValue;
o->type = nullptr;
}
return Expr_Expr;
}
ExprKind check_try_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
String name = str_lit("try else");
ast_node(te, TryElseExpr, node);
Operand x = {};
Operand y = {};
check_multi_expr_with_type_hint(c, &x, te->expr, type_hint);
if (x.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
o->type = t_invalid;
return Expr_Expr;
}
check_multi_expr_with_type_hint(c, &y, te->else_expr, x.type);
error_operand_no_value(&y);
if (y.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
o->type = t_invalid;
return Expr_Expr;
}
Type *left_type = nullptr;
Type *right_type = nullptr;
check_try_split_types(c, &x, name, &left_type, &right_type);
add_type_and_value(&c->checker->info, te->expr, x.mode, x.type, x.value);
if (left_type != nullptr) {
check_assignment(c, &y, left_type, name);
} else {
check_try_expr_no_value_error(c, name, x, type_hint);
}
if (left_type == nullptr) {
left_type = t_invalid;
}
o->mode = Addressing_Value;
o->type = left_type;
return Expr_Expr;
}
ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type *type_hint) {
u32 prev_state_flags = c->state_flags;
@@ -7781,14 +7603,6 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
}
case_end;
case_ast_node(te, TryExpr, node);
return check_try_expr(c, o, node, type_hint);
case_end;
case_ast_node(te, TryElseExpr, node);
return check_try_else_expr(c, o, node, type_hint);
case_end;
case_ast_node(se, SelectorExpr, node);
check_selector(c, o, node, type_hint);
node->viral_state_flags |= se->expr->viral_state_flags;