diff options
| author | gingerBill <bill@gingerbill.org> | 2023-01-30 12:54:11 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-01-30 12:54:11 +0000 |
| commit | 8e8eb9e5cd4ba986d6125305e77cb82238bbcf0d (patch) | |
| tree | 7d3bd09992b95918ebb084efe973410a43f81e55 /src/check_expr.cpp | |
| parent | 88b578ca11a9042e1e35a4ab272f18debe6c7a79 (diff) | |
Improve ternary if expression type inference rues
Allow for expression like this
`x: union{f32} = f32(123) if cond else nil`
Diffstat (limited to 'src/check_expr.cpp')
| -rw-r--r-- | src/check_expr.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index df293e955..27efbc9ae 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7518,16 +7518,25 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n return kind; } - convert_to_typed(c, &x, y.type); + convert_to_typed(c, &x, type_hint ? type_hint : y.type); if (x.mode == Addressing_Invalid) { return kind; } - convert_to_typed(c, &y, x.type); + convert_to_typed(c, &y, type_hint ? type_hint : x.type); if (y.mode == Addressing_Invalid) { x.mode = Addressing_Invalid; return kind; } + // 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 (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); + } + } + if (!ternary_compare_types(x.type, y.type)) { gbString its = type_to_string(x.type); gbString ets = type_to_string(y.type); |