added #auto typed local variables
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 5 Oct 2022 00:17:04 +0000 (19:17 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 5 Oct 2022 00:17:04 +0000 (19:17 -0500)
compiler/src/parser.c
compiler/src/wasm_emit.c

index 246c9582f21a9a4255e422691be0e478ed512764..8c878f61db36ebcb9d25dc8734727391ef572a40 100644 (file)
@@ -1341,7 +1341,13 @@ static i32 parse_possible_symbol_declaration(OnyxParser* parser, AstNode** ret)
 
     AstType* type_node = NULL;
     if (parser->curr->type != '=') {
-        type_node = parse_type(parser);
+        if (parse_possible_directive(parser, "auto")) {
+            // Do nothing here.
+            // This allows for "x: #auto" to declare an x that will automatically be
+            // typed on the first assignment.
+        } else {
+            type_node = parse_type(parser);
+        }
     }
 
     AstLocal* local = make_local(parser->allocator, symbol, type_node);
index 6a9b9d8c7df57b58b193cddd527c52bdffbcce84..6978c6ddcfc597e5556906dcb2ae3c0097c3eaa0 100644 (file)
@@ -709,6 +709,17 @@ EMIT_FUNC(statement, AstNode* stmt) {
 }
 
 EMIT_FUNC(local_allocation, AstTyped* stmt) {
+    //
+    // If the statement does not have a type, it should not
+    // be emitted. The only case this should be used by is
+    // when a local is declared with "#auto" type and is
+    // never used, therefore never declaring its type.
+    if (stmt->type == NULL) {
+        assert(stmt->kind == Ast_Kind_Local);
+        onyx_report_warning(stmt->token->pos, "Unused local variable with unassigned type.");
+        return;
+    }
+
     u64 local_idx = local_allocate(mod->local_alloc, stmt);
     bh_imap_put(&mod->local_map, (u64) stmt, local_idx);