authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-21 21:42:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-22 11:33:34-07:00
log44ae978523f759382d5724fac1ffd83a2d0eda2c
tree72ce6f201c97e811685cfaaaded07a7f5b616a90
parent9393a83323bfdfa37cda703997f4e0faf1ac2732

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.

2 files changed, 20 insertions(+), 8 deletions(-)

deps/aro/aro/Parser.zig+2-2
......@@ -7796,7 +7796,7 @@ fn stringLiteral(p: *Parser) Error!Result {
77967796 }
77977797 },
77987798 };
7799 for (char_literal_parser.errors.constSlice()) |item| {
7799 for (char_literal_parser.errors()) |item| {
78007800 try p.errExtra(item.tag, p.tok_i, item.extra);
78017801 }
78027802 }
......@@ -7911,7 +7911,7 @@ fn charLiteral(p: *Parser) Error!Result {
79117911 char_literal_parser.err(.char_lit_too_wide, .{ .none = {} });
79127912 }
79137913
7914 for (char_literal_parser.errors.constSlice()) |item| {
7914 for (char_literal_parser.errors()) |item| {
79157915 try p.errExtra(item.tag, p.tok_i, item.extra);
79167916 }
79177917 }
deps/aro/aro/text_literal.zig+18-6
......@@ -157,7 +157,8 @@ pub const Parser = struct {
157157 max_codepoint: u21,
158158 /// We only want to issue a max of 1 error per char literal
159159 errored: bool = false,
160 errors: std.BoundedArray(CharDiagnostic, 4) = .{},
160 errors_buffer: [4]CharDiagnostic,
161 errors_len: usize,
161162 comp: *const Compilation,
162163
163164 pub fn init(literal: []const u8, kind: Kind, max_codepoint: u21, comp: *const Compilation) Parser {
......@@ -166,6 +167,8 @@ pub const Parser = struct {
166167 .comp = comp,
167168 .kind = kind,
168169 .max_codepoint = max_codepoint,
170 .errors_buffer = undefined,
171 .errors_len = 0,
169172 };
170173 }
171174
......@@ -178,19 +181,28 @@ pub const Parser = struct {
178181 };
179182 }
180183
184 pub fn errors(p: *Parser) []CharDiagnostic {
185 return p.errors_buffer[0..p.errors_len];
186 }
187
181188 pub fn err(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void {
182189 if (self.errored) return;
183190 self.errored = true;
184191 const diagnostic = .{ .tag = tag, .extra = extra };
185 self.errors.append(diagnostic) catch {
186 _ = self.errors.pop();
187 self.errors.append(diagnostic) catch unreachable;
188 };
192 if (self.errors_len == self.errors_buffer.len) {
193 self.errors_buffer[self.errors_buffer.len - 1] = diagnostic;
194 } else {
195 self.errors_buffer[self.errors_len] = diagnostic;
196 self.errors_len += 1;
197 }
189198 }
190199
191200 pub fn warn(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void {
192201 if (self.errored) return;
193 self.errors.append(.{ .tag = tag, .extra = extra }) catch {};
202 if (self.errors_len < self.errors_buffer.len) {
203 self.errors_buffer[self.errors_len] = .{ .tag = tag, .extra = extra };
204 self.errors_len += 1;
205 }
194206 }
195207
196208 pub fn next(self: *Parser) ?Item {