diff --git a/tccgen.c b/tccgen.c index 863d6a4d..73680300 100644 --- a/tccgen.c +++ b/tccgen.c @@ -6351,6 +6351,14 @@ static void expr_cond(void) skip(':'); expr_cond(); + if ((vtop->type.t & VT_BTYPE) == VT_FUNC) + mk_pointer(&vtop->type); + + /* cast operands to correct type according to ISOC rules */ + if (!combine_types(&type, &sv, vtop, '?')) + type_incompatibility_error(&sv.type, &vtop->type, + "type mismatch in conditional expression (have '%s' and '%s')"); + if (c < 0 && is_cond_bool(vtop) && is_cond_bool(&sv)) { /* optimize "if (f ? a > b : c || d) ..." for example, where normally "a < b" and "c || d" would be forced to "(int)0/1" first, whereas @@ -6362,17 +6370,11 @@ static void expr_cond(void) /* combine jump targets of 2nd op with VT_CMP of 1st op */ gvtst_set(0, t1); gvtst_set(1, t2); + gen_cast(&type); // tcc_warning("two conditions expr_cond"); return; } - if ((vtop->type.t & VT_BTYPE) == VT_FUNC) - mk_pointer(&vtop->type); - - /* cast operands to correct type according to ISOC rules */ - if (!combine_types(&type, &sv, vtop, '?')) - type_incompatibility_error(&sv.type, &vtop->type, - "type mismatch in conditional expression (have '%s' and '%s')"); /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so that `(expr ? a : b).mem` does not error with "lvalue expected" */ islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE); diff --git a/tests/tcctest.c b/tests/tcctest.c index f9df38ab..29f88bd4 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -1269,6 +1269,7 @@ void bool_test() t = 1; printf("type of bool: %d\n", (int) ( (~ ((unsigned int) (t && 1))) / 2) ); + printf("type of cond: %d\n", (~(t ? 0U : (unsigned int)0)) / 2 ); /* test ? : cast */ { int aspect_on;