Skip to content

Commit de8b7ef

Browse files
committed
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.
1 parent c0c3614 commit de8b7ef

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

deps/aro/aro/Parser.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

0 commit comments

Comments
 (0)