Commit 5aca3baea6

Lachlan Easton <lachlan@lakebythewoods.xyz>
2020-08-31 15:39:30
zig fmt: Remove dynamic stack from auto-indenting-stream
1 parent 50c8a53
lib/std/io/auto_indenting_stream.zig
@@ -1,37 +1,31 @@
 const std = @import("../std.zig");
 const io = std.io;
 const mem = std.mem;
-const Allocator = mem.Allocator;
-const ArrayList = std.ArrayList;
 const assert = std.debug.assert;
 
 /// Automatically inserts indentation of written data by keeping
 /// track of the current indentation level
-pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: type) type {
+pub fn AutoIndentingStream(comptime WriterType: type) type {
     return struct {
         const Self = @This();
         pub const Error = WriterType.Error;
-        pub const PushError = Allocator.Error;
         pub const Writer = io.Writer(*Self, Error, write);
-        const Stack = ArrayList(usize);
 
         writer_pointer: *WriterType,
-        indent_stack: Stack,
 
+        indent_stack: usize = 0,
+        indent_delta: usize,
         current_line_empty: bool = true,
         indent_one_shot_count: usize = 0, // automatically popped when applied
         applied_indent: usize = 0, // the most recently applied indent
         indent_next_line: usize = 0, // not used until the next line
 
-        pub fn init(writer_pointer: *WriterType, allocator: *Allocator) Self {
-            var indent_stack = Stack.init(allocator);
-            return Self{ .writer_pointer = writer_pointer, .indent_stack = indent_stack };
+        pub fn init(indent_delta: usize, writer_pointer: *WriterType) Self {
+            return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta };
         }
 
         /// Release all allocated memory.
-        pub fn deinit(self: Self) void {
-            self.indent_stack.deinit();
-        }
+        pub fn deinit(self: Self) void {}
 
         pub fn writer(self: *Self) Writer {
             return .{ .context = self };
@@ -71,21 +65,16 @@ pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: ty
         }
 
         /// Push default indentation
-        pub fn pushIndent(self: *Self) PushError!void {
+        pub fn pushIndent(self: *Self) void {
             // Doesn't actually write any indentation.
             // Just primes the stream to be able to write the correct indentation if it needs to.
-            try self.pushIndentN(indent_delta);
-        }
-
-        /// Push an indent of arbitrary width
-        pub fn pushIndentN(self: *Self, n: usize) PushError!void {
-            try self.indent_stack.append(n);
+            self.indent_stack += 1;
         }
 
         /// Push an indent that is automatically popped after being applied
-        pub fn pushIndentOneShot(self: *Self) PushError!void {
+        pub fn pushIndentOneShot(self: *Self) void {
             self.indent_one_shot_count += 1;
-            try self.pushIndent();
+            self.pushIndent();
         }
 
         /// Turns all one-shot indents into regular indents
@@ -97,15 +86,15 @@ pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: ty
         }
 
         /// Push an indent that should not take effect until the next line
-        pub fn pushIndentNextLine(self: *Self) PushError!void {
+        pub fn pushIndentNextLine(self: *Self) void {
             self.indent_next_line += 1;
-            try self.pushIndent();
+            self.pushIndent();
         }
 
         pub fn popIndent(self: *Self) void {
-            assert(self.indent_stack.items.len != 0);
-            self.indent_stack.items.len -= 1;
-            self.indent_next_line = std.math.min(self.indent_stack.items.len, self.indent_next_line); // Tentative indent may have been popped before there was a newline
+            assert(self.indent_stack != 0);
+            self.indent_stack -= 1;
+            self.indent_next_line = std.math.min(self.indent_stack, self.indent_next_line); // Tentative indent may have been popped before there was a newline
         }
 
         /// Writes ' ' bytes if the current line is empty
@@ -116,7 +105,7 @@ pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: ty
                 self.applied_indent = current_indent;
             }
 
-            self.indent_stack.items.len -= self.indent_one_shot_count;
+            self.indent_stack -= self.indent_one_shot_count;
             self.indent_one_shot_count = 0;
             self.current_line_empty = false;
         }
@@ -129,11 +118,9 @@ pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: ty
 
         fn currentIndent(self: *Self) usize {
             var indent_current: usize = 0;
-            if (self.indent_stack.items.len > 0) {
-                const stack_top = self.indent_stack.items.len - self.indent_next_line;
-                for (self.indent_stack.items[0..stack_top]) |indent| {
-                    indent_current += indent;
-                }
+            if (self.indent_stack > 0) {
+                const stack_top = self.indent_stack - self.indent_next_line;
+                indent_current = stack_top * self.indent_delta;
             }
             return indent_current;
         }
@@ -141,10 +128,9 @@ pub fn AutoIndentingStream(comptime indent_delta: usize, comptime WriterType: ty
 }
 
 pub fn autoIndentingStream(
-    comptime indent_delta: usize,
+    indent_delta: usize,
     underlying_stream: anytype,
-    allocator: *Allocator,
-) AutoIndentingStream(indent_delta, @TypeOf(underlying_stream).Child) {
+) AutoIndentingStream(@TypeOf(underlying_stream).Child) {
     comptime assert(@typeInfo(@TypeOf(underlying_stream)) == .Pointer);
-    return AutoIndentingStream(indent_delta, @TypeOf(underlying_stream).Child).init(underlying_stream, allocator);
+    return AutoIndentingStream(@TypeOf(underlying_stream).Child).init(indent_delta, underlying_stream);
 }
lib/std/io/change_detection_stream.zig
@@ -11,11 +11,11 @@ pub fn ChangeDetectionStream(comptime WriterType: type) type {
         pub const Writer = io.Writer(*Self, Error, write);
 
         anything_changed: bool = false,
-        writer_pointer: *WriterType,
+        writer_pointer: *const WriterType,
         source_index: usize,
         source: []const u8,
 
-        pub fn init(source: []const u8, writer_pointer: *WriterType) Self {
+        pub fn init(source: []const u8, writer_pointer: *const WriterType) Self {
             return Self{
                 .writer_pointer = writer_pointer,
                 .source_index = 0,
lib/std/io/find_byte_out_stream.zig
@@ -10,11 +10,11 @@ pub fn FindByteOutStream(comptime WriterType: type) type {
         pub const Error = WriterType.Error;
         pub const Writer = io.Writer(*Self, Error, write);
 
-        writer_pointer: *WriterType,
+        writer_pointer: *const WriterType,
         byte_found: bool,
         byte: u8,
 
-        pub fn init(byte: u8, writer_pointer: *WriterType) Self {
+        pub fn init(byte: u8, writer_pointer: *const WriterType) Self {
             return Self{
                 .writer_pointer = writer_pointer,
                 .byte = byte,
lib/std/zig/parser_test.zig
@@ -2827,7 +2827,7 @@ test "zig fmt: inline asm" {
         \\    return asm volatile ("syscall"
         \\        : [ret] "={rax}" (-> usize)
         \\        : [number] "{rax}" (number),
-        \\          [arg1] "{rdi}" (arg1)
+        \\            [arg1] "{rdi}" (arg1)
         \\        : "rcx", "r11"
         \\    );
         \\}
@@ -2930,14 +2930,14 @@ test "zig fmt: inline asm parameter alignment" {
         \\        \\ foo
         \\        \\ bar
         \\        : [_] "" (-> usize),
-        \\          [_] "" (-> usize)
+        \\            [_] "" (-> usize)
         \\    );
         \\    asm volatile (
         \\        \\ foo
         \\        \\ bar
         \\        :
         \\        : [_] "" (0),
-        \\          [_] "" (0)
+        \\            [_] "" (0)
         \\    );
         \\    asm volatile (
         \\        \\ foo
@@ -2950,9 +2950,9 @@ test "zig fmt: inline asm parameter alignment" {
         \\        \\ foo
         \\        \\ bar
         \\        : [_] "" (-> usize),
-        \\          [_] "" (-> usize)
+        \\            [_] "" (-> usize)
         \\        : [_] "" (0),
-        \\          [_] "" (0)
+        \\            [_] "" (0)
         \\        : "", ""
         \\    );
         \\}
lib/std/zig/render.zig
@@ -24,7 +24,7 @@ pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (meta
 
     var s = stream.*;
     var change_detection_stream = std.io.changeDetectionStream(tree.source, &s);
-    var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream, allocator);
+    var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream);
     defer auto_indenting_stream.deinit();
 
     try renderRoot(allocator, &auto_indenting_stream, tree);
@@ -389,11 +389,11 @@ fn renderExpression(
             }
 
             if (block.statements.len == 0) {
-                try stream.pushIndentNextLine();
+                stream.pushIndentNextLine();
                 defer stream.popIndent();
                 try renderToken(tree, stream, block.lbrace, Space.None);
             } else {
-                try stream.pushIndentNextLine();
+                stream.pushIndentNextLine();
                 defer stream.popIndent();
 
                 try renderToken(tree, stream, block.lbrace, Space.Newline);
@@ -463,7 +463,7 @@ fn renderExpression(
                 try renderExpression(allocator, stream, tree, payload, Space.Space);
             }
 
-            try stream.pushIndentOneShot();
+            stream.pushIndentOneShot();
             return renderExpression(allocator, stream, tree, infix_op_node.rhs, space);
         },
 
@@ -524,7 +524,7 @@ fn renderExpression(
             };
 
             try renderToken(tree, stream, infix_op_node.op_token, after_op_space);
-            try stream.pushIndentOneShot();
+            stream.pushIndentOneShot();
             return renderExpression(allocator, stream, tree, infix_op_node.rhs, space);
         },
 
@@ -718,7 +718,7 @@ fn renderExpression(
                 }
 
                 {
-                    try stream.pushIndent();
+                    stream.pushIndent();
                     defer stream.popIndent();
                     try renderToken(tree, stream, lbrace, Space.None);
                 }
@@ -783,7 +783,7 @@ fn renderExpression(
 
                 // Null stream for counting the printed length of each expression
                 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
-                var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream, allocator);
+                var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream);
                 defer auto_indenting_stream.deinit();
 
                 for (exprs) |expr, i| {
@@ -796,7 +796,7 @@ fn renderExpression(
                 }
 
                 {
-                    try stream.pushIndentNextLine();
+                    stream.pushIndentNextLine();
                     defer stream.popIndent();
                     try renderToken(tree, stream, lbrace, Space.Newline);
 
@@ -880,7 +880,7 @@ fn renderExpression(
                 }
 
                 {
-                    try stream.pushIndentNextLine();
+                    stream.pushIndentNextLine();
                     defer stream.popIndent();
                     try renderToken(tree, stream, lbrace, Space.None);
                 }
@@ -902,7 +902,7 @@ fn renderExpression(
                 // render field expressions until a LF is found
                 for (field_inits) |field_init| {
                     var find_stream = std.io.findByteOutStream('\n', &std.io.null_out_stream);
-                    var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream, allocator);
+                    var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream);
                     defer auto_indenting_stream.deinit();
 
                     try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None);
@@ -963,7 +963,7 @@ fn renderExpression(
                     .node => |node| try renderExpression(allocator, stream, tree, node, Space.None),
                 }
 
-                try stream.pushIndentNextLine();
+                stream.pushIndentNextLine();
                 defer stream.popIndent();
 
                 try renderToken(tree, stream, lbrace, Space.Newline);
@@ -1011,7 +1011,7 @@ fn renderExpression(
 
                 const params = call.params();
                 for (params) |param_node, i| {
-                    try stream.pushIndent();
+                    stream.pushIndent();
                     defer stream.popIndent();
 
                     if (i + 1 < params.len) {
@@ -1031,7 +1031,7 @@ fn renderExpression(
 
             const params = call.params();
             for (params) |param_node, i| {
-                if (param_node.*.tag == .MultilineStringLiteral) try stream.pushIndentOneShot();
+                if (param_node.*.tag == .MultilineStringLiteral) stream.pushIndentOneShot();
 
                 try renderExpression(allocator, stream, tree, param_node, Space.None);
 
@@ -1058,7 +1058,7 @@ fn renderExpression(
             {
                 const new_space = if (ends_with_comment) Space.Newline else Space.None;
 
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
                 try renderExpression(allocator, stream, tree, suffix_op.index_expr, new_space);
             }
@@ -1194,7 +1194,7 @@ fn renderExpression(
 
             try renderToken(tree, stream, grouped_expr.lparen, Space.None);
             {
-                try stream.pushIndentOneShot();
+                stream.pushIndentOneShot();
                 try renderExpression(allocator, stream, tree, grouped_expr.expr, Space.None);
             }
             return renderToken(tree, stream, grouped_expr.rparen, space);
@@ -1254,7 +1254,7 @@ fn renderExpression(
 
             if (container_decl.fields_and_decls_len == 0) {
                 {
-                    try stream.pushIndentNextLine();
+                    stream.pushIndentNextLine();
                     defer stream.popIndent();
                     try renderToken(tree, stream, container_decl.lbrace_token, Space.None); // {
                 }
@@ -1289,7 +1289,7 @@ fn renderExpression(
 
             if (src_has_trailing_comma or !src_has_only_fields) {
                 // One declaration per line
-                try stream.pushIndentNextLine();
+                stream.pushIndentNextLine();
                 defer stream.popIndent();
                 try renderToken(tree, stream, container_decl.lbrace_token, .Newline); // {
 
@@ -1305,7 +1305,7 @@ fn renderExpression(
                 // their own line
                 try renderToken(tree, stream, container_decl.lbrace_token, .Newline); // {
 
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
 
                 for (fields_and_decls) |decl, i| {
@@ -1361,7 +1361,7 @@ fn renderExpression(
 
             if (src_has_trailing_comma) {
                 {
-                    try stream.pushIndent();
+                    stream.pushIndent();
                     defer stream.popIndent();
 
                     try renderToken(tree, stream, lbrace, Space.Newline); // {
@@ -1451,7 +1451,7 @@ fn renderExpression(
                 }
             } else {
                 // one param per line
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
                 try renderToken(tree, stream, lparen, Space.Newline); // (
 
@@ -1530,7 +1530,7 @@ fn renderExpression(
                 }
             } else {
                 // one param per line
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
                 try renderToken(tree, stream, lparen, Space.Newline); // (
 
@@ -1627,7 +1627,7 @@ fn renderExpression(
             try renderToken(tree, stream, rparen, Space.Space); // )
 
             {
-                try stream.pushIndentNextLine();
+                stream.pushIndentNextLine();
                 defer stream.popIndent();
                 try renderToken(tree, stream, lbrace, Space.Newline); // {
 
@@ -1711,7 +1711,7 @@ fn renderExpression(
             if (same_line) {
                 return renderExpression(allocator, stream, tree, else_node.body, space);
             } else {
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
                 return renderExpression(allocator, stream, tree, else_node.body, space);
             }
@@ -1775,7 +1775,7 @@ fn renderExpression(
             }
 
             {
-                if (!body_is_block) try stream.pushIndent();
+                if (!body_is_block) stream.pushIndent();
                 defer if (!body_is_block) stream.popIndent();
                 try renderExpression(allocator, stream, tree, while_node.body, after_body_space);
             }
@@ -1826,7 +1826,7 @@ fn renderExpression(
             };
 
             {
-                if (!body_on_same_line) try stream.pushIndent();
+                if (!body_on_same_line) stream.pushIndent();
                 defer if (!body_on_same_line) stream.popIndent();
                 try renderExpression(allocator, stream, tree, for_node.body, space_after_body); // { body }
             }
@@ -1882,7 +1882,7 @@ fn renderExpression(
                     const else_is_block = nodeIsBlock(@"else".body);
 
                     {
-                        try stream.pushIndent();
+                        stream.pushIndent();
                         defer stream.popIndent();
                         try renderExpression(allocator, stream, tree, if_node.body, Space.Newline);
                     }
@@ -1903,12 +1903,12 @@ fn renderExpression(
                             try renderExpression(allocator, stream, tree, payload, Space.Newline);
                         }
 
-                        try stream.pushIndent();
+                        stream.pushIndent();
                         defer stream.popIndent();
                         return renderExpression(allocator, stream, tree, @"else".body, space);
                     }
                 } else {
-                    try stream.pushIndent();
+                    stream.pushIndent();
                     defer stream.popIndent();
                     return renderExpression(allocator, stream, tree, if_node.body, space);
                 }
@@ -1949,7 +1949,7 @@ fn renderExpression(
             }
 
             asmblk: {
-                try stream.pushIndent();
+                stream.pushIndent();
                 defer stream.popIndent();
 
                 if (asm_node.outputs.len == 0 and asm_node.inputs.len == 0 and asm_node.clobbers.len == 0) {
@@ -1968,7 +1968,7 @@ fn renderExpression(
                 } else blk: {
                     try renderToken(tree, stream, colon1, Space.Space); // :
 
-                    try stream.pushIndentN(2);
+                    stream.pushIndent();
                     defer stream.popIndent();
 
                     for (asm_node.outputs) |*asm_output, i| {
@@ -1999,7 +1999,7 @@ fn renderExpression(
                     break :blk tree.nextToken(colon2);
                 } else blk: {
                     try renderToken(tree, stream, colon2, Space.Space); // :
-                    try stream.pushIndentN(2);
+                    stream.pushIndent();
                     defer stream.popIndent();
                     for (asm_node.inputs) |*asm_input, i| {
                         if (i + 1 < asm_node.inputs.len) {
@@ -2025,7 +2025,7 @@ fn renderExpression(
                 };
 
                 try renderToken(tree, stream, colon3, Space.Space); // :
-                try stream.pushIndentN(2);
+                stream.pushIndent();
                 defer stream.popIndent();
                 for (asm_node.clobbers) |clobber_node, i| {
                     if (i + 1 >= asm_node.clobbers.len) {
@@ -2078,7 +2078,7 @@ fn renderArrayType(
     const new_space = if (ends_with_comment) Space.Newline else Space.None;
     {
         const do_indent = (starts_with_comment or ends_with_comment);
-        if (do_indent) try stream.pushIndent();
+        if (do_indent) stream.pushIndent();
         defer if (do_indent) stream.popIndent();
 
         try renderToken(tree, stream, lbracket, Space.None); // [
@@ -2212,7 +2212,7 @@ fn renderVarDecl(
     if (var_decl.getTrailer("init_node")) |init_node| {
         const s = if (init_node.tag == .MultilineStringLiteral) Space.None else Space.Space;
         try renderToken(tree, stream, var_decl.getTrailer("eq_token").?, s); // =
-        try stream.pushIndentOneShot();
+        stream.pushIndentOneShot();
         try renderExpression(allocator, stream, tree, init_node, Space.None);
     }
 
lib/std/io.zig
@@ -191,10 +191,10 @@ pub const BufferedAtomicFile = @import("io/buffered_atomic_file.zig").BufferedAt
 pub const StreamSource = @import("io/stream_source.zig").StreamSource;
 
 /// A Writer that doesn't write to anything.
-pub var null_writer = @as(NullWriter, .{ .context = {} });
+pub const null_writer = @as(NullWriter, .{ .context = {} });
 
 /// Deprecated: use `null_writer`
-pub var null_out_stream = null_writer;
+pub const null_out_stream = null_writer;
 
 const NullWriter = Writer(void, error{}, dummyWrite);
 /// Deprecated: use NullWriter
lib/std/target.zig
@@ -101,7 +101,7 @@ pub const Target = struct {
 
             /// Latest Windows version that the Zig Standard Library is aware of
             pub const latest = WindowsVersion.win10_20h1;
-            
+
             pub const Range = struct {
                 min: WindowsVersion,
                 max: WindowsVersion,