From 332c9910640840440f0be5c2d4d884aba47d175a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 20 Jan 2025 10:17:01 +0900 Subject: [PATCH] [qfcc] Do type promotions for conditional expressions Where possible of course, but the two results need to have the same type. When not possible, an error is generated. --- tools/qfcc/source/expr_process.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/qfcc/source/expr_process.c b/tools/qfcc/source/expr_process.c index bbcd9d7c3..c76b44fc3 100644 --- a/tools/qfcc/source/expr_process.c +++ b/tools/qfcc/source/expr_process.c @@ -620,6 +620,17 @@ proc_cond (const expr_t *expr, rua_ctx_t *ctx) if (is_error (false_expr)) { return false_expr; } + auto true_type = get_type (true_expr); + auto false_type = get_type (false_expr); + if (!type_same (true_type, false_type)) { + if (type_promotes (true_type, false_type)) { + false_expr = cast_expr (true_type, false_expr); + } else if (type_promotes (false_type, true_type)) { + true_expr = cast_expr (false_type, true_expr); + } else { + return error (expr, "incompatible types in conditional expression"); + } + } return new_cond_expr (test, true_expr, false_expr); }