authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-05-08 17:59:06+10:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-08 10:59:06+03:00
log5a3eca5d4ca42f92abe60040e21ffd8e307d8466
treea69d1c9ccff1feeb63f6117658695703f682ccd4
parentbac3a28214d07f761e25fcc12a6708f70c8ccc01
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Disallow named test decls with duplicate names


15 files changed, 158 insertions(+), 125 deletions(-)

lib/std/fmt.zig+37-38
......@@ -2244,8 +2244,8 @@ test "struct" {
22442244 field: u8,
22452245 };
22462246 const value = Struct{ .field = 42 };
2247 try expectFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value});
2248 try expectFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
2247 try expectFmt("struct: fmt.test.struct.Struct{ .field = 42 }\n", "struct: {}\n", .{value});
2248 try expectFmt("struct: fmt.test.struct.Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
22492249 }
22502250 {
22512251 const Struct = struct {
......@@ -2253,8 +2253,24 @@ test "struct" {
22532253 b: u1,
22542254 };
22552255 const value = Struct{ .a = 0, .b = 1 };
2256 try expectFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
2256 try expectFmt("struct: fmt.test.struct.Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
22572257 }
2258
2259 const S = struct {
2260 a: u32,
2261 b: anyerror,
2262 };
2263
2264 const inst = S{
2265 .a = 456,
2266 .b = error.Unused,
2267 };
2268
2269 try expectFmt("fmt.test.struct.S{ .a = 456, .b = error.Unused }", "{}", .{inst});
2270 // Tuples
2271 try expectFmt("{ }", "{}", .{.{}});
2272 try expectFmt("{ -1 }", "{}", .{.{-1}});
2273 try expectFmt("{ -1, 42, 2.5e+04 }", "{}", .{.{ -1, 42, 0.25e5 }});
22582274}
22592275
22602276test "enum" {
......@@ -2263,13 +2279,26 @@ test "enum" {
22632279 Two,
22642280 };
22652281 const value = Enum.Two;
2266 try expectFmt("enum: Enum.Two\n", "enum: {}\n", .{value});
2267 try expectFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
2268 try expectFmt("enum: Enum.One\n", "enum: {}\n", .{Enum.One});
2269 try expectFmt("enum: Enum.Two\n", "enum: {}\n", .{Enum.Two});
2282 try expectFmt("enum: fmt.test.enum.Enum.Two\n", "enum: {}\n", .{value});
2283 try expectFmt("enum: fmt.test.enum.Enum.Two\n", "enum: {}\n", .{&value});
2284 try expectFmt("enum: fmt.test.enum.Enum.One\n", "enum: {}\n", .{Enum.One});
2285 try expectFmt("enum: fmt.test.enum.Enum.Two\n", "enum: {}\n", .{Enum.Two});
22702286
22712287 // test very large enum to verify ct branch quota is large enough
2272 try expectFmt("enum: os.windows.win32error.Win32Error.INVALID_FUNCTION\n", "enum: {}\n", .{std.os.windows.Win32Error.INVALID_FUNCTION});
2288 // TODO: https://github.com/ziglang/zig/issues/15609
2289 if (!((builtin.cpu.arch == .wasm32) and builtin.mode == .Debug)) {
2290 try expectFmt("enum: os.windows.win32error.Win32Error.INVALID_FUNCTION\n", "enum: {}\n", .{std.os.windows.Win32Error.INVALID_FUNCTION});
2291 }
2292
2293 const E = enum {
2294 One,
2295 Two,
2296 Three,
2297 };
2298
2299 const inst = E.Two;
2300
2301 try expectFmt("fmt.test.enum.E.Two", "{}", .{inst});
22732302}
22742303
22752304test "non-exhaustive enum" {
......@@ -2445,24 +2474,6 @@ test "custom" {
24452474 try expectFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value});
24462475}
24472476
2448test "struct" {
2449 const S = struct {
2450 a: u32,
2451 b: anyerror,
2452 };
2453
2454 const inst = S{
2455 .a = 456,
2456 .b = error.Unused,
2457 };
2458
2459 try expectFmt("fmt.test.struct.S{ .a = 456, .b = error.Unused }", "{}", .{inst});
2460 // Tuples
2461 try expectFmt("{ }", "{}", .{.{}});
2462 try expectFmt("{ -1 }", "{}", .{.{-1}});
2463 try expectFmt("{ -1, 42, 2.5e+04 }", "{}", .{.{ -1, 42, 0.25e5 }});
2464}
2465
24662477test "union" {
24672478 const TU = union(enum) {
24682479 float: f32,
......@@ -2493,18 +2504,6 @@ test "union" {
24932504 try std.testing.expect(mem.eql(u8, eu_result[0..18], "fmt.test.union.EU@"));
24942505}
24952506
2496test "enum" {
2497 const E = enum {
2498 One,
2499 Two,
2500 Three,
2501 };
2502
2503 const inst = E.Two;
2504
2505 try expectFmt("fmt.test.enum.E.Two", "{}", .{inst});
2506}
2507
25082507test "struct.self-referential" {
25092508 const S = struct {
25102509 const SelfType = @This();
lib/std/hash_map.zig+16-18
......@@ -1741,6 +1741,22 @@ test "std.hash_map clone" {
17411741 try expectEqual(b.get(1).?, 1);
17421742 try expectEqual(b.get(2).?, 2);
17431743 try expectEqual(b.get(3).?, 3);
1744
1745 var original = AutoHashMap(i32, i32).init(std.testing.allocator);
1746 defer original.deinit();
1747
1748 var i: u8 = 0;
1749 while (i < 10) : (i += 1) {
1750 try original.putNoClobber(i, i * 10);
1751 }
1752
1753 var copy = try original.clone();
1754 defer copy.deinit();
1755
1756 i = 0;
1757 while (i < 10) : (i += 1) {
1758 try testing.expect(copy.get(i).? == i * 10);
1759 }
17441760}
17451761
17461762test "std.hash_map ensureTotalCapacity with existing elements" {
......@@ -2072,24 +2088,6 @@ test "std.hash_map basic hash map usage" {
20722088 try testing.expect(map.remove(3) == true);
20732089}
20742090
2075test "std.hash_map clone" {
2076 var original = AutoHashMap(i32, i32).init(std.testing.allocator);
2077 defer original.deinit();
2078
2079 var i: u8 = 0;
2080 while (i < 10) : (i += 1) {
2081 try original.putNoClobber(i, i * 10);
2082 }
2083
2084 var copy = try original.clone();
2085 defer copy.deinit();
2086
2087 i = 0;
2088 while (i < 10) : (i += 1) {
2089 try testing.expect(copy.get(i).? == i * 10);
2090 }
2091}
2092
20932091test "std.hash_map getOrPutAdapted" {
20942092 const AdaptedContext = struct {
20952093 fn eql(self: @This(), adapted_key: []const u8, test_key: u64) bool {
lib/std/io/reader.zig+12-11
......@@ -553,10 +553,18 @@ test "Reader.readUntilDelimiter returns StreamTooLong, then bytes read until the
553553}
554554
555555test "Reader.readUntilDelimiter returns EndOfStream" {
556 var buf: [5]u8 = undefined;
557 var fis = std.io.fixedBufferStream("");
558 const reader = fis.reader();
559 try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
556 {
557 var buf: [5]u8 = undefined;
558 var fis = std.io.fixedBufferStream("");
559 const reader = fis.reader();
560 try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
561 }
562 {
563 var buf: [5]u8 = undefined;
564 var fis = std.io.fixedBufferStream("1234");
565 const reader = fis.reader();
566 try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
567 }
560568}
561569
562570test "Reader.readUntilDelimiter returns bytes read until delimiter, then EndOfStream" {
......@@ -567,13 +575,6 @@ test "Reader.readUntilDelimiter returns bytes read until delimiter, then EndOfSt
567575 try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
568576}
569577
570test "Reader.readUntilDelimiter returns EndOfStream" {
571 var buf: [5]u8 = undefined;
572 var fis = std.io.fixedBufferStream("1234");
573 const reader = fis.reader();
574 try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
575}
576
577578test "Reader.readUntilDelimiter returns StreamTooLong, then EndOfStream" {
578579 var buf: [5]u8 = undefined;
579580 var fis = std.io.fixedBufferStream("12345");
lib/std/math/big/int_test.zig+4-9
......@@ -2012,15 +2012,10 @@ test "big.int shift-right negative" {
20122012 defer arg2.deinit();
20132013 try a.shiftRight(&arg2, 10);
20142014 try testing.expect((try a.to(i32)) == -1); // -5 >> 10 == -1
2015}
20162015
2017test "big.int shift-right negative" {
2018 var a = try Managed.init(testing.allocator);
2019 defer a.deinit();
2020
2021 var arg = try Managed.initSet(testing.allocator, -10);
2022 defer arg.deinit();
2023 try a.shiftRight(&arg, 1232);
2016 var arg3 = try Managed.initSet(testing.allocator, -10);
2017 defer arg3.deinit();
2018 try a.shiftRight(&arg3, 1232);
20242019 try testing.expect((try a.to(i32)) == -1); // -10 >> 1232 == -1
20252020}
20262021
......@@ -2483,7 +2478,7 @@ test "big.int gcd non-one small" {
24832478 try testing.expect((try r.to(u32)) == 1);
24842479}
24852480
2486test "big.int gcd non-one small" {
2481test "big.int gcd non-one medium" {
24872482 var a = try Managed.initSet(testing.allocator, 4864);
24882483 defer a.deinit();
24892484 var b = try Managed.initSet(testing.allocator, 3458);
lib/std/math/big/rational.zig+27-25
......@@ -782,36 +782,38 @@ test "big.rational mul" {
782782}
783783
784784test "big.rational div" {
785 var a = try Rational.init(testing.allocator);
786 defer a.deinit();
787 var b = try Rational.init(testing.allocator);
788 defer b.deinit();
789 var r = try Rational.init(testing.allocator);
790 defer r.deinit();
785 {
786 var a = try Rational.init(testing.allocator);
787 defer a.deinit();
788 var b = try Rational.init(testing.allocator);
789 defer b.deinit();
790 var r = try Rational.init(testing.allocator);
791 defer r.deinit();
791792
792 try a.setRatio(78923, 23341);
793 try b.setRatio(123097, 12441414);
794 try a.div(a, b);
793 try a.setRatio(78923, 23341);
794 try b.setRatio(123097, 12441414);
795 try a.div(a, b);
795796
796 try r.setRatio(75531824394, 221015929);
797 try testing.expect((try a.order(r)) == .eq);
798}
797 try r.setRatio(75531824394, 221015929);
798 try testing.expect((try a.order(r)) == .eq);
799 }
799800
800test "big.rational div" {
801 var a = try Rational.init(testing.allocator);
802 defer a.deinit();
803 var r = try Rational.init(testing.allocator);
804 defer r.deinit();
801 {
802 var a = try Rational.init(testing.allocator);
803 defer a.deinit();
804 var r = try Rational.init(testing.allocator);
805 defer r.deinit();
805806
806 try a.setRatio(78923, 23341);
807 a.invert();
807 try a.setRatio(78923, 23341);
808 a.invert();
808809
809 try r.setRatio(23341, 78923);
810 try testing.expect((try a.order(r)) == .eq);
810 try r.setRatio(23341, 78923);
811 try testing.expect((try a.order(r)) == .eq);
811812
812 try a.setRatio(-78923, 23341);
813 a.invert();
813 try a.setRatio(-78923, 23341);
814 a.invert();
814815
815 try r.setRatio(-23341, 78923);
816 try testing.expect((try a.order(r)) == .eq);
816 try r.setRatio(-23341, 78923);
817 try testing.expect((try a.order(r)) == .eq);
818 }
817819}
lib/std/net/test.zig+1-1
......@@ -182,7 +182,7 @@ test "listen on a port, send bytes, receive bytes" {
182182 try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]);
183183}
184184
185test "listen on a port, send bytes, receive bytes" {
185test "listen on a port, send bytes, receive bytes, async-only" {
186186 if (!std.io.is_async) return error.SkipZigTest;
187187
188188 if (builtin.os.tag != .linux and !builtin.os.tag.isDarwin()) {
lib/std/priority_dequeue.zig+2-2
......@@ -633,7 +633,7 @@ test "std.PriorityDequeue: peekMax" {
633633 try expect(queue.peekMax().? == 9);
634634}
635635
636test "std.PriorityDequeue: sift up with odd indices" {
636test "std.PriorityDequeue: sift up with odd indices, removeMin" {
637637 var queue = PDQ.init(testing.allocator, {});
638638 defer queue.deinit();
639639 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
......@@ -647,7 +647,7 @@ test "std.PriorityDequeue: sift up with odd indices" {
647647 }
648648}
649649
650test "std.PriorityDequeue: sift up with odd indices" {
650test "std.PriorityDequeue: sift up with odd indices, removeMax" {
651651 var queue = PDQ.init(testing.allocator, {});
652652 defer queue.deinit();
653653 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
lib/std/zig/parser_test.zig+4-4
......@@ -1240,7 +1240,7 @@ test "zig fmt: infix operator and then multiline string literal" {
12401240 );
12411241}
12421242
1243test "zig fmt: infix operator and then multiline string literal" {
1243test "zig fmt: infix operator and then multiline string literal over multiple lines" {
12441244 try testCanonical(
12451245 \\const x = "" ++
12461246 \\ \\ hi0
......@@ -4310,7 +4310,7 @@ test "zig fmt: comptime before comptime field" {
43104310 });
43114311}
43124312
4313test "zig fmt: invalid else branch statement" {
4313test "zig fmt: invalid doc comments on comptime and test blocks" {
43144314 try testError(
43154315 \\/// This is a doc comment for a comptime block.
43164316 \\comptime {}
......@@ -5191,7 +5191,7 @@ test "zig fmt: preserve container doc comment in container without trailing comm
51915191 );
51925192}
51935193
5194test "zig fmt: make single-line if no trailing comma" {
5194test "zig fmt: make single-line if no trailing comma, fmt: off" {
51955195 try testCanonical(
51965196 \\// Test trailing comma syntax
51975197 \\// zig fmt: off
......@@ -5270,7 +5270,7 @@ test "zig fmt: variable initialized with ==" {
52705270 , &.{.wrong_equal_var_decl});
52715271}
52725272
5273test "zig fmt: missing const/var before local variable" {
5273test "zig fmt: missing const/var before local variable in comptime block" {
52745274 try testError(
52755275 \\comptime {
52765276 \\ z: u32;
src/Module.zig+25-1
......@@ -5280,6 +5280,9 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
52805280 }
52815281 },
52825282 };
5283 var must_free_decl_name = true;
5284 defer if (must_free_decl_name) gpa.free(decl_name);
5285
52835286 const is_exported = export_bit and decl_name_index != 0;
52845287 if (kind == .@"usingnamespace") try namespace.usingnamespace_set.ensureUnusedCapacity(gpa, 1);
52855288
......@@ -5296,6 +5299,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
52965299 const new_decl = mod.declPtr(new_decl_index);
52975300 new_decl.kind = kind;
52985301 new_decl.name = decl_name;
5302 must_free_decl_name = false;
52995303 if (kind == .@"usingnamespace") {
53005304 namespace.usingnamespace_set.putAssumeCapacity(new_decl_index, is_pub);
53015305 }
......@@ -5339,9 +5343,29 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
53395343 new_decl.alive = true; // This Decl corresponds to an AST node and therefore always alive.
53405344 return;
53415345 }
5342 gpa.free(decl_name);
53435346 const decl_index = gop.key_ptr.*;
53445347 const decl = mod.declPtr(decl_index);
5348 if (kind == .@"test") {
5349 const src_loc = SrcLoc{
5350 .file_scope = decl.getFileScope(),
5351 .parent_decl_node = decl.src_node,
5352 .lazy = .{ .token_offset = 1 },
5353 };
5354 const msg = try ErrorMsg.create(
5355 gpa,
5356 src_loc,
5357 "found test declaration with duplicate name: {s}",
5358 .{decl_name},
5359 );
5360 errdefer msg.destroy(gpa);
5361 try mod.failed_decls.putNoClobber(gpa, decl_index, msg);
5362 const other_src_loc = SrcLoc{
5363 .file_scope = namespace.file_scope,
5364 .parent_decl_node = decl_node,
5365 .lazy = .{ .token_offset = 1 },
5366 };
5367 try mod.errNoteNonLazy(other_src_loc, msg, "other test here", .{});
5368 }
53455369 log.debug("scan existing {*} ({s}) of {*}", .{ decl, decl.name, namespace });
53465370 // Update the AST node of the decl; even if its contents are unchanged, it may
53475371 // have been re-ordered.
test/behavior.zig+1
......@@ -150,6 +150,7 @@ test {
150150 _ = @import("behavior/comptime_memory.zig");
151151 _ = @import("behavior/const_slice_child.zig");
152152 _ = @import("behavior/decltest.zig");
153 _ = @import("behavior/duplicated_test_names.zig");
153154 _ = @import("behavior/defer.zig");
154155 _ = @import("behavior/empty_tuple_fields.zig");
155156 _ = @import("behavior/empty_union.zig");
test/behavior/basic.zig+1-1
......@@ -203,7 +203,7 @@ test "multiline string comments at multiple places" {
203203 try expect(mem.eql(u8, s1, s2));
204204}
205205
206test "string concatenation" {
206test "string concatenation simple" {
207207 try expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
208208}
209209
test/behavior/comptime_memory.zig+1-1
......@@ -45,7 +45,7 @@ test "type pun signed and unsigned as offset many pointer" {
4545 }
4646}
4747
48test "type pun signed and unsigned as array pointer" {
48test "type pun signed and unsigned as array pointer with pointer arithemtic" {
4949 if (true) {
5050 // TODO https://github.com/ziglang/zig/issues/9646
5151 return error.SkipZigTest;
test/behavior/duplicated_test_names.zig created+17
......@@ -0,0 +1,17 @@
1const Namespace = struct {
2 test "thingy" {}
3};
4
5fn thingy(a: usize, b: usize) usize {
6 return a + b;
7}
8
9comptime {
10 _ = Namespace;
11}
12
13test "thingy" {}
14
15test thingy {
16 if (thingy(1, 2) != 3) unreachable;
17}
test/behavior/vector.zig-14
......@@ -1129,20 +1129,6 @@ test "array of vectors is copied" {
11291129 try std.testing.expectEqual(points2[6], Vec3{ -345, -311, 381 });
11301130}
11311131
1132test "byte vector initialized in inline function" {
1133 const S = struct {
1134 inline fn boolx4(e0: bool, e1: bool, e2: bool, e3: bool) @Vector(4, bool) {
1135 return .{ e0, e1, e2, e3 };
1136 }
1137
1138 fn all(vb: @Vector(4, bool)) bool {
1139 return @reduce(.And, vb);
1140 }
1141 };
1142
1143 try expect(S.all(S.boolx4(true, true, true, true)));
1144}
1145
11461132test "byte vector initialized in inline function" {
11471133 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
11481134 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
test/cases/compile_errors/invalid_duplicate_test_decl_name.zig created+10
......@@ -0,0 +1,10 @@
1test "thingy" {}
2test "thingy" {}
3
4// error
5// backend=stage2
6// target=native
7// is_test=1
8//
9// :1:6: error: found test declaration with duplicate name: test.thingy
10// :2:6: note: other test here