Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/std/zig/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,16 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
return stream.writeAll("for input is not captured");
},

.unterminated_literal => {
return stream.print("unterminated '{s} literal'", .{
switch (tree.source[tree.tokens.items(.start)[parse_error.token]]) {
'\'' => "character",
'"' => "string",
else => unreachable,
},
});
},

.invalid_byte => {
const tok_slice = tree.source[tree.tokens.items(.start)[parse_error.token]..];
return stream.print("{s} contains invalid byte: '{'}'", .{
Expand Down Expand Up @@ -3003,6 +3013,7 @@ pub const Error = struct {
var_const_decl,
extra_for_capture,
for_input_not_captured,
unterminated_literal,

zig_style_container,
previous_field,
Expand Down
28 changes: 21 additions & 7 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14039,15 +14039,29 @@ fn lowerAstErrors(astgen: *AstGen) !void {
}
break :blk idx - tok_start;
};

const err: Ast.Error = .{
.tag = Ast.Error.Tag.invalid_byte,
.token = tok,
.extra = .{ .offset = bad_off },
};
const err: Ast.Error =
if ((start_char == '\"' or start_char == '\'') and // If `tok` is a string or character literal and...
(tok_start + bad_off == tree.source.len or // it's directly followed by EOF, or...
tree.source[tok_start + bad_off] == '\n')) // it's terminated by a newline.
.{
.tag = Ast.Error.Tag.unterminated_literal,
.token = tok,
}
else
.{
.tag = Ast.Error.Tag.invalid_byte,
.token = tok,
.extra = .{ .offset = bad_off },
};
msg.clearRetainingCapacity();
try tree.renderError(err, msg.writer(gpa));
return try astgen.appendErrorTokNotesOff(tok, bad_off, "{s}", .{msg.items}, notes.items);
return try astgen.appendErrorTokNotesOff(
tok,
if (err.tag == .invalid_byte) bad_off else 0,
"{s}",
.{msg.items},
notes.items,
);
}

var cur_err = tree.errors[0];
Expand Down
8 changes: 8 additions & 0 deletions test/cases/compile_errors/character_literal_with_newline.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const foo = 'a
';

// error
// backend=stage2
// target=native
//
// :1:13: error: unterminated 'character literal'
2 changes: 1 addition & 1 deletion test/cases/compile_errors/normal_string_with_newline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ b";
// backend=stage2
// target=native
//
// :1:15: error: string literal contains invalid byte: '\n'
// :1:13: error: unterminated 'string literal'
16 changes: 16 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
});
}

{
const case = ctx.obj("unterminated character literal eof", b.graph.host);

case.addError("const c = '", &[_][]const u8{
":1:11: error: unterminated 'character literal'",
});
}

{
const case = ctx.obj("unterminated string literal eof", b.graph.host);

case.addError("const s = \"hello, ", &[_][]const u8{
":1:11: error: unterminated 'string literal'",
});
}

{
const case = ctx.obj("invalid byte at start of token", b.graph.host);

Expand Down
Loading