Skip to content

Commit 40d541c

Browse files
committed
fix: Correct error message for invalid bytes in multiline strings
1 parent 4d81e8e commit 40d541c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/std/zig/AstGen.zig

+33
Original file line numberDiff line numberDiff line change
@@ -13955,6 +13955,39 @@ fn lowerAstErrors(astgen: *AstGen) !void {
1395513955
var notes: std.ArrayListUnmanaged(u32) = .empty;
1395613956
defer notes.deinit(gpa);
1395713957

13958+
const token_starts = tree.tokens.items(.start);
13959+
const token_tags = tree.tokens.items(.tag);
13960+
const tok = parse_err.token + @intFromBool(parse_err.token_is_prev);
13961+
const tok_start = token_starts[tok];
13962+
13963+
if (token_tags[tok] == .invalid and tree.source[tok_start] == '\\') {
13964+
const bad_off = blk: {
13965+
const tok_len: u32 = @intCast(tree.tokenSlice(tok).len);
13966+
const tok_end = tok_start + tok_len;
13967+
var idx = tok_start;
13968+
while (idx < tok_end) : (idx += 1) {
13969+
switch (tree.source[idx]) {
13970+
0x00...0x09, 0x0b...0x1f, 0x7f => break,
13971+
else => {},
13972+
}
13973+
}
13974+
break :blk idx - tok_start;
13975+
};
13976+
13977+
const byte_abs = tok_start + bad_off;
13978+
try notes.append(gpa, try astgen.errNoteTokOff(tok, bad_off, "invalid byte: '{'}'", .{
13979+
std.zig.fmtEscapes(tree.source[byte_abs..][0..1]),
13980+
}));
13981+
const err: Ast.Error = .{
13982+
.tag = Ast.Error.Tag.expected_token,
13983+
.token = tok,
13984+
.extra = .{ .expected_tag = std.zig.Token.Tag.multiline_string_literal_line },
13985+
};
13986+
msg.clearRetainingCapacity();
13987+
try tree.renderError(err, msg.writer(gpa));
13988+
return try astgen.appendErrorTokNotes(tok, "{s}", .{msg.items}, notes.items);
13989+
}
13990+
1395813991
for (tree.errors[1..]) |note| {
1395913992
if (!note.is_note) break;
1396013993

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export fn entry() void {
2+
const foo =
3+
\\const S = struct {
4+
\\ // hello
5+
\\}
6+
;
7+
_ = foo;
8+
}
9+
// error
10+
// backend=stage2
11+
// target=native
12+
//
13+
// :4:9: error: expected 'a string literal', found invalid bytes
14+
// :4:11: note: invalid byte: '\t'

0 commit comments

Comments
 (0)