changed: union tags start indexing at 0, not 1
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 16 Jun 2023 21:11:10 +0000 (16:11 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 16 Jun 2023 21:11:10 +0000 (16:11 -0500)
There were too many places were the fact the ZII was not followed for union types was too annoying.

compiler/src/astnodes.c
compiler/src/checker.c
compiler/src/types.c
compiler/src/wasm_emit.c

index 0d22b61e5f1b7df07d7f434eac1f3a5a44b23bd1..f8f43626301bb3aea5a0728a0acb3dd913cb4984 100644 (file)
@@ -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;
index e8847a11b3eebaf57dccdcf930aaeab8cd0a0e1f..44376efded40d6f43d94ec4e9edfc146a8507d93 100644 (file)
@@ -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];
index 027483bb88a07a6d95244b2115353877c5f1a5a1..073da57a66dae458d239234de4bf9a3d21e33b34 100644 (file)
@@ -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;
index ea9f945990135675cb57fc90238f78ebff8f60bf..13507e0317a0690391729fe157d7a66009efb09d 100644 (file)
@@ -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);