Browse Source

Allow casting of `#optional_ok` call expressions

gingerBill 4 years ago
parent
commit
3612569624
2 changed files with 25 additions and 4 deletions
  1. 22 1
      src/check_expr.cpp
  2. 3 3
      src/check_stmt.cpp

+ 22 - 1
src/check_expr.cpp

@@ -2231,6 +2231,26 @@ bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
 		return true;
 	}
 
+	if (is_type_tuple(src)) {
+		Ast *expr = unparen_expr(operand->expr);
+		if (expr && expr->kind == Ast_CallExpr) {
+			// NOTE(bill, 2021-04-19): Allow casting procedure calls with #optional_ok
+			ast_node(ce, CallExpr, expr);
+			Type *pt = base_type(type_of_expr(ce->proc));
+			if (pt->kind == Type_Proc && pt->Proc.optional_ok) {
+				if (pt->Proc.result_count > 0) {
+					Operand op = *operand;
+					op.type = pt->Proc.results->Tuple.variables[0]->type;
+					bool ok = check_is_castable_to(c, &op, y);
+					if (ok) {
+						ce->optional_ok_one = true;
+					}
+					return ok;
+				}
+			}
+		}
+	}
+
 	if (is_constant && is_type_untyped(src) && is_type_string(src)) {
 		if (is_type_u8_array(dst)) {
 			String s = operand->value.value_string;
@@ -2346,6 +2366,7 @@ bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) {
 	if (is_type_rawptr(src) && is_type_proc(dst)) {
 		return true;
 	}
+
 	return false;
 }
 
@@ -10598,7 +10619,7 @@ void check_not_tuple(CheckerContext *c, Operand *o) {
 		if (o->type->kind == Type_Tuple) {
 			isize count = o->type->Tuple.variables.count;
 			error(o->expr,
-			      "%td-valued tuple found where single value expected", count);
+			      "%td-valued expression found where single value expected", count);
 			o->mode = Addressing_Invalid;
 			GB_ASSERT(count != 1);
 		}

+ 3 - 3
src/check_stmt.cpp

@@ -1751,7 +1751,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
 						Type *cond_type = t->Tuple.variables[count-1]->type;
 						if (!is_type_boolean(cond_type)) {
 							gbString s = type_to_string(cond_type);
-							error(operand.expr, "The final type of %td-valued tuple must be a boolean, got %s", count, s);
+							error(operand.expr, "The final type of %td-valued expression must be a boolean, got %s", count, s);
 							gb_string_free(s);
 							break;
 						}
@@ -1762,14 +1762,14 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
 
 						if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) {
 							gbString s = type_to_string(t);
-							error(operand.expr, "Expected a 3-value tuple on the rhs, got (%s)", s);
+							error(operand.expr, "Expected a 3-valued expression on the rhs, got (%s)", s);
 							gb_string_free(s);
 							break;
 						}
 
 						if (rs->vals.count > 0 && rs->vals[0] != nullptr && count < 2) {
 							gbString s = type_to_string(t);
-							error(operand.expr, "Expected at least a 2-values tuple on the rhs, got (%s)", s);
+							error(operand.expr, "Expected at least a 2-valued expression on the rhs, got (%s)", s);
 							gb_string_free(s);
 							break;
 						}