From: Brendan Hansen Date: Fri, 16 Jun 2023 21:11:10 +0000 (-0500) Subject: changed: union tags start indexing at 0, not 1 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ec14aaf3498e3ef4df13e1661be02381203ce3e9;p=onyx.git changed: union tags start indexing at 0, not 1 There were too many places were the fact the ZII was not followed for union types was too annoying. --- diff --git a/compiler/src/astnodes.c b/compiler/src/astnodes.c index 0d22b61e..f8f43626 100644 --- a/compiler/src/astnodes.c +++ b/compiler/src/astnodes.c @@ -763,6 +763,14 @@ TypeMatch unify_node_and_type_(AstTyped** pnode, Type* type, b32 permanent) { return legal ? TYPE_MATCH_SUCCESS : TYPE_MATCH_FAILED; } + if (node->kind == Ast_Kind_Zero_Value) { + if (node_type == NULL) { + node->type = type; + return TYPE_MATCH_SUCCESS; // Shouldn't this be on the next line? And have node_type == node->type checked? + } + } + + // If the destination type is an optional, and the node's type is a value of // the same underlying type, then we can construct an optional with a value // implicitly. This makes working with optionals barable. @@ -847,13 +855,6 @@ TypeMatch unify_node_and_type_(AstTyped** pnode, Type* type, b32 permanent) { } } - if (node->kind == Ast_Kind_Zero_Value) { - if (node_type == NULL) { - node->type = type; - return TYPE_MATCH_SUCCESS; // Shouldn't this be on the next line? And have node_type == node->type checked? - } - } - // // This is a SUPER RARE case. It is used when checking to // see if the return value from a interface constraint is @@ -1602,7 +1603,7 @@ AstStructLiteral* make_optional_literal_some(bh_allocator a, AstTyped *expr, Typ arguments_initialize(&opt_lit->args); arguments_ensure_length(&opt_lit->args, 2); - opt_lit->args.values[0] = (AstTyped *) make_int_literal(a, 2); + opt_lit->args.values[0] = (AstTyped *) make_int_literal(a, 1); // 1 is Some opt_lit->args.values[1] = expr; opt_lit->type = opt_type; diff --git a/compiler/src/checker.c b/compiler/src/checker.c index e8847a11..44376efd 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -542,7 +542,8 @@ CheckStatus check_switch(AstSwitch* switchnode) { // We subtract one here because variant numbering starts at 1, instead of 0. // This is so a zeroed out block of memory does not have a valid variant. - i32 variant_number = get_expression_integer_value(*value, NULL) - 1; + // This is going to change now... + i32 variant_number = get_expression_integer_value(*value, NULL); switchnode->union_variants_handled[variant_number] = 1; UnionVariant *union_variant = union_expr_type->Union.variants_ordered[variant_number]; diff --git a/compiler/src/types.c b/compiler/src/types.c index 027483bb..073da57a 100644 --- a/compiler/src/types.c +++ b/compiler/src/types.c @@ -752,7 +752,7 @@ static Type* type_build_from_ast_inner(bh_allocator alloc, AstType* type_node, b u32 size = 0; u32 alignment = 0; - u32 next_tag_value = 1; + u32 next_tag_value = 0; AstEnumType* tag_enum_node = onyx_ast_node_new(alloc, sizeof(AstEnumType), Ast_Kind_Enum_Type); tag_enum_node->token = union_->token; diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index ea9f9459..13507e03 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -3579,7 +3579,7 @@ EMIT_FUNC(expression, AstTyped* expr) { WI(NULL, WI_I32_EQ); emit_enter_structured_block(mod, &code, SBT_Basic_If, field->token); emit_stack_address(mod, &code, intermediate_local, field->token); - WIL(NULL, WI_I32_CONST, 2); // 2 is Some + WIL(NULL, WI_I32_CONST, 1); // 1 is Some emit_store_instruction(mod, &code, &basic_types[Basic_Kind_I32], 0); emit_stack_address(mod, &code, intermediate_local + type_alignment_of(field->type), field->token); @@ -3591,7 +3591,7 @@ EMIT_FUNC(expression, AstTyped* expr) { WI(NULL, WI_ELSE); emit_stack_address(mod, &code, intermediate_local, field->token); - WIL(NULL, WI_I32_CONST, 1); // 1 is None + WIL(NULL, WI_I32_CONST, 0); // 0 is None emit_store_instruction(mod, &code, &basic_types[Basic_Kind_I32], 0); emit_leave_structured_block(mod, &code);