authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-18 19:44:59+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-18 23:56:05+03:00
logb6fe839248751f6e2cfbdbe2cc31e47aee154555
tree1278697f1d9095eba22bc9cf1d4c4033b7fe6f24
parentfff00c3bbb9ed211f46eb1f1be07ebab84bc8d9d
signature Commit is signed but in an unrecognized format.

update std lib to decls being disallowed between fields


10 files changed, 103 insertions(+), 42 deletions(-)

lib/std/json.zig+14-18
......@@ -77,7 +77,7 @@ test "encodesTo" {
7777 testing.expectEqual(true, encodesTo("false", "false"));
7878 // totally different
7979 testing.expectEqual(false, encodesTo("false", "true"));
80 // differnt lengths
80 // different lengths
8181 testing.expectEqual(false, encodesTo("false", "other"));
8282 // with escape
8383 testing.expectEqual(true, encodesTo("\\", "\\\\"));
......@@ -1771,22 +1771,20 @@ test "parse into struct with misc fields" {
17711771 static_array: [3]f64,
17721772 dynamic_array: []f64,
17731773
1774 const Bar = struct {
1774 complex: struct {
17751775 nested: []const u8,
1776 };
1777 complex: Bar,
1776 },
17781777
1779 const Baz = struct {
1778 veryComplex: []struct {
17801779 foo: []const u8,
1781 };
1782 veryComplex: []Baz,
1780 },
17831781
1782 a_union: Union,
17841783 const Union = union(enum) {
17851784 x: u8,
17861785 float: f64,
17871786 string: []const u8,
17881787 };
1789 a_union: Union,
17901788 };
17911789 const r = try parse(T, &TokenStream.init(
17921790 \\{
......@@ -2323,13 +2321,14 @@ pub const StringifyOptions = struct {
23232321 /// How many indentation levels deep are we?
23242322 indent_level: usize = 0,
23252323
2326 pub const Indentation = union(enum) {
2324 /// What character(s) should be used for indentation?
2325 indent: union(enum) {
23272326 Space: u8,
23282327 Tab: void,
2329 };
2328 } = .{ .Space = 4 },
23302329
2331 /// What character(s) should be used for indentation?
2332 indent: Indentation = Indentation{ .Space = 4 },
2330 /// After a colon, should whitespace be inserted?
2331 separator: bool = true,
23332332
23342333 fn outputIndent(
23352334 whitespace: @This(),
......@@ -2350,17 +2349,17 @@ pub const StringifyOptions = struct {
23502349 n_chars *= whitespace.indent_level;
23512350 try out_stream.writeByteNTimes(char, n_chars);
23522351 }
2353
2354 /// After a colon, should whitespace be inserted?
2355 separator: bool = true,
23562352 };
23572353
23582354 /// Controls the whitespace emitted
23592355 whitespace: ?Whitespace = null,
23602356
2357 string: StringOptions = StringOptions{ .String = .{} },
2358
23612359 /// Should []u8 be serialised as a string? or an array?
23622360 pub const StringOptions = union(enum) {
23632361 Array,
2362 String: StringOutputOptions,
23642363
23652364 /// String output options
23662365 const StringOutputOptions = struct {
......@@ -2370,10 +2369,7 @@ pub const StringifyOptions = struct {
23702369 /// Should unicode characters be escaped in strings?
23712370 escape_unicode: bool = false,
23722371 };
2373 String: StringOutputOptions,
23742372 };
2375
2376 string: StringOptions = StringOptions{ .String = .{} },
23772373};
23782374
23792375fn outputUnicodeEscape(
lib/std/mem.zig+4-7
......@@ -374,7 +374,7 @@ test "mem.zeroes" {
374374 testing.expect(a.y == 10);
375375
376376 const ZigStruct = struct {
377 const IntegralTypes = struct {
377 integral_types: struct {
378378 integer_0: i0,
379379 integer_8: i8,
380380 integer_16: i16,
......@@ -390,16 +390,13 @@ test "mem.zeroes" {
390390
391391 float_32: f32,
392392 float_64: f64,
393 };
394
395 integral_types: IntegralTypes,
393 },
396394
397 const Pointers = struct {
395 pointers: struct {
398396 optional: ?*u8,
399397 c_pointer: [*c]u8,
400398 slice: []u8,
401 };
402 pointers: Pointers,
399 },
403400
404401 array: [2]u32,
405402 optional_int: ?u8,
lib/std/os/bits/linux.zig+19-9
......@@ -1226,17 +1226,11 @@ pub const io_cqring_offsets = extern struct {
12261226};
12271227
12281228pub const io_uring_sqe = extern struct {
1229 opcode: IORING_OP,
1230 flags: u8,
1231 ioprio: u16,
1232 fd: i32,
12331229 pub const union1 = extern union {
12341230 off: u64,
12351231 addr2: u64,
12361232 };
1237 union1: union1,
1238 addr: u64,
1239 len: u32,
1233
12401234 pub const union2 = extern union {
12411235 rw_flags: kernel_rwf,
12421236 fsync_flags: u32,
......@@ -1250,8 +1244,7 @@ pub const io_uring_sqe = extern struct {
12501244 statx_flags: u32,
12511245 fadvise_flags: u32,
12521246 };
1253 union2: union2,
1254 user_data: u64,
1247
12551248 pub const union3 = extern union {
12561249 struct1: extern struct {
12571250 /// index into fixed buffers, if used
......@@ -1262,6 +1255,23 @@ pub const io_uring_sqe = extern struct {
12621255 },
12631256 __pad2: [3]u64,
12641257 };
1258 opcode: IORING_OP,
1259 flags: u8,
1260 ioprio: u16,
1261 fd: i32,
1262
1263 opcode: u8,
1264 flags: u8,
1265 ioprio: u16,
1266 fd: i32,
1267
1268 union1: union1,
1269 addr: u64,
1270 len: u32,
1271
1272 union2: union2,
1273 user_data: u64,
1274
12651275 union3: union3,
12661276};
12671277
lib/std/os/bits/linux/errno-generic.zig+2
......@@ -384,8 +384,10 @@ pub const EKEYREVOKED = 128;
384384pub const EKEYREJECTED = 129;
385385
386386// for robust mutexes
387
387388/// Owner died
388389pub const EOWNERDEAD = 130;
390
389391/// State not recoverable
390392pub const ENOTRECOVERABLE = 131;
391393
lib/std/os/bits/linux/netlink.zig+3-3
......@@ -122,6 +122,9 @@ pub const NLM_F_CAPPED = 0x100;
122122pub const NLM_F_ACK_TLVS = 0x200;
123123
124124pub const NetlinkMessageType = extern enum(u16) {
125 /// < 0x10: reserved control messages
126 pub const MIN_TYPE = 0x10;
127
125128 /// Nothing.
126129 NOOP = 0x1,
127130
......@@ -134,9 +137,6 @@ pub const NetlinkMessageType = extern enum(u16) {
134137 /// Data lost
135138 OVERRUN = 0x4,
136139
137 /// < 0x10: reserved control messages
138 pub const MIN_TYPE = 0x10;
139
140140 // rtlink types
141141
142142 RTM_NEWLINK = 16,
lib/std/os/bits/linux/riscv64.zig+2-1
......@@ -5,6 +5,8 @@ const gid_t = std.os.linux.gid_t;
55const pid_t = std.os.linux.pid_t;
66
77pub const SYS = extern enum(usize) {
8 pub const arch_specific_syscall = 244;
9
810 io_setup = 0,
911 io_destroy = 1,
1012 io_submit = 2,
......@@ -249,7 +251,6 @@ pub const SYS = extern enum(usize) {
249251 accept4 = 242,
250252 recvmmsg = 243,
251253
252 pub const arch_specific_syscall = 244;
253254 riscv_flush_icache = arch_specific_syscall + 15,
254255
255256 wait4 = 260,
lib/std/zig/ast.zig+4
......@@ -164,6 +164,7 @@ pub const Error = union(enum) {
164164 ExpectedLoopExpr: ExpectedLoopExpr,
165165 ExpectedDerefOrUnwrap: ExpectedDerefOrUnwrap,
166166 ExpectedSuffixOp: ExpectedSuffixOp,
167 DeclBetweenFields: DeclBetweenFields,
167168
168169 pub fn render(self: *const Error, tokens: *Tree.TokenList, stream: var) !void {
169170 switch (self.*) {
......@@ -211,6 +212,7 @@ pub const Error = union(enum) {
211212 .ExpectedLoopExpr => |*x| return x.render(tokens, stream),
212213 .ExpectedDerefOrUnwrap => |*x| return x.render(tokens, stream),
213214 .ExpectedSuffixOp => |*x| return x.render(tokens, stream),
215 .DeclBetweenFields => |*x| return x.render(tokens, stream),
214216 }
215217 }
216218
......@@ -260,6 +262,7 @@ pub const Error = union(enum) {
260262 .ExpectedLoopExpr => |x| return x.token,
261263 .ExpectedDerefOrUnwrap => |x| return x.token,
262264 .ExpectedSuffixOp => |x| return x.token,
265 .DeclBetweenFields => |x| return x.token,
263266 }
264267 }
265268
......@@ -304,6 +307,7 @@ pub const Error = union(enum) {
304307 pub const ExtraConstQualifier = SimpleError("Extra const qualifier");
305308 pub const ExtraVolatileQualifier = SimpleError("Extra volatile qualifier");
306309 pub const ExtraAllowZeroQualifier = SimpleError("Extra allowzero qualifier");
310 pub const DeclBetweenFields = SimpleError("Declarations are not allowed between container fields");
307311
308312 pub const ExpectedCall = struct {
309313 node: *Node,
lib/std/zig/parse.zig+33
......@@ -88,6 +88,18 @@ fn parseRoot(arena: *Allocator, it: *TokenIterator, tree: *Tree) Error!*Node.Roo
8888fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !Node.Root.DeclList {
8989 var list = Node.Root.DeclList.init(arena);
9090
91 var field_state: union(enum) {
92 /// no fields have been seen
93 none,
94 /// currently parsing fields
95 seen,
96 /// saw fields and then a declaration after them.
97 /// payload is first token of previous declaration.
98 end: TokenIndex,
99 /// ther was a declaration between fields, don't report more errors
100 err,
101 } = .none;
102
91103 while (true) {
92104 if (try parseContainerDocComments(arena, it, tree)) |node| {
93105 try list.push(node);
......@@ -97,12 +109,18 @@ fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !No
97109 const doc_comments = try parseDocComment(arena, it, tree);
98110
99111 if (try parseTestDecl(arena, it, tree)) |node| {
112 if (field_state == .seen) {
113 field_state = .{ .end = node.firstToken() };
114 }
100115 node.cast(Node.TestDecl).?.doc_comments = doc_comments;
101116 try list.push(node);
102117 continue;
103118 }
104119
105120 if (try parseTopLevelComptime(arena, it, tree)) |node| {
121 if (field_state == .seen) {
122 field_state = .{ .end = node.firstToken() };
123 }
106124 node.cast(Node.Comptime).?.doc_comments = doc_comments;
107125 try list.push(node);
108126 continue;
......@@ -111,6 +129,9 @@ fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !No
111129 const visib_token = eatToken(it, .Keyword_pub);
112130
113131 if (try parseTopLevelDecl(arena, it, tree)) |node| {
132 if (field_state == .seen) {
133 field_state = .{ .end = visib_token orelse node.firstToken() };
134 }
114135 switch (node.id) {
115136 .FnProto => {
116137 node.cast(Node.FnProto).?.doc_comments = doc_comments;
......@@ -146,6 +167,18 @@ fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !No
146167 }
147168
148169 if (try parseContainerField(arena, it, tree)) |node| {
170 switch (field_state) {
171 .none => field_state = .seen,
172 .err, .seen => {},
173 .end => |tok| {
174 try tree.errors.push(.{
175 .DeclBetweenFields = .{ .token = tok },
176 });
177 // continue parsing, error will be reported later
178 field_state = .err;
179 },
180 }
181
149182 const field = node.cast(Node.ContainerField).?;
150183 field.doc_comments = doc_comments;
151184 try list.push(node);
lib/std/zig/parser_test.zig+17-2
......@@ -1,3 +1,18 @@
1test "zig fmt: decl between fields" {
2 try testError(
3 \\const S = struct {
4 \\ const foo = 2;
5 \\ const bar = 2;
6 \\ const baz = 2;
7 \\ a: usize,
8 \\ const foo1 = 2;
9 \\ const bar1 = 2;
10 \\ const baz1 = 2;
11 \\ b: usize,
12 \\};
13 );
14}
15
116test "zig fmt: errdefer with payload" {
217 try testCanonical(
318 \\pub fn main() anyerror!void {
......@@ -2001,11 +2016,11 @@ test "zig fmt: struct declaration" {
20012016 \\ f1: u8,
20022017 \\ f3: u8,
20032018 \\
2019 \\ f2: u8,
2020 \\
20042021 \\ fn method(self: *Self) Self {
20052022 \\ return self.*;
20062023 \\ }
2007 \\
2008 \\ f2: u8,
20092024 \\};
20102025 \\
20112026 \\const Ps = packed struct {
test/compile_errors.zig+5-2
......@@ -4,17 +4,20 @@ const std = @import("std");
44pub fn addCases(cases: *tests.CompileErrorContext) void {
55 cases.add("declaration between fields",
66 \\const S = struct {
7 \\ a: usize,
87 \\ const foo = 2;
98 \\ const bar = 2;
109 \\ const baz = 2;
10 \\ a: usize,
11 \\ const foo1 = 2;
12 \\ const bar1 = 2;
13 \\ const baz1 = 2;
1114 \\ b: usize,
1215 \\};
1316 \\comptime {
1417 \\ _ = S;
1518 \\}
1619 , &[_][]const u8{
17 "tmp.zig:3:5: error: declarations are not allowed between container fields",
20 "tmp.zig:6:5: error: declarations are not allowed between container fields",
1821 });
1922
2023 cases.add("non-extern function with var args",