authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 17:39:41+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 17:39:41+02:00
log928790364ab486de240b8c14ef5ef312079377d7
treef87cda11eb9755e9c5a8477b7ccb1f58b886fb13
parent69d5a106da3fd339c7d1ed177b706aa30d8cb1a9
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: correct Node.firstToken for .fn_decl, add error for missing container


4 files changed, 99 insertions(+), 74 deletions(-)

lib/std/zig/ast.zig+24-1
...@@ -258,6 +258,11 @@ pub const Tree = struct {...@@ -258,6 +258,11 @@ pub const Tree = struct {
258 token_tags[parse_error.token].symbol(),258 token_tags[parse_error.token].symbol(),
259 });259 });
260 },260 },
261 .expected_container => {
262 return stream.print("expected a struct, enum or union, found '{s}'", .{
263 token_tags[parse_error.token].symbol(),
264 });
265 },
261 .extra_align_qualifier => {266 .extra_align_qualifier => {
262 return stream.writeAll("extra align qualifier");267 return stream.writeAll("extra align qualifier");
263 },268 },
...@@ -441,10 +446,27 @@ pub const Tree = struct {...@@ -441,10 +446,27 @@ pub const Tree = struct {
441 .call,446 .call,
442 .call_comma,447 .call_comma,
443 .switch_range,448 .switch_range,
444 .fn_decl,
445 .error_union,449 .error_union,
446 => n = datas[n].lhs,450 => n = datas[n].lhs,
447451
452 .fn_decl => {
453 var i = main_tokens[n]; // fn token
454 while (i > 0) {
455 i -= 1;
456 switch (token_tags[i]) {
457 .keyword_extern,
458 .keyword_export,
459 .keyword_pub,
460 .keyword_threadlocal,
461 .string_literal,
462 => continue,
463
464 else => return i + 1 - end_offset,
465 }
466 }
467 return i - end_offset;
468 },
469
448 .async_call_one,470 .async_call_one,
449 .async_call_one_comma,471 .async_call_one_comma,
450 .async_call,472 .async_call,
...@@ -2338,6 +2360,7 @@ pub const Error = struct {...@@ -2338,6 +2360,7 @@ pub const Error = struct {
2338 expected_var_decl,2360 expected_var_decl,
2339 expected_var_decl_or_fn,2361 expected_var_decl_or_fn,
2340 expected_loop_payload,2362 expected_loop_payload,
2363 expected_container,
2341 extra_align_qualifier,2364 extra_align_qualifier,
2342 extra_allowzero_qualifier,2365 extra_allowzero_qualifier,
2343 extra_const_qualifier,2366 extra_const_qualifier,
lib/std/zig/parse.zig+4-1
...@@ -3627,7 +3627,10 @@ const Parser = struct {...@@ -3627,7 +3627,10 @@ const Parser = struct {
3627 break :blk null_node;3627 break :blk null_node;
3628 }3628 }
3629 },3629 },
3630 else => unreachable,3630 else => {
3631 p.tok_i -= 1;
3632 return p.fail(.expected_container);
3633 },
3631 };3634 };
3632 _ = try p.expectToken(.l_brace);3635 _ = try p.expectToken(.l_brace);
3633 const members = try p.parseContainerMembers();3636 const members = try p.parseContainerMembers();
lib/std/zig/parser_test.zig+70-71
...@@ -2399,20 +2399,20 @@ test "zig fmt: comments before test decl" {...@@ -2399,20 +2399,20 @@ test "zig fmt: comments before test decl" {
2399 );2399 );
2400}2400}
24012401
2402//test "zig fmt: preserve spacing" {2402test "zig fmt: preserve spacing" {
2403// try testCanonical(2403 try testCanonical(
2404// \\const std = @import("std");2404 \\const std = @import("std");
2405// \\2405 \\
2406// \\pub fn main() !void {2406 \\pub fn main() !void {
2407// \\ var stdout_file = std.io.getStdOut;2407 \\ var stdout_file = std.io.getStdOut;
2408// \\ var stdout_file = std.io.getStdOut;2408 \\ var stdout_file = std.io.getStdOut;
2409// \\2409 \\
2410// \\ var stdout_file = std.io.getStdOut;2410 \\ var stdout_file = std.io.getStdOut;
2411// \\ var stdout_file = std.io.getStdOut;2411 \\ var stdout_file = std.io.getStdOut;
2412// \\}2412 \\}
2413// \\2413 \\
2414// );2414 );
2415//}2415}
24162416
2417//test "zig fmt: return types" {2417//test "zig fmt: return types" {
2418// try testCanonical(2418// try testCanonical(
...@@ -2431,27 +2431,27 @@ test "zig fmt: imports" {...@@ -2431,27 +2431,27 @@ test "zig fmt: imports" {
2431 );2431 );
2432}2432}
24332433
2434//test "zig fmt: global declarations" {2434test "zig fmt: global declarations" {
2435// try testCanonical(2435 try testCanonical(
2436// \\const a = b;2436 \\const a = b;
2437// \\pub const a = b;2437 \\pub const a = b;
2438// \\var a = b;2438 \\var a = b;
2439// \\pub var a = b;2439 \\pub var a = b;
2440// \\const a: i32 = b;2440 \\const a: i32 = b;
2441// \\pub const a: i32 = b;2441 \\pub const a: i32 = b;
2442// \\var a: i32 = b;2442 \\var a: i32 = b;
2443// \\pub var a: i32 = b;2443 \\pub var a: i32 = b;
2444// \\extern const a: i32 = b;2444 \\extern const a: i32 = b;
2445// \\pub extern const a: i32 = b;2445 \\pub extern const a: i32 = b;
2446// \\extern var a: i32 = b;2446 \\extern var a: i32 = b;
2447// \\pub extern var a: i32 = b;2447 \\pub extern var a: i32 = b;
2448// \\extern "a" const a: i32 = b;2448 \\extern "a" const a: i32 = b;
2449// \\pub extern "a" const a: i32 = b;2449 \\pub extern "a" const a: i32 = b;
2450// \\extern "a" var a: i32 = b;2450 \\extern "a" var a: i32 = b;
2451// \\pub extern "a" var a: i32 = b;2451 \\pub extern "a" var a: i32 = b;
2452// \\2452 \\
2453// );2453 );
2454//}2454}
24552455
2456test "zig fmt: extern declaration" {2456test "zig fmt: extern declaration" {
2457 try testCanonical(2457 try testCanonical(
...@@ -2680,23 +2680,23 @@ test "zig fmt: functions" {...@@ -2680,23 +2680,23 @@ test "zig fmt: functions" {
2680 );2680 );
2681}2681}
26822682
2683//test "zig fmt: multiline string" {2683test "zig fmt: multiline string" {
2684// try testCanonical(2684 try testCanonical(
2685// \\test "" {2685 \\test "" {
2686// \\ const s1 =2686 \\ const s1 =
2687// \\ \\one2687 \\ \\one
2688// \\ \\two)2688 \\ \\two)
2689// \\ \\three2689 \\ \\three
2690// \\ ;2690 \\ ;
2691// \\ const s3 = // hi2691 \\ const s3 = // hi
2692// \\ \\one2692 \\ \\one
2693// \\ \\two)2693 \\ \\two)
2694// \\ \\three2694 \\ \\three
2695// \\ ;2695 \\ ;
2696// \\}2696 \\}
2697// \\2697 \\
2698// );2698 );
2699//}2699}
27002700
2701test "zig fmt: values" {2701test "zig fmt: values" {
2702 try testCanonical(2702 try testCanonical(
...@@ -3578,15 +3578,14 @@ test "zig fmt: comment after empty comment" {...@@ -3578,15 +3578,14 @@ test "zig fmt: comment after empty comment" {
3578// );3578// );
3579//}3579//}
35803580
3581//test "zig fmt: extern without container keyword returns error" {3581test "zig fmt: extern without container keyword returns error" {
3582// try testError(3582 try testError(
3583// \\const container = extern {};3583 \\const container = extern {};
3584// \\3584 \\
3585// , &[_]Error{3585 , &[_]Error{
3586// .expected_expr,3586 .expected_container,
3587// .expected_var_decl_or_fn,3587 });
3588// });3588}
3589//}
35903589
3591test "zig fmt: same line doc comment returns error" {3590test "zig fmt: same line doc comment returns error" {
3592 try testError(3591 try testError(
...@@ -3706,16 +3705,16 @@ test "zig fmt: C var args" {...@@ -3706,16 +3705,16 @@ test "zig fmt: C var args" {
3706// );3705// );
3707//}3706//}
37083707
3709//test "zig fmt: Don't add extra newline after if" {3708test "zig fmt: Don't add extra newline after if" {
3710// try testCanonical(3709 try testCanonical(
3711// \\pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {3710 \\pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {
3712// \\ if (cwd().symLink(existing_path, new_path, .{})) {3711 \\ if (cwd().symLink(existing_path, new_path, .{})) {
3713// \\ return;3712 \\ return;
3714// \\ }3713 \\ }
3715// \\}3714 \\}
3716// \\3715 \\
3717// );3716 );
3718//}3717}
37193718
3720//test "zig fmt: comments in ternary ifs" {3719//test "zig fmt: comments in ternary ifs" {
3721// try testCanonical(3720// try testCanonical(
lib/std/zig/render.zig+1-1
...@@ -873,7 +873,7 @@ fn renderVarDecl(ais: *Ais, tree: ast.Tree, var_decl: ast.full.VarDecl) Error!vo...@@ -873,7 +873,7 @@ fn renderVarDecl(ais: *Ais, tree: ast.Tree, var_decl: ast.full.VarDecl) Error!vo
873 try renderToken(ais, tree, extern_export_token, Space.space); // extern873 try renderToken(ais, tree, extern_export_token, Space.space); // extern
874874
875 if (var_decl.lib_name) |lib_name| {875 if (var_decl.lib_name) |lib_name| {
876 try renderExpression(ais, tree, lib_name, Space.space); // "lib"876 try renderToken(ais, tree, lib_name, Space.space); // "lib"
877 }877 }
878 }878 }
879879