diff options
| author | gingerBill <bill@gingerbill.org> | 2021-07-05 16:23:13 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-07-05 16:23:13 +0100 |
| commit | a98eee145d60a42324835c578d8573675d6fdd49 (patch) | |
| tree | 2f4824a5812115ff84277d798b7322e97f962811 /src/check_expr.cpp | |
| parent | c6b9b3b9a41d6e049d39780933067d44cf2b6469 (diff) | |
Remove `try`; Replace `try x else y` with `or_else(x, y)`
Diffstat (limited to 'src/check_expr.cpp')
| -rw-r--r-- | src/check_expr.cpp | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 6ff43ecab..20e54ab3d 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -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; |