mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Improve ternary if expression type inference rues
Allow for expression like this
`x: union{f32} = f32(123) if cond else nil`
This commit is contained in:
+11
-2
@@ -7518,16 +7518,25 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n
|
|||||||
return kind;
|
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) {
|
if (x.mode == Addressing_Invalid) {
|
||||||
return kind;
|
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) {
|
if (y.mode == Addressing_Invalid) {
|
||||||
x.mode = Addressing_Invalid;
|
x.mode = Addressing_Invalid;
|
||||||
return kind;
|
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)) {
|
if (!ternary_compare_types(x.type, y.type)) {
|
||||||
gbString its = type_to_string(x.type);
|
gbString its = type_to_string(x.type);
|
||||||
gbString ets = type_to_string(y.type);
|
gbString ets = type_to_string(y.type);
|
||||||
|
|||||||
Reference in New Issue
Block a user