authorgravatar for will.lillis24@gmail.comWill Lillis <will.lillis24@gmail.com> 2024-07-21 04:55:52-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-21 01:55:52-07:00
log7e76818132318ec7b322fb1f54eb5fded9ec1d89
tree2d4730f053d6e97b5c6f59b6fa43a5e3c2426ee8
parent82679297422d02039c992cc223f5cfbf6fcd8675
signaturebadge-check Signed by PGP key B5690EEEBB952194

fix(fmt): pointer type syntax to index (take 2) (#20336)

* Change main token for many-item and c style pointers from asterisk to l brace, update main token in c translation

9 files changed, 50 insertions(+), 47 deletions(-)

lib/compiler/aro_translate_c/ast.zig+4-4
......@@ -1527,11 +1527,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
15271527 .c_pointer, .single_pointer => {
15281528 const payload = @as(*Payload.Pointer, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
15291529
1530 const asterisk = if (node.tag() == .single_pointer)
1530 const main_token = if (node.tag() == .single_pointer)
15311531 try c.addToken(.asterisk, "*")
15321532 else blk: {
1533 _ = try c.addToken(.l_bracket, "[");
1534 const res = try c.addToken(.asterisk, "*");
1533 const res = try c.addToken(.l_bracket, "[");
1534 _ = try c.addToken(.asterisk, "*");
15351535 _ = try c.addIdentifier("c");
15361536 _ = try c.addToken(.r_bracket, "]");
15371537 break :blk res;
......@@ -1542,7 +1542,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
15421542
15431543 return c.addNode(.{
15441544 .tag = .ptr_type_aligned,
1545 .main_token = asterisk,
1545 .main_token = main_token,
15461546 .data = .{
15471547 .lhs = 0,
15481548 .rhs = elem_type,
lib/std/zig/Ast.zig+17-23
......@@ -729,19 +729,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
729729 .ptr_type_sentinel,
730730 .ptr_type,
731731 .ptr_type_bit_range,
732 => {
733 const main_token = main_tokens[n];
734 return switch (token_tags[main_token]) {
735 .asterisk,
736 .asterisk_asterisk,
737 => switch (token_tags[main_token -| 1]) {
738 .l_bracket => main_token -| 1,
739 else => main_token,
740 },
741 .l_bracket => main_token,
742 else => unreachable,
743 } - end_offset;
744 },
732 => return main_tokens[n] - end_offset,
745733
746734 .switch_case_one => {
747735 if (datas[n].lhs == 0) {
......@@ -2159,12 +2147,11 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType
21592147 const size: Size = switch (token_tags[info.main_token]) {
21602148 .asterisk,
21612149 .asterisk_asterisk,
2162 => switch (token_tags[info.main_token + 1]) {
2163 .r_bracket, .colon => .Many,
2164 .identifier => if (token_tags[info.main_token -| 1] == .l_bracket) Size.C else .One,
2165 else => .One,
2150 => .One,
2151 .l_bracket => switch (token_tags[info.main_token + 1]) {
2152 .asterisk => if (token_tags[info.main_token + 2] == .identifier) Size.C else Size.Many,
2153 else => Size.Slice,
21662154 },
2167 .l_bracket => Size.Slice,
21682155 else => unreachable,
21692156 };
21702157 var result: full.PtrType = .{
......@@ -2178,7 +2165,10 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType
21782165 // here while looking for modifiers as that could result in false
21792166 // positives. Therefore, start after a sentinel if there is one and
21802167 // skip over any align node and bit range nodes.
2181 var i = if (info.sentinel != 0) tree.lastToken(info.sentinel) + 1 else info.main_token;
2168 var i = if (info.sentinel != 0) tree.lastToken(info.sentinel) + 1 else switch (size) {
2169 .Many, .C => info.main_token + 1,
2170 else => info.main_token,
2171 };
21822172 const end = tree.firstToken(info.child_type);
21832173 while (i < end) : (i += 1) {
21842174 switch (token_tags[i]) {
......@@ -3147,24 +3137,28 @@ pub const Node = struct {
31473137 /// `[*]align(lhs) rhs`. lhs can be omitted.
31483138 /// `*align(lhs) rhs`. lhs can be omitted.
31493139 /// `[]rhs`.
3150 /// main_token is the asterisk if a pointer or the lbracket if a slice
3140 /// main_token is the asterisk if a single item pointer or the lbracket
3141 /// if a slice, many-item pointer, or C-pointer
31513142 /// main_token might be a ** token, which is shared with a parent/child
31523143 /// pointer type and may require special handling.
31533144 ptr_type_aligned,
31543145 /// `[*:lhs]rhs`. lhs can be omitted.
31553146 /// `*rhs`.
31563147 /// `[:lhs]rhs`.
3157 /// main_token is the asterisk if a pointer or the lbracket if a slice
3148 /// main_token is the asterisk if a single item pointer or the lbracket
3149 /// if a slice, many-item pointer, or C-pointer
31583150 /// main_token might be a ** token, which is shared with a parent/child
31593151 /// pointer type and may require special handling.
31603152 ptr_type_sentinel,
31613153 /// lhs is index into ptr_type. rhs is the element type expression.
3162 /// main_token is the asterisk if a pointer or the lbracket if a slice
3154 /// main_token is the asterisk if a single item pointer or the lbracket
3155 /// if a slice, many-item pointer, or C-pointer
31633156 /// main_token might be a ** token, which is shared with a parent/child
31643157 /// pointer type and may require special handling.
31653158 ptr_type,
31663159 /// lhs is index into ptr_type_bit_range. rhs is the element type expression.
3167 /// main_token is the asterisk if a pointer or the lbracket if a slice
3160 /// main_token is the asterisk if a single item pointer or the lbracket
3161 /// if a slice, many-item pointer, or C-pointer
31683162 /// main_token might be a ** token, which is shared with a parent/child
31693163 /// pointer type and may require special handling.
31703164 ptr_type_bit_range,
lib/std/zig/Parse.zig+5-5
......@@ -1905,8 +1905,8 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
19051905 },
19061906 .l_bracket => switch (p.token_tags[p.tok_i + 1]) {
19071907 .asterisk => {
1908 const l_bracket = p.nextToken();
19081909 _ = p.nextToken();
1909 const asterisk = p.nextToken();
19101910 var sentinel: Node.Index = 0;
19111911 if (p.eatToken(.identifier)) |ident| {
19121912 const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]];
......@@ -1923,7 +1923,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
19231923 if (sentinel == 0 and mods.addrspace_node == 0) {
19241924 return p.addNode(.{
19251925 .tag = .ptr_type_aligned,
1926 .main_token = asterisk,
1926 .main_token = l_bracket,
19271927 .data = .{
19281928 .lhs = mods.align_node,
19291929 .rhs = elem_type,
......@@ -1932,7 +1932,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
19321932 } else if (mods.align_node == 0 and mods.addrspace_node == 0) {
19331933 return p.addNode(.{
19341934 .tag = .ptr_type_sentinel,
1935 .main_token = asterisk,
1935 .main_token = l_bracket,
19361936 .data = .{
19371937 .lhs = sentinel,
19381938 .rhs = elem_type,
......@@ -1941,7 +1941,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
19411941 } else {
19421942 return p.addNode(.{
19431943 .tag = .ptr_type,
1944 .main_token = asterisk,
1944 .main_token = l_bracket,
19451945 .data = .{
19461946 .lhs = try p.addExtra(Node.PtrType{
19471947 .sentinel = sentinel,
......@@ -1955,7 +1955,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
19551955 } else {
19561956 return p.addNode(.{
19571957 .tag = .ptr_type_bit_range,
1958 .main_token = asterisk,
1958 .main_token = l_bracket,
19591959 .data = .{
19601960 .lhs = try p.addExtra(Node.PtrTypeBitRange{
19611961 .sentinel = sentinel,
lib/std/zig/parser_test.zig+9
......@@ -5915,6 +5915,15 @@ test "zig fmt: error for ptr mod on array child type" {
59155915 });
59165916}
59175917
5918test "zig fmt: pointer type syntax to index" {
5919 try testCanonical(
5920 \\test {
5921 \\ _ = .{}[*0];
5922 \\}
5923 \\
5924 );
5925}
5926
59185927test "recovery: top level" {
59195928 try testError(
59205929 \\test "" {inline}
lib/std/zig/render.zig+10-10
......@@ -961,22 +961,22 @@ fn renderPtrType(r: *Render, ptr_type: Ast.full.PtrType, space: Space) Error!voi
961961 },
962962 .Many => {
963963 if (ptr_type.ast.sentinel == 0) {
964 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket
965 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk
966 try renderToken(r, ptr_type.ast.main_token + 1, .none); // rbracket
964 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
965 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
966 try renderToken(r, ptr_type.ast.main_token + 2, .none); // rbracket
967967 } else {
968 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket
969 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk
970 try renderToken(r, ptr_type.ast.main_token + 1, .none); // colon
968 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
969 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
970 try renderToken(r, ptr_type.ast.main_token + 2, .none); // colon
971971 try renderExpression(r, ptr_type.ast.sentinel, .none);
972972 try renderToken(r, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
973973 }
974974 },
975975 .C => {
976 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket
977 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk
978 try renderToken(r, ptr_type.ast.main_token + 1, .none); // c
979 try renderToken(r, ptr_type.ast.main_token + 2, .none); // rbracket
976 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
977 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
978 try renderToken(r, ptr_type.ast.main_token + 2, .none); // c
979 try renderToken(r, ptr_type.ast.main_token + 3, .none); // rbracket
980980 },
981981 .Slice => {
982982 if (ptr_type.ast.sentinel == 0) {
test/cases/compile_errors/disallow_coercion_from_non-null-terminated_pointer_to_null-terminated_pointer.zig+1-1
......@@ -11,4 +11,4 @@ pub export fn entry() void {
1111//
1212// :5:14: error: expected type '[*:0]const u8', found '[*]const u8'
1313// :5:14: note: destination pointer requires '0' sentinel
14// :1:20: note: parameter type declared here
14// :1:19: note: parameter type declared here
test/cases/compile_errors/incompatible_sentinels.zig+2-2
......@@ -21,10 +21,10 @@ export fn entry4() void {
2121//
2222// :4:12: error: expected type '[*:0]u8', found '[*:255]u8'
2323// :4:12: note: pointer sentinel '255' cannot cast into pointer sentinel '0'
24// :3:35: note: function return type declared here
24// :3:34: note: function return type declared here
2525// :7:12: error: expected type '[*:0]u8', found '[*]u8'
2626// :7:12: note: destination pointer requires '0' sentinel
27// :6:31: note: function return type declared here
27// :6:30: note: function return type declared here
2828// :10:35: error: expected type '[2:0]u8', found '[2:255]u8'
2929// :10:35: note: array sentinel '255' cannot cast into array sentinel '0'
3030// :14:31: error: expected type '[2:0]u8', found '[2]u8'
test/cases/compile_errors/issue_4207_coerce_from_non-terminated-slice_to_terminated-pointer.zig+1-1
......@@ -9,4 +9,4 @@ export fn foo() [*:0]const u8 {
99//
1010// :3:18: error: expected type '[*:0]const u8', found '*[64]u8'
1111// :3:18: note: destination pointer requires '0' sentinel
12// :1:18: note: function return type declared here
12// :1:17: note: function return type declared here
test/cases/compile_errors/using_an_unknown_len_ptr_type_instead_of_array.zig+1-1
......@@ -10,4 +10,4 @@ comptime {
1010// backend=stage2
1111// target=native
1212//
13// :1:22: error: type '[*][*]const u8' does not support array initialization syntax
13// :1:21: error: type '[*][*]const u8' does not support array initialization syntax