authorgravatar for pentuppup@noreply.codeberg.orgpentuppup <pentuppup@noreply.codeberg.org> 2025-12-31 12:34:24-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-06 23:40:01+01:00
logc475f1fcd547a93d9e75770900b2fa0c45b43de3
treee131c5cc0a06dd35e7dda10500e629acec81f701
parent8b71ec6db700dc7af22ce0729991bce846ae6d76

Ast: disallow bit alignment on many-item and C pointers


3 files changed, 38 insertions(+), 42 deletions(-)

lib/std/zig/Ast.zig+1-1
...@@ -476,7 +476,7 @@ pub fn renderError(tree: Ast, parse_error: Error, w: *Writer) Writer.Error!void...@@ -476,7 +476,7 @@ pub fn renderError(tree: Ast, parse_error: Error, w: *Writer) Writer.Error!void
476 });476 });
477 },477 },
478 .invalid_bit_range => {478 .invalid_bit_range => {
479 return w.writeAll("bit range not allowed on slices and arrays");479 return w.writeAll("bit range only allowed on single item pointers");
480 },480 },
481 .same_line_doc_comment => {481 .same_line_doc_comment => {
482 return w.writeAll("same line documentation comment");482 return w.writeAll("same line documentation comment");
lib/std/zig/Parse.zig+27-38
...@@ -1820,50 +1820,39 @@ fn parseTypeExpr(p: *Parse) Error!?Node.Index {...@@ -1820,50 +1820,39 @@ fn parseTypeExpr(p: *Parse) Error!?Node.Index {
1820 _ = try p.expectToken(.r_bracket);1820 _ = try p.expectToken(.r_bracket);
1821 const mods = try p.parsePtrModifiers();1821 const mods = try p.parsePtrModifiers();
1822 const elem_type = try p.expectTypeExpr();1822 const elem_type = try p.expectTypeExpr();
1823 if (mods.bit_range_start == .none) {1823 if (mods.bit_range_start.unwrap()) |bit_range_start| {
1824 if (sentinel == null and mods.addrspace_node == .none) {1824 try p.warnMsg(.{
1825 return try p.addNode(.{1825 .tag = .invalid_bit_range,
1826 .tag = .ptr_type_aligned,1826 .token = p.nodeMainToken(bit_range_start),
1827 .main_token = l_bracket,1827 });
1828 .data = .{ .opt_node_and_node = .{1828 }
1829 mods.align_node,1829 if (sentinel == null and mods.addrspace_node == .none) {
1830 elem_type,1830 return try p.addNode(.{
1831 } },1831 .tag = .ptr_type_aligned,
1832 });1832 .main_token = l_bracket,
1833 } else if (mods.align_node == .none and mods.addrspace_node == .none) {1833 .data = .{ .opt_node_and_node = .{
1834 return try p.addNode(.{1834 mods.align_node,
1835 .tag = .ptr_type_sentinel,1835 elem_type,
1836 .main_token = l_bracket,1836 } },
1837 .data = .{ .opt_node_and_node = .{1837 });
1838 .fromOptional(sentinel),1838 } else if (mods.align_node == .none and mods.addrspace_node == .none) {
1839 elem_type,1839 return try p.addNode(.{
1840 } },1840 .tag = .ptr_type_sentinel,
1841 });1841 .main_token = l_bracket,
1842 } else {1842 .data = .{ .opt_node_and_node = .{
1843 return try p.addNode(.{1843 .fromOptional(sentinel),
1844 .tag = .ptr_type,1844 elem_type,
1845 .main_token = l_bracket,1845 } },
1846 .data = .{ .extra_and_node = .{1846 });
1847 try p.addExtra(Node.PtrType{
1848 .sentinel = .fromOptional(sentinel),
1849 .align_node = mods.align_node,
1850 .addrspace_node = mods.addrspace_node,
1851 }),
1852 elem_type,
1853 } },
1854 });
1855 }
1856 } else {1847 } else {
1857 return try p.addNode(.{1848 return try p.addNode(.{
1858 .tag = .ptr_type_bit_range,1849 .tag = .ptr_type,
1859 .main_token = l_bracket,1850 .main_token = l_bracket,
1860 .data = .{ .extra_and_node = .{1851 .data = .{ .extra_and_node = .{
1861 try p.addExtra(Node.PtrTypeBitRange{1852 try p.addExtra(Node.PtrType{
1862 .sentinel = .fromOptional(sentinel),1853 .sentinel = .fromOptional(sentinel),
1863 .align_node = mods.align_node.unwrap().?,1854 .align_node = mods.align_node,
1864 .addrspace_node = mods.addrspace_node,1855 .addrspace_node = mods.addrspace_node,
1865 .bit_range_start = mods.bit_range_start.unwrap().?,
1866 .bit_range_end = mods.bit_range_end.unwrap().?,
1867 }),1856 }),
1868 elem_type,1857 elem_type,
1869 } },1858 } },
lib/std/zig/parser_test.zig+10-3
...@@ -668,7 +668,6 @@ test "zig fmt: pointer-to-many with modifiers" {...@@ -668,7 +668,6 @@ test "zig fmt: pointer-to-many with modifiers" {
668 try testCanonical(668 try testCanonical(
669 \\const x: [*]u32 = undefined;669 \\const x: [*]u32 = undefined;
670 \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;670 \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
671 \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
672 \\671 \\
673 );672 );
674}673}
...@@ -677,7 +676,6 @@ test "zig fmt: sentinel pointer with modifiers" {...@@ -677,7 +676,6 @@ test "zig fmt: sentinel pointer with modifiers" {
677 try testCanonical(676 try testCanonical(
678 \\const x: [*:42]u32 = undefined;677 \\const x: [*:42]u32 = undefined;
679 \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;678 \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
680 \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
681 \\679 \\
682 );680 );
683}681}
...@@ -686,7 +684,6 @@ test "zig fmt: c pointer with modifiers" {...@@ -686,7 +684,6 @@ test "zig fmt: c pointer with modifiers" {
686 try testCanonical(684 try testCanonical(
687 \\const x: [*c]u32 = undefined;685 \\const x: [*c]u32 = undefined;
688 \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;686 \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
689 \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
690 \\687 \\
691 );688 );
692}689}
...@@ -5817,6 +5814,16 @@ test "zig fmt: error for missing sentinel value in sentinel slice" {...@@ -5817,6 +5814,16 @@ test "zig fmt: error for missing sentinel value in sentinel slice" {
5817}5814}
58185815
5819test "zig fmt: error for invalid bit range" {5816test "zig fmt: error for invalid bit range" {
5817 try testError(
5818 \\var x: [*]align(0:0:0)u8 = bar;
5819 , &[_]Error{
5820 .invalid_bit_range,
5821 });
5822 try testError(
5823 \\var x: [*c]align(0:0:0)u8 = bar;
5824 , &[_]Error{
5825 .invalid_bit_range,
5826 });
5820 try testError(5827 try testError(
5821 \\var x: []align(0:0:0)u8 = bar;5828 \\var x: []align(0:0:0)u8 = bar;
5822 , &[_]Error{5829 , &[_]Error{