Skip to content

Commit

Permalink
fix: Correct error message for invalid bytes in multiline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis committed Sep 20, 2024
1 parent 4d81e8e commit 40d541c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13955,6 +13955,39 @@ fn lowerAstErrors(astgen: *AstGen) !void {
var notes: std.ArrayListUnmanaged(u32) = .empty;
defer notes.deinit(gpa);

const token_starts = tree.tokens.items(.start);
const token_tags = tree.tokens.items(.tag);
const tok = parse_err.token + @intFromBool(parse_err.token_is_prev);
const tok_start = token_starts[tok];

if (token_tags[tok] == .invalid and tree.source[tok_start] == '\\') {
const bad_off = blk: {
const tok_len: u32 = @intCast(tree.tokenSlice(tok).len);
const tok_end = tok_start + tok_len;
var idx = tok_start;
while (idx < tok_end) : (idx += 1) {
switch (tree.source[idx]) {
0x00...0x09, 0x0b...0x1f, 0x7f => break,
else => {},
}
}
break :blk idx - tok_start;
};

const byte_abs = tok_start + bad_off;
try notes.append(gpa, try astgen.errNoteTokOff(tok, bad_off, "invalid byte: '{'}'", .{
std.zig.fmtEscapes(tree.source[byte_abs..][0..1]),
}));
const err: Ast.Error = .{
.tag = Ast.Error.Tag.expected_token,
.token = tok,
.extra = .{ .expected_tag = std.zig.Token.Tag.multiline_string_literal_line },
};
msg.clearRetainingCapacity();
try tree.renderError(err, msg.writer(gpa));
return try astgen.appendErrorTokNotes(tok, "{s}", .{msg.items}, notes.items);
}

for (tree.errors[1..]) |note| {
if (!note.is_note) break;

Expand Down
14 changes: 14 additions & 0 deletions test/cases/compile_errors/tab_inside_multiline_string.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export fn entry() void {
const foo =
\\const S = struct {
\\ // hello
\\}
;
_ = foo;
}
// error
// backend=stage2
// target=native
//
// :4:9: error: expected 'a string literal', found invalid bytes
// :4:11: note: invalid byte: '\t'

0 comments on commit 40d541c

Please sign in to comment.