zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 44ae978523f759382d5724fac1ffd83a2d0eda2c (tree)
parent 9393a83323bfdfa37cda703997f4e0faf1ac2732
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 21 Nov 2023 21:42:58 -0700

aro: avoid BoundedArray in text_literal

I consider this change to be neutral. It inlines a bit of logic that
previously was abstracted, however, it also eliminates an instance of
`catch unreachable` as well as a clumsy failed pop / append in favor of
writing directly to the appropriate array element.

Diffstat:
Mdeps/aro/aro/Parser.zig | 4++--
Mdeps/aro/aro/text_literal.zig | 24++++++++++++++++++------
2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/deps/aro/aro/Parser.zig b/deps/aro/aro/Parser.zig @@ -7796,7 +7796,7 @@ fn stringLiteral(p: *Parser) Error!Result { } }, }; - for (char_literal_parser.errors.constSlice()) |item| { + for (char_literal_parser.errors()) |item| { try p.errExtra(item.tag, p.tok_i, item.extra); } } @@ -7911,7 +7911,7 @@ fn charLiteral(p: *Parser) Error!Result { char_literal_parser.err(.char_lit_too_wide, .{ .none = {} }); } - for (char_literal_parser.errors.constSlice()) |item| { + for (char_literal_parser.errors()) |item| { try p.errExtra(item.tag, p.tok_i, item.extra); } } diff --git a/deps/aro/aro/text_literal.zig b/deps/aro/aro/text_literal.zig @@ -157,7 +157,8 @@ pub const Parser = struct { max_codepoint: u21, /// We only want to issue a max of 1 error per char literal errored: bool = false, - errors: std.BoundedArray(CharDiagnostic, 4) = .{}, + errors_buffer: [4]CharDiagnostic, + errors_len: usize, comp: *const Compilation, pub fn init(literal: []const u8, kind: Kind, max_codepoint: u21, comp: *const Compilation) Parser { @@ -166,6 +167,8 @@ pub const Parser = struct { .comp = comp, .kind = kind, .max_codepoint = max_codepoint, + .errors_buffer = undefined, + .errors_len = 0, }; } @@ -178,19 +181,28 @@ pub const Parser = struct { }; } + pub fn errors(p: *Parser) []CharDiagnostic { + return p.errors_buffer[0..p.errors_len]; + } + pub fn err(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void { if (self.errored) return; self.errored = true; const diagnostic = .{ .tag = tag, .extra = extra }; - self.errors.append(diagnostic) catch { - _ = self.errors.pop(); - self.errors.append(diagnostic) catch unreachable; - }; + if (self.errors_len == self.errors_buffer.len) { + self.errors_buffer[self.errors_buffer.len - 1] = diagnostic; + } else { + self.errors_buffer[self.errors_len] = diagnostic; + self.errors_len += 1; + } } pub fn warn(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void { if (self.errored) return; - self.errors.append(.{ .tag = tag, .extra = extra }) catch {}; + if (self.errors_len < self.errors_buffer.len) { + self.errors_buffer[self.errors_len] = .{ .tag = tag, .extra = extra }; + self.errors_len += 1; + } } pub fn next(self: *Parser) ?Item {