diff options
| author | gingerBill <bill@gingerbill.org> | 2023-01-30 15:29:59 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-01-30 15:29:59 +0000 |
| commit | f59846377d1877302abc108bab5ef1b0610e55ba (patch) | |
| tree | 45ca1233d31ddf3ede3adf97841e35553b745b08 /src/check_expr.cpp | |
| parent | 8e8eb9e5cd4ba986d6125305e77cb82238bbcf0d (diff) | |
Improve ternary logic for untyped nil stuff
Diffstat (limited to 'src/check_expr.cpp')
| -rw-r--r-- | src/check_expr.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 27efbc9ae..47f453548 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7518,11 +7518,13 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n return kind; } - convert_to_typed(c, &x, type_hint ? type_hint : y.type); + bool use_type_hint = type_hint != nullptr && (is_operand_nil(x) || is_operand_nil(y)); + + convert_to_typed(c, &x, use_type_hint ? type_hint : y.type); if (x.mode == Addressing_Invalid) { return kind; } - convert_to_typed(c, &y, type_hint ? type_hint : x.type); + convert_to_typed(c, &y, use_type_hint ? type_hint : x.type); if (y.mode == Addressing_Invalid) { x.mode = Addressing_Invalid; return kind; @@ -7530,7 +7532,7 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n // NOTE(bill, 2023-01-30): Allow for expression like this: // x: union{f32} = f32(123) if cond else nil - if (type_hint && !ternary_compare_types(x.type, y.type)) { + if (type_hint && !is_type_any(type_hint) && !ternary_compare_types(x.type, y.type)) { if (check_is_assignable_to(c, &x, type_hint) && check_is_assignable_to(c, &y, type_hint)) { check_cast(c, &x, type_hint); check_cast(c, &y, type_hint); |