mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Correct ternary if expression type determination
This commit is contained in:
+14
-9
@@ -837,8 +837,6 @@ void check_assignment(CheckerContext *c, Operand *operand, Type *type, String co
|
|||||||
if (operand->mode == Addressing_Type && is_type_typeid(type)) {
|
if (operand->mode == Addressing_Type && is_type_typeid(type)) {
|
||||||
add_type_info_type(c, operand->type);
|
add_type_info_type(c, operand->type);
|
||||||
add_type_and_value(c->info, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
|
add_type_and_value(c->info, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
|
||||||
} else {
|
|
||||||
convert_to_typed(c, operand, type);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
gbString expr_str = expr_to_string(operand->expr);
|
gbString expr_str = expr_to_string(operand->expr);
|
||||||
@@ -3226,9 +3224,9 @@ void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
|
|||||||
|
|
||||||
case Basic_UntypedNil:
|
case Basic_UntypedNil:
|
||||||
if (is_type_any(target_type)) {
|
if (is_type_any(target_type)) {
|
||||||
target_type = t_untyped_nil;
|
// target_type = t_untyped_nil;
|
||||||
} else if (is_type_cstring(target_type)) {
|
} else if (is_type_cstring(target_type)) {
|
||||||
target_type = t_untyped_nil;
|
// target_type = t_untyped_nil;
|
||||||
} else if (!type_has_nil(target_type)) {
|
} else if (!type_has_nil(target_type)) {
|
||||||
operand->mode = Addressing_Invalid;
|
operand->mode = Addressing_Invalid;
|
||||||
convert_untyped_error(c, operand, target_type);
|
convert_untyped_error(c, operand, target_type);
|
||||||
@@ -3376,6 +3374,14 @@ void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_type_any(target_type) && is_type_untyped(operand->type)) {
|
||||||
|
if (is_type_untyped_nil(operand->type) && is_type_untyped_undef(operand->type)) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
target_type = default_type(operand->type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
update_untyped_expr_type(c, operand->expr, target_type, true);
|
update_untyped_expr_type(c, operand->expr, target_type, true);
|
||||||
operand->type = target_type;
|
operand->type = target_type;
|
||||||
@@ -6743,15 +6749,14 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
|
|||||||
return kind;
|
return kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type *type = x.type;
|
o->type = x.type;
|
||||||
if (is_type_untyped_nil(type) || is_type_untyped_undef(type)) {
|
if (is_type_untyped_nil(o->type) || is_type_untyped_undef(o->type)) {
|
||||||
type = y.type;
|
o->type = y.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
o->type = x.type;
|
|
||||||
o->mode = Addressing_Value;
|
o->mode = Addressing_Value;
|
||||||
o->expr = node;
|
o->expr = node;
|
||||||
if (type_hint != nullptr && is_type_untyped(type)) {
|
if (type_hint != nullptr && is_type_untyped(o->type)) {
|
||||||
if (check_cast_internal(c, &x, type_hint) &&
|
if (check_cast_internal(c, &x, type_hint) &&
|
||||||
check_cast_internal(c, &y, type_hint)) {
|
check_cast_internal(c, &y, type_hint)) {
|
||||||
convert_to_typed(c, o, type_hint);
|
convert_to_typed(c, o, type_hint);
|
||||||
|
|||||||
@@ -1728,8 +1728,6 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
|
|||||||
Entity *e = pt->results->Tuple.variables[i];
|
Entity *e = pt->results->Tuple.variables[i];
|
||||||
Operand *o = &operands[i];
|
Operand *o = &operands[i];
|
||||||
check_assignment(ctx, o, e->type, str_lit("return statement"));
|
check_assignment(ctx, o, e->type, str_lit("return statement"));
|
||||||
convert_to_typed(ctx, o, e->type);
|
|
||||||
|
|
||||||
if (is_type_untyped(o->type)) {
|
if (is_type_untyped(o->type)) {
|
||||||
update_untyped_expr_type(ctx, o->expr, e->type, true);
|
update_untyped_expr_type(ctx, o->expr, e->type, true);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-5
@@ -1111,10 +1111,7 @@ void check_set_expr_info(CheckerContext *c, Ast *expr, AddressingMode mode, Type
|
|||||||
void check_remove_expr_info(CheckerContext *c, Ast *e) {
|
void check_remove_expr_info(CheckerContext *c, Ast *e) {
|
||||||
if (c->untyped != nullptr) {
|
if (c->untyped != nullptr) {
|
||||||
map_remove(c->untyped, hash_pointer(e));
|
map_remove(c->untyped, hash_pointer(e));
|
||||||
if (map_get(c->untyped, hash_pointer(e)) != nullptr) {
|
GB_ASSERT(map_get(c->untyped, hash_pointer(e)) == nullptr);
|
||||||
map_remove(c->untyped, hash_pointer(e));
|
|
||||||
GB_ASSERT(map_get(c->untyped, hash_pointer(e)) == nullptr);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
auto *untyped = &c->info->global_untyped;
|
auto *untyped = &c->info->global_untyped;
|
||||||
mutex_lock(&c->info->global_untyped_mutex);
|
mutex_lock(&c->info->global_untyped_mutex);
|
||||||
@@ -1196,7 +1193,12 @@ void add_type_and_value(CheckerInfo *i, Ast *expr, AddressingMode mode, Type *ty
|
|||||||
while (prev_expr != expr) {
|
while (prev_expr != expr) {
|
||||||
prev_expr = expr;
|
prev_expr = expr;
|
||||||
expr->tav.mode = mode;
|
expr->tav.mode = mode;
|
||||||
expr->tav.type = type;
|
if (type != nullptr && expr->tav.type != nullptr &&
|
||||||
|
is_type_any(type) && is_type_untyped(expr->tav.type)) {
|
||||||
|
// ignore
|
||||||
|
} else {
|
||||||
|
expr->tav.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
if (mode == Addressing_Constant || mode == Addressing_Invalid) {
|
if (mode == Addressing_Constant || mode == Addressing_Invalid) {
|
||||||
expr->tav.value = value;
|
expr->tav.value = value;
|
||||||
|
|||||||
@@ -762,12 +762,6 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
|
|||||||
case Token_Xor:
|
case Token_Xor:
|
||||||
case Token_AndNot: {
|
case Token_AndNot: {
|
||||||
Type *type = default_type(tv.type);
|
Type *type = default_type(tv.type);
|
||||||
if (is_type_typed(be->left->tav.type) && is_type_untyped(be->right->tav.type)) {
|
|
||||||
be->right->tav.type = be->left->tav.type;
|
|
||||||
} else if (is_type_untyped(be->left->tav.type) && is_type_typed(be->right->tav.type)) {
|
|
||||||
be->left->tav.type = type_of_expr(be->right);
|
|
||||||
}
|
|
||||||
|
|
||||||
lbValue left = lb_build_expr(p, be->left);
|
lbValue left = lb_build_expr(p, be->left);
|
||||||
lbValue right = lb_build_expr(p, be->right);
|
lbValue right = lb_build_expr(p, be->right);
|
||||||
return lb_emit_arith(p, be->op.kind, left, right, type);
|
return lb_emit_arith(p, be->op.kind, left, right, type);
|
||||||
|
|||||||
Reference in New Issue
Block a user