Browse Source

Improve ternary if type inference

gingerBill 3 years ago
parent
commit
dca2fbccff
1 changed files with 5 additions and 1 deletions
  1. 5 1
      src/check_expr.cpp

+ 5 - 1
src/check_expr.cpp

@@ -7241,7 +7241,11 @@ ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *node, Type *t
 	node->viral_state_flags |= te->x->viral_state_flags;
 	node->viral_state_flags |= te->x->viral_state_flags;
 
 
 	if (te->y != nullptr) {
 	if (te->y != nullptr) {
-		check_expr_or_type(c, &y, te->y, type_hint);
+		Type *th = type_hint;
+		if (type_hint == nullptr && is_type_typed(x.type)) {
+			th = x.type;
+		}
+		check_expr_or_type(c, &y, te->y, th);
 		node->viral_state_flags |= te->y->viral_state_flags;
 		node->viral_state_flags |= te->y->viral_state_flags;
 	} else {
 	} else {
 		error(node, "A ternary expression must have an else clause");
 		error(node, "A ternary expression must have an else clause");