Commit dd402f6d83

mlugg <mlugg@mlugg.co.uk>
2023-10-20 01:56:20
AstGen: omit make_ptr_const for resolve_inferred_alloc
After the previous commit, these make_ptr_const ZIR instructions are redundant.
1 parent 5e1db5e
Changed files (2)
src/AstGen.zig
@@ -3123,10 +3123,10 @@ fn varDecl(
             if (nodeMayAppendToErrorTrace(tree, var_decl.ast.init_node))
                 _ = try gz.addSaveErrRetIndex(.{ .if_of_error_type = init_inst });
 
-            if (resolve_inferred_alloc != .none) {
+            const const_ptr = if (resolve_inferred_alloc != .none) p: {
                 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
-            }
-            const const_ptr = try gz.addUnNode(.make_ptr_const, var_ptr, node);
+                break :p var_ptr;
+            } else try gz.addUnNode(.make_ptr_const, var_ptr, node);
 
             try gz.addDbgVar(.dbg_var_ptr, ident_name, const_ptr);
 
@@ -3533,7 +3533,9 @@ fn assignDestructureMaybeDecls(
             else => unreachable,
         };
         // If the alloc was const, make it const.
-        const var_ptr = if (is_const) make_const: {
+        const var_ptr = if (is_const and full.ast.type_node != 0) make_const: {
+            // Note that we don't do this if type_node == 0 since `resolve_inferred_alloc`
+            // handles it for us.
             break :make_const try gz.addUnNode(.make_ptr_const, raw_ptr, node);
         } else raw_ptr;
         const name_token = full.ast.mut_token + 1;
src/Zir.zig
@@ -997,8 +997,9 @@ pub const Inst = struct {
         /// is the allocation that needs to have its type inferred.
         /// Uses the `un_node` field. The AST node is the var decl.
         resolve_inferred_alloc,
-        /// Turns a pointer coming from an `alloc`, `alloc_inferred`, `alloc_inferred_comptime` or
-        /// `Extended.alloc` into a constant version of the same pointer.
+        /// Turns a pointer coming from an `alloc` or `Extended.alloc` into a constant
+        /// version of the same pointer. For inferred allocations this is instead implicitly
+        /// handled by the `resolve_inferred_alloc` instruction.
         /// Uses the `un_node` union field.
         make_ptr_const,