Skip to content

Commit 1cb5cbe

Browse files
committed
fix: correct error message for invalid bytes in comments
1 parent cfcf644 commit 1cb5cbe

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

lib/std/zig/AstGen.zig

+14-4
Original file line numberDiff line numberDiff line change
@@ -13987,10 +13987,10 @@ fn lowerAstErrors(astgen: *AstGen) !void {
1398713987
const tok = parse_err.token + @intFromBool(parse_err.token_is_prev);
1398813988
const tok_start = token_starts[tok];
1398913989

13990-
if (token_tags[tok] == .invalid and tree.source[tok_start] == '\\') {
13990+
if (token_tags[tok] == .invalid and (tree.source[tok_start] == '\\' or tree.source[tok_start] == '/')) {
13991+
const tok_len: u32 = @intCast(tree.tokenSlice(tok).len);
13992+
const tok_end = tok_start + tok_len;
1399113993
const bad_off = blk: {
13992-
const tok_len: u32 = @intCast(tree.tokenSlice(tok).len);
13993-
const tok_end = tok_start + tok_len;
1399413994
var idx = tok_start;
1399513995
while (idx < tok_end) : (idx += 1) {
1399613996
switch (tree.source[idx]) {
@@ -14005,10 +14005,20 @@ fn lowerAstErrors(astgen: *AstGen) !void {
1400514005
try notes.append(gpa, try astgen.errNoteTokOff(tok, bad_off, "invalid byte: '{'}'", .{
1400614006
std.zig.fmtEscapes(tree.source[byte_abs..][0..1]),
1400714007
}));
14008+
const expected_tag = switch (tree.source[tok_start]) {
14009+
'\\' => std.zig.Token.Tag.multiline_string_literal_line,
14010+
'/' => blk: {
14011+
if (std.mem.indexOf(u8, tree.source[tok_start..tok_end], "///")) |idx| {
14012+
if (idx == 0) break :blk std.zig.Token.Tag.doc_comment;
14013+
}
14014+
break :blk std.zig.Token.Tag.comment;
14015+
},
14016+
else => unreachable,
14017+
};
1400814018
const err: Ast.Error = .{
1400914019
.tag = Ast.Error.Tag.expected_token,
1401014020
.token = tok,
14011-
.extra = .{ .expected_tag = std.zig.Token.Tag.multiline_string_literal_line },
14021+
.extra = .{ .expected_tag = expected_tag },
1401214022
};
1401314023
msg.clearRetainingCapacity();
1401414024
try tree.renderError(err, msg.writer(gpa));

lib/std/zig/tokenizer.zig

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub const Token = struct {
137137
angle_bracket_angle_bracket_right_equal,
138138
tilde,
139139
number_literal,
140+
comment,
140141
doc_comment,
141142
container_doc_comment,
142143
keyword_addrspace,
@@ -199,6 +200,7 @@ pub const Token = struct {
199200
.eof,
200201
.builtin,
201202
.number_literal,
203+
.comment,
202204
.doc_comment,
203205
.container_doc_comment,
204206
=> null,
@@ -327,6 +329,7 @@ pub const Token = struct {
327329
.eof => "EOF",
328330
.builtin => "a builtin function",
329331
.number_literal => "a number literal",
332+
.comment => "a comment",
330333
.doc_comment, .container_doc_comment => "a document comment",
331334
else => unreachable,
332335
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Some comment
2+
export fn entry() void {}
3+
4+
// error
5+
// backend=stage2
6+
// target=native
7+
//
8+
// :1:1: error: expected 'a comment', found invalid bytes
9+
// :1:8: note: invalid byte: '\t'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// Some doc comment
2+
export fn entry() void {}
3+
4+
// error
5+
// backend=stage2
6+
// target=native
7+
//
8+
// :1:1: error: expected 'a document comment', found invalid bytes
9+
// :1:13: note: invalid byte: '\t'

test/compile_errors.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
225225
const case = ctx.obj("invalid byte in comment", b.graph.host);
226226

227227
case.addError("//\x01Q", &[_][]const u8{
228-
":1:1: error: expected type expression, found 'invalid token'",
228+
":1:1: error: expected 'a comment', found invalid bytes",
229+
":1:3: note: invalid byte: '\\x01'"
229230
});
230231
}
231232

0 commit comments

Comments
 (0)