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 {...@@ -1527,11 +1527,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1527 .c_pointer, .single_pointer => {1527 .c_pointer, .single_pointer => {
1528 const payload = @as(*Payload.Pointer, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;1528 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)
1531 try c.addToken(.asterisk, "*")1531 try c.addToken(.asterisk, "*")
1532 else blk: {1532 else blk: {
1533 _ = try c.addToken(.l_bracket, "[");1533 const res = try c.addToken(.l_bracket, "[");
1534 const res = try c.addToken(.asterisk, "*");1534 _ = try c.addToken(.asterisk, "*");
1535 _ = try c.addIdentifier("c");1535 _ = try c.addIdentifier("c");
1536 _ = try c.addToken(.r_bracket, "]");1536 _ = try c.addToken(.r_bracket, "]");
1537 break :blk res;1537 break :blk res;
...@@ -1542,7 +1542,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1542,7 +1542,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
15421542
1543 return c.addNode(.{1543 return c.addNode(.{
1544 .tag = .ptr_type_aligned,1544 .tag = .ptr_type_aligned,
1545 .main_token = asterisk,1545 .main_token = main_token,
1546 .data = .{1546 .data = .{
1547 .lhs = 0,1547 .lhs = 0,
1548 .rhs = elem_type,1548 .rhs = elem_type,
lib/std/zig/Ast.zig+17-23
...@@ -729,19 +729,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -729,19 +729,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
729 .ptr_type_sentinel,729 .ptr_type_sentinel,
730 .ptr_type,730 .ptr_type,
731 .ptr_type_bit_range,731 .ptr_type_bit_range,
732 => {732 => return main_tokens[n] - end_offset,
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 },
745733
746 .switch_case_one => {734 .switch_case_one => {
747 if (datas[n].lhs == 0) {735 if (datas[n].lhs == 0) {
...@@ -2159,12 +2147,11 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType...@@ -2159,12 +2147,11 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType
2159 const size: Size = switch (token_tags[info.main_token]) {2147 const size: Size = switch (token_tags[info.main_token]) {
2160 .asterisk,2148 .asterisk,
2161 .asterisk_asterisk,2149 .asterisk_asterisk,
2162 => switch (token_tags[info.main_token + 1]) {2150 => .One,
2163 .r_bracket, .colon => .Many,2151 .l_bracket => switch (token_tags[info.main_token + 1]) {
2164 .identifier => if (token_tags[info.main_token -| 1] == .l_bracket) Size.C else .One,2152 .asterisk => if (token_tags[info.main_token + 2] == .identifier) Size.C else Size.Many,
2165 else => .One,2153 else => Size.Slice,
2166 },2154 },
2167 .l_bracket => Size.Slice,
2168 else => unreachable,2155 else => unreachable,
2169 };2156 };
2170 var result: full.PtrType = .{2157 var result: full.PtrType = .{
...@@ -2178,7 +2165,10 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType...@@ -2178,7 +2165,10 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType
2178 // here while looking for modifiers as that could result in false2165 // here while looking for modifiers as that could result in false
2179 // positives. Therefore, start after a sentinel if there is one and2166 // positives. Therefore, start after a sentinel if there is one and
2180 // skip over any align node and bit range nodes.2167 // 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 };
2182 const end = tree.firstToken(info.child_type);2172 const end = tree.firstToken(info.child_type);
2183 while (i < end) : (i += 1) {2173 while (i < end) : (i += 1) {
2184 switch (token_tags[i]) {2174 switch (token_tags[i]) {
...@@ -3147,24 +3137,28 @@ pub const Node = struct {...@@ -3147,24 +3137,28 @@ pub const Node = struct {
3147 /// `[*]align(lhs) rhs`. lhs can be omitted.3137 /// `[*]align(lhs) rhs`. lhs can be omitted.
3148 /// `*align(lhs) rhs`. lhs can be omitted.3138 /// `*align(lhs) rhs`. lhs can be omitted.
3149 /// `[]rhs`.3139 /// `[]rhs`.
3150 /// main_token is the asterisk if a pointer or the lbracket if a slice3140 /// main_token is the asterisk if a single item pointer or the lbracket
3141 /// if a slice, many-item pointer, or C-pointer
3151 /// main_token might be a ** token, which is shared with a parent/child3142 /// main_token might be a ** token, which is shared with a parent/child
3152 /// pointer type and may require special handling.3143 /// pointer type and may require special handling.
3153 ptr_type_aligned,3144 ptr_type_aligned,
3154 /// `[*:lhs]rhs`. lhs can be omitted.3145 /// `[*:lhs]rhs`. lhs can be omitted.
3155 /// `*rhs`.3146 /// `*rhs`.
3156 /// `[:lhs]rhs`.3147 /// `[:lhs]rhs`.
3157 /// main_token is the asterisk if a pointer or the lbracket if a slice3148 /// main_token is the asterisk if a single item pointer or the lbracket
3149 /// if a slice, many-item pointer, or C-pointer
3158 /// main_token might be a ** token, which is shared with a parent/child3150 /// main_token might be a ** token, which is shared with a parent/child
3159 /// pointer type and may require special handling.3151 /// pointer type and may require special handling.
3160 ptr_type_sentinel,3152 ptr_type_sentinel,
3161 /// lhs is index into ptr_type. rhs is the element type expression.3153 /// 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 slice3154 /// main_token is the asterisk if a single item pointer or the lbracket
3155 /// if a slice, many-item pointer, or C-pointer
3163 /// main_token might be a ** token, which is shared with a parent/child3156 /// main_token might be a ** token, which is shared with a parent/child
3164 /// pointer type and may require special handling.3157 /// pointer type and may require special handling.
3165 ptr_type,3158 ptr_type,
3166 /// lhs is index into ptr_type_bit_range. rhs is the element type expression.3159 /// 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 slice3160 /// main_token is the asterisk if a single item pointer or the lbracket
3161 /// if a slice, many-item pointer, or C-pointer
3168 /// main_token might be a ** token, which is shared with a parent/child3162 /// main_token might be a ** token, which is shared with a parent/child
3169 /// pointer type and may require special handling.3163 /// pointer type and may require special handling.
3170 ptr_type_bit_range,3164 ptr_type_bit_range,
lib/std/zig/Parse.zig+5-5
...@@ -1905,8 +1905,8 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {...@@ -1905,8 +1905,8 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
1905 },1905 },
1906 .l_bracket => switch (p.token_tags[p.tok_i + 1]) {1906 .l_bracket => switch (p.token_tags[p.tok_i + 1]) {
1907 .asterisk => {1907 .asterisk => {
1908 const l_bracket = p.nextToken();
1908 _ = p.nextToken();1909 _ = p.nextToken();
1909 const asterisk = p.nextToken();
1910 var sentinel: Node.Index = 0;1910 var sentinel: Node.Index = 0;
1911 if (p.eatToken(.identifier)) |ident| {1911 if (p.eatToken(.identifier)) |ident| {
1912 const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]];1912 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 {...@@ -1923,7 +1923,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
1923 if (sentinel == 0 and mods.addrspace_node == 0) {1923 if (sentinel == 0 and mods.addrspace_node == 0) {
1924 return p.addNode(.{1924 return p.addNode(.{
1925 .tag = .ptr_type_aligned,1925 .tag = .ptr_type_aligned,
1926 .main_token = asterisk,1926 .main_token = l_bracket,
1927 .data = .{1927 .data = .{
1928 .lhs = mods.align_node,1928 .lhs = mods.align_node,
1929 .rhs = elem_type,1929 .rhs = elem_type,
...@@ -1932,7 +1932,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {...@@ -1932,7 +1932,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
1932 } else if (mods.align_node == 0 and mods.addrspace_node == 0) {1932 } else if (mods.align_node == 0 and mods.addrspace_node == 0) {
1933 return p.addNode(.{1933 return p.addNode(.{
1934 .tag = .ptr_type_sentinel,1934 .tag = .ptr_type_sentinel,
1935 .main_token = asterisk,1935 .main_token = l_bracket,
1936 .data = .{1936 .data = .{
1937 .lhs = sentinel,1937 .lhs = sentinel,
1938 .rhs = elem_type,1938 .rhs = elem_type,
...@@ -1941,7 +1941,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {...@@ -1941,7 +1941,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
1941 } else {1941 } else {
1942 return p.addNode(.{1942 return p.addNode(.{
1943 .tag = .ptr_type,1943 .tag = .ptr_type,
1944 .main_token = asterisk,1944 .main_token = l_bracket,
1945 .data = .{1945 .data = .{
1946 .lhs = try p.addExtra(Node.PtrType{1946 .lhs = try p.addExtra(Node.PtrType{
1947 .sentinel = sentinel,1947 .sentinel = sentinel,
...@@ -1955,7 +1955,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {...@@ -1955,7 +1955,7 @@ fn parseTypeExpr(p: *Parse) Error!Node.Index {
1955 } else {1955 } else {
1956 return p.addNode(.{1956 return p.addNode(.{
1957 .tag = .ptr_type_bit_range,1957 .tag = .ptr_type_bit_range,
1958 .main_token = asterisk,1958 .main_token = l_bracket,
1959 .data = .{1959 .data = .{
1960 .lhs = try p.addExtra(Node.PtrTypeBitRange{1960 .lhs = try p.addExtra(Node.PtrTypeBitRange{
1961 .sentinel = sentinel,1961 .sentinel = sentinel,
lib/std/zig/parser_test.zig+9
...@@ -5915,6 +5915,15 @@ test "zig fmt: error for ptr mod on array child type" {...@@ -5915,6 +5915,15 @@ test "zig fmt: error for ptr mod on array child type" {
5915 });5915 });
5916}5916}
59175917
5918test "zig fmt: pointer type syntax to index" {
5919 try testCanonical(
5920 \\test {
5921 \\ _ = .{}[*0];
5922 \\}
5923 \\
5924 );
5925}
5926
5918test "recovery: top level" {5927test "recovery: top level" {
5919 try testError(5928 try testError(
5920 \\test "" {inline}5929 \\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...@@ -961,22 +961,22 @@ fn renderPtrType(r: *Render, ptr_type: Ast.full.PtrType, space: Space) Error!voi
961 },961 },
962 .Many => {962 .Many => {
963 if (ptr_type.ast.sentinel == 0) {963 if (ptr_type.ast.sentinel == 0) {
964 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket964 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
965 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk965 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
966 try renderToken(r, ptr_type.ast.main_token + 1, .none); // rbracket966 try renderToken(r, ptr_type.ast.main_token + 2, .none); // rbracket
967 } else {967 } else {
968 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket968 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
969 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk969 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
970 try renderToken(r, ptr_type.ast.main_token + 1, .none); // colon970 try renderToken(r, ptr_type.ast.main_token + 2, .none); // colon
971 try renderExpression(r, ptr_type.ast.sentinel, .none);971 try renderExpression(r, ptr_type.ast.sentinel, .none);
972 try renderToken(r, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket972 try renderToken(r, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
973 }973 }
974 },974 },
975 .C => {975 .C => {
976 try renderToken(r, ptr_type.ast.main_token - 1, .none); // lbracket976 try renderToken(r, ptr_type.ast.main_token, .none); // lbracket
977 try renderToken(r, ptr_type.ast.main_token, .none); // asterisk977 try renderToken(r, ptr_type.ast.main_token + 1, .none); // asterisk
978 try renderToken(r, ptr_type.ast.main_token + 1, .none); // c978 try renderToken(r, ptr_type.ast.main_token + 2, .none); // c
979 try renderToken(r, ptr_type.ast.main_token + 2, .none); // rbracket979 try renderToken(r, ptr_type.ast.main_token + 3, .none); // rbracket
980 },980 },
981 .Slice => {981 .Slice => {
982 if (ptr_type.ast.sentinel == 0) {982 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 {...@@ -11,4 +11,4 @@ pub export fn entry() void {
11//11//
12// :5:14: error: expected type '[*:0]const u8', found '[*]const u8'12// :5:14: error: expected type '[*:0]const u8', found '[*]const u8'
13// :5:14: note: destination pointer requires '0' sentinel13// :5:14: note: destination pointer requires '0' sentinel
14// :1:20: note: parameter type declared here14// :1:19: note: parameter type declared here
test/cases/compile_errors/incompatible_sentinels.zig+2-2
...@@ -21,10 +21,10 @@ export fn entry4() void {...@@ -21,10 +21,10 @@ export fn entry4() void {
21//21//
22// :4:12: error: expected type '[*:0]u8', found '[*:255]u8'22// :4:12: error: expected type '[*:0]u8', found '[*:255]u8'
23// :4:12: note: pointer sentinel '255' cannot cast into pointer sentinel '0'23// :4:12: note: pointer sentinel '255' cannot cast into pointer sentinel '0'
24// :3:35: note: function return type declared here24// :3:34: note: function return type declared here
25// :7:12: error: expected type '[*:0]u8', found '[*]u8'25// :7:12: error: expected type '[*:0]u8', found '[*]u8'
26// :7:12: note: destination pointer requires '0' sentinel26// :7:12: note: destination pointer requires '0' sentinel
27// :6:31: note: function return type declared here27// :6:30: note: function return type declared here
28// :10:35: error: expected type '[2:0]u8', found '[2:255]u8'28// :10:35: error: expected type '[2:0]u8', found '[2:255]u8'
29// :10:35: note: array sentinel '255' cannot cast into array sentinel '0'29// :10:35: note: array sentinel '255' cannot cast into array sentinel '0'
30// :14:31: error: expected type '[2:0]u8', found '[2]u8'30// :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 {...@@ -9,4 +9,4 @@ export fn foo() [*:0]const u8 {
9//9//
10// :3:18: error: expected type '[*:0]const u8', found '*[64]u8'10// :3:18: error: expected type '[*:0]const u8', found '*[64]u8'
11// :3:18: note: destination pointer requires '0' sentinel11// :3:18: note: destination pointer requires '0' sentinel
12// :1:18: note: function return type declared here12// :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 {...@@ -10,4 +10,4 @@ comptime {
10// backend=stage210// backend=stage2
11// target=native11// target=native
12//12//
13// :1:22: error: type '[*][*]const u8' does not support array initialization syntax13// :1:21: error: type '[*][*]const u8' does not support array initialization syntax