authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-17 22:14:45+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-17 22:16:26+02:00
log6b65590715d0871c11635fc49cb1fc471a60ea59
treef0de6e9c50c86a48df5656efb6ae963a8765d2ed
parent92f276781417d7e710081470d97606e26cf764d6

parser: add notes to decl_between_fields error


6 files changed, 131 insertions(+), 61 deletions(-)

lib/std/zig/Ast.zig+11
......@@ -329,6 +329,13 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
329329 return stream.writeAll("expected field initializer");
330330 },
331331
332 .previous_field => {
333 return stream.writeAll("field before declarations here");
334 },
335 .next_field => {
336 return stream.writeAll("field after declarations here");
337 },
338
332339 .expected_token => {
333340 const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
334341 const expected_symbol = parse_error.extra.expected_tag.symbol();
......@@ -2470,6 +2477,7 @@ pub const full = struct {
24702477
24712478pub const Error = struct {
24722479 tag: Tag,
2480 is_note: bool = false,
24732481 /// True if `token` points to the token before the token causing an issue.
24742482 token_is_prev: bool = false,
24752483 token: TokenIndex,
......@@ -2527,6 +2535,9 @@ pub const Error = struct {
25272535 expected_comma_after_switch_prong,
25282536 expected_initializer,
25292537
2538 previous_field,
2539 next_field,
2540
25302541 /// `expected_tag` is populated.
25312542 expected_token,
25322543 };
lib/std/zig/parse.zig+25
......@@ -91,6 +91,9 @@ const Parser = struct {
9191 extra_data: std.ArrayListUnmanaged(Node.Index),
9292 scratch: std.ArrayListUnmanaged(Node.Index),
9393
94 /// Used for the error note of decl_between_fields error.
95 last_field: TokenIndex = undefined,
96
9497 const SmallSpan = union(enum) {
9598 zero_or_one: Node.Index,
9699 multi: Node.SubRange,
......@@ -270,6 +273,8 @@ const Parser = struct {
270273 .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {
271274 .identifier => {
272275 p.tok_i += 1;
276 const identifier = p.tok_i;
277 defer p.last_field = identifier;
273278 const container_field = try p.expectContainerFieldRecoverable();
274279 if (container_field != 0) {
275280 switch (field_state) {
......@@ -280,6 +285,16 @@ const Parser = struct {
280285 .tag = .decl_between_fields,
281286 .token = p.nodes.items(.main_token)[node],
282287 });
288 try p.warnMsg(.{
289 .tag = .previous_field,
290 .is_note = true,
291 .token = p.last_field,
292 });
293 try p.warnMsg(.{
294 .tag = .next_field,
295 .is_note = true,
296 .token = identifier,
297 });
283298 // Continue parsing; error will be reported later.
284299 field_state = .err;
285300 },
......@@ -373,6 +388,8 @@ const Parser = struct {
373388 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
374389 },
375390 .identifier => {
391 const identifier = p.tok_i;
392 defer p.last_field = identifier;
376393 const container_field = try p.expectContainerFieldRecoverable();
377394 if (container_field != 0) {
378395 switch (field_state) {
......@@ -383,6 +400,14 @@ const Parser = struct {
383400 .tag = .decl_between_fields,
384401 .token = p.nodes.items(.main_token)[node],
385402 });
403 try p.warnMsg(.{
404 .tag = .previous_field,
405 .token = p.last_field,
406 });
407 try p.warnMsg(.{
408 .tag = .next_field,
409 .token = identifier,
410 });
386411 // Continue parsing; error will be reported later.
387412 field_state = .err;
388413 },
lib/std/zig/parser_test.zig+2
......@@ -226,6 +226,8 @@ test "zig fmt: decl between fields" {
226226 \\};
227227 , &[_]Error{
228228 .decl_between_fields,
229 .previous_field,
230 .next_field,
229231 });
230232}
231233
src/Module.zig+11
......@@ -3014,6 +3014,17 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
30143014 .parent_decl_node = 0,
30153015 .lazy = .{ .byte_abs = byte_abs },
30163016 }, err_msg, "invalid byte: '{'}'", .{std.zig.fmtEscapes(source[byte_abs..][0..1])});
3017 } else if (parse_err.tag == .decl_between_fields) {
3018 try mod.errNoteNonLazy(.{
3019 .file_scope = file,
3020 .parent_decl_node = 0,
3021 .lazy = .{ .byte_abs = token_starts[file.tree.errors[1].token] },
3022 }, err_msg, "field before declarations here", .{});
3023 try mod.errNoteNonLazy(.{
3024 .file_scope = file,
3025 .parent_decl_node = 0,
3026 .lazy = .{ .byte_abs = token_starts[file.tree.errors[2].token] },
3027 }, err_msg, "field after declarations here", .{});
30173028 }
30183029
30193030 {
src/main.zig+80-61
......@@ -3769,9 +3769,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
37693769 };
37703770 defer tree.deinit(gpa);
37713771
3772 for (tree.errors) |parse_error| {
3773 try printErrMsgToStdErr(gpa, arena, parse_error, tree, "<stdin>", color);
3774 }
3772 try printErrsMsgToStdErr(gpa, arena, tree.errors, tree, "<stdin>", color);
37753773 var has_ast_error = false;
37763774 if (check_ast_flag) {
37773775 const Module = @import("Module.zig");
......@@ -3959,9 +3957,7 @@ fn fmtPathFile(
39593957 var tree = try std.zig.parse(fmt.gpa, source_code);
39603958 defer tree.deinit(fmt.gpa);
39613959
3962 for (tree.errors) |parse_error| {
3963 try printErrMsgToStdErr(fmt.gpa, fmt.arena, parse_error, tree, file_path, fmt.color);
3964 }
3960 try printErrsMsgToStdErr(fmt.gpa, fmt.arena, tree.errors, tree, file_path, fmt.color);
39653961 if (tree.errors.len != 0) {
39663962 fmt.any_error = true;
39673963 return;
......@@ -4041,66 +4037,95 @@ fn fmtPathFile(
40414037 }
40424038}
40434039
4044fn printErrMsgToStdErr(
4040fn printErrsMsgToStdErr(
40454041 gpa: mem.Allocator,
40464042 arena: mem.Allocator,
4047 parse_error: Ast.Error,
4043 parse_errors: []const Ast.Error,
40484044 tree: Ast,
40494045 path: []const u8,
40504046 color: Color,
40514047) !void {
4052 const lok_token = parse_error.token;
4053 const token_tags = tree.tokens.items(.tag);
4054 const start_loc = tree.tokenLocation(0, lok_token);
4055 const source_line = tree.source[start_loc.line_start..start_loc.line_end];
4056
4057 var text_buf = std.ArrayList(u8).init(gpa);
4058 defer text_buf.deinit();
4059 const writer = text_buf.writer();
4060 try tree.renderError(parse_error, writer);
4061 const text = text_buf.items;
4062
4063 var notes_buffer: [1]Compilation.AllErrors.Message = undefined;
4064 var notes_len: usize = 0;
4065
4066 if (token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)] == .invalid) {
4067 const bad_off = @intCast(u32, tree.tokenSlice(parse_error.token + @boolToInt(parse_error.token_is_prev)).len);
4068 const byte_offset = @intCast(u32, start_loc.line_start) + @intCast(u32, start_loc.column) + bad_off;
4069 notes_buffer[notes_len] = .{
4048 var i: usize = 0;
4049 while (i < parse_errors.len) : (i += 1) {
4050 const parse_error = parse_errors[i];
4051 const lok_token = parse_error.token;
4052 const token_tags = tree.tokens.items(.tag);
4053 const start_loc = tree.tokenLocation(0, lok_token);
4054 const source_line = tree.source[start_loc.line_start..start_loc.line_end];
4055
4056 var text_buf = std.ArrayList(u8).init(gpa);
4057 defer text_buf.deinit();
4058 const writer = text_buf.writer();
4059 try tree.renderError(parse_error, writer);
4060 const text = text_buf.items;
4061
4062 var notes_buffer: [2]Compilation.AllErrors.Message = undefined;
4063 var notes_len: usize = 0;
4064
4065 if (token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)] == .invalid) {
4066 const bad_off = @intCast(u32, tree.tokenSlice(parse_error.token + @boolToInt(parse_error.token_is_prev)).len);
4067 const byte_offset = @intCast(u32, start_loc.line_start) + @intCast(u32, start_loc.column) + bad_off;
4068 notes_buffer[notes_len] = .{
4069 .src = .{
4070 .src_path = path,
4071 .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{
4072 std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),
4073 }),
4074 .byte_offset = byte_offset,
4075 .line = @intCast(u32, start_loc.line),
4076 .column = @intCast(u32, start_loc.column) + bad_off,
4077 .source_line = source_line,
4078 },
4079 };
4080 notes_len += 1;
4081 } else if (parse_error.tag == .decl_between_fields) {
4082 const prev_loc = tree.tokenLocation(0, parse_errors[i + 1].token);
4083 notes_buffer[0] = .{
4084 .src = .{
4085 .src_path = path,
4086 .msg = "field before declarations here",
4087 .byte_offset = @intCast(u32, prev_loc.line_start),
4088 .line = @intCast(u32, prev_loc.line),
4089 .column = @intCast(u32, prev_loc.column),
4090 .source_line = tree.source[prev_loc.line_start..prev_loc.line_end],
4091 },
4092 };
4093 const next_loc = tree.tokenLocation(0, parse_errors[i + 2].token);
4094 notes_buffer[1] = .{
4095 .src = .{
4096 .src_path = path,
4097 .msg = "field after declarations here",
4098 .byte_offset = @intCast(u32, next_loc.line_start),
4099 .line = @intCast(u32, next_loc.line),
4100 .column = @intCast(u32, next_loc.column),
4101 .source_line = tree.source[next_loc.line_start..next_loc.line_end],
4102 },
4103 };
4104 notes_len = 2;
4105 i += 2;
4106 }
4107
4108 const extra_offset = tree.errorOffset(parse_error);
4109 const message: Compilation.AllErrors.Message = .{
40704110 .src = .{
40714111 .src_path = path,
4072 .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{
4073 std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),
4074 }),
4075 .byte_offset = byte_offset,
4112 .msg = text,
4113 .byte_offset = @intCast(u32, start_loc.line_start) + extra_offset,
40764114 .line = @intCast(u32, start_loc.line),
4077 .column = @intCast(u32, start_loc.column) + bad_off,
4115 .column = @intCast(u32, start_loc.column) + extra_offset,
40784116 .source_line = source_line,
4117 .notes = notes_buffer[0..notes_len],
40794118 },
40804119 };
4081 notes_len += 1;
4082 }
40834120
4084 const extra_offset = tree.errorOffset(parse_error);
4085 const message: Compilation.AllErrors.Message = .{
4086 .src = .{
4087 .src_path = path,
4088 .msg = text,
4089 .byte_offset = @intCast(u32, start_loc.line_start) + extra_offset,
4090 .line = @intCast(u32, start_loc.line),
4091 .column = @intCast(u32, start_loc.column) + extra_offset,
4092 .source_line = source_line,
4093 .notes = notes_buffer[0..notes_len],
4094 },
4095 };
4096
4097 const ttyconf: std.debug.TTY.Config = switch (color) {
4098 .auto => std.debug.detectTTYConfig(),
4099 .on => .escape_codes,
4100 .off => .no_color,
4101 };
4121 const ttyconf: std.debug.TTY.Config = switch (color) {
4122 .auto => std.debug.detectTTYConfig(),
4123 .on => .escape_codes,
4124 .off => .no_color,
4125 };
41024126
4103 message.renderToStdErr(ttyconf);
4127 message.renderToStdErr(ttyconf);
4128 }
41044129}
41054130
41064131pub const info_zen =
......@@ -4658,9 +4683,7 @@ pub fn cmdAstCheck(
46584683 file.tree_loaded = true;
46594684 defer file.tree.deinit(gpa);
46604685
4661 for (file.tree.errors) |parse_error| {
4662 try printErrMsgToStdErr(gpa, arena, parse_error, file.tree, file.sub_file_path, color);
4663 }
4686 try printErrsMsgToStdErr(gpa, arena, file.tree.errors, file.tree, file.sub_file_path, color);
46644687 if (file.tree.errors.len != 0) {
46654688 process.exit(1);
46664689 }
......@@ -4786,9 +4809,7 @@ pub fn cmdChangelist(
47864809 file.tree_loaded = true;
47874810 defer file.tree.deinit(gpa);
47884811
4789 for (file.tree.errors) |parse_error| {
4790 try printErrMsgToStdErr(gpa, arena, parse_error, file.tree, old_source_file, .auto);
4791 }
4812 try printErrsMsgToStdErr(gpa, arena, file.tree.errors, file.tree, old_source_file, .auto);
47924813 if (file.tree.errors.len != 0) {
47934814 process.exit(1);
47944815 }
......@@ -4825,9 +4846,7 @@ pub fn cmdChangelist(
48254846 var new_tree = try std.zig.parse(gpa, new_source);
48264847 defer new_tree.deinit(gpa);
48274848
4828 for (new_tree.errors) |parse_error| {
4829 try printErrMsgToStdErr(gpa, arena, parse_error, new_tree, new_source_file, .auto);
4830 }
4849 try printErrsMsgToStdErr(gpa, arena, new_tree.errors, new_tree, new_source_file, .auto);
48314850 if (new_tree.errors.len != 0) {
48324851 process.exit(1);
48334852 }
test/compile_errors.zig+2
......@@ -877,6 +877,8 @@ pub fn addCases(ctx: *TestContext) !void {
877877 \\}
878878 , &[_][]const u8{
879879 "tmp.zig:6:5: error: declarations are not allowed between container fields",
880 "tmp.zig:5:5: note: field before declarations here",
881 "tmp.zig:9:5: note: field after declarations here",
880882 });
881883
882884 ctx.objErrStage1("non-extern function with var args",