authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-12 17:45:57-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-12 17:45:57-04:00
logffa700ee58cd29dafe2bbdfe78a4bd4f7bab0674
treeaffe8b6dc716051f32259ad171f93d7009855c0f
parent6e42d45dccf4ba6fa07082db1cb820897d36924f
parent0a9d6956e7cac96c870ad062b4125b0a0a3b0143
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11837 from Vexu/stage2

Fix (nearly) all stage2 crashes when testing stdlib

43 files changed, 501 insertions(+), 626 deletions(-)

ci/zinc/linux_test.sh+1
......@@ -59,6 +59,7 @@ stage2/bin/zig build -Dtarget=arm-linux-musleabihf # test building self-hosted f
5959# * https://github.com/ziglang/zig/issues/11367 (and corresponding workaround in compiler source)
6060# * https://github.com/ziglang/zig/pull/11492#issuecomment-1112871321
6161stage2/bin/zig build test-behavior -fqemu -fwasmtime
62stage2/bin/zig test lib/std/std.zig --zig-lib-dir lib
6263
6364$ZIG build test-behavior -fqemu -fwasmtime -Domit-stage2
6465$ZIG build test-compiler-rt -fqemu -fwasmtime
lib/std/bit_set.zig+4
......@@ -1330,6 +1330,7 @@ fn testStaticBitSet(comptime Set: type) !void {
13301330}
13311331
13321332test "IntegerBitSet" {
1333 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
13331334 try testStaticBitSet(IntegerBitSet(0));
13341335 try testStaticBitSet(IntegerBitSet(1));
13351336 try testStaticBitSet(IntegerBitSet(2));
......@@ -1341,6 +1342,7 @@ test "IntegerBitSet" {
13411342}
13421343
13431344test "ArrayBitSet" {
1345 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
13441346 if (@import("builtin").cpu.arch == .aarch64) {
13451347 // https://github.com/ziglang/zig/issues/9879
13461348 return error.SkipZigTest;
......@@ -1355,6 +1357,7 @@ test "ArrayBitSet" {
13551357}
13561358
13571359test "DynamicBitSetUnmanaged" {
1360 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
13581361 const allocator = std.testing.allocator;
13591362 var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);
13601363 try testing.expectEqual(@as(usize, 0), a.count());
......@@ -1395,6 +1398,7 @@ test "DynamicBitSetUnmanaged" {
13951398}
13961399
13971400test "DynamicBitSet" {
1401 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
13981402 const allocator = std.testing.allocator;
13991403 var a = try DynamicBitSet.initEmpty(allocator, 300);
14001404 try testing.expectEqual(@as(usize, 0), a.count());
lib/std/compress.zig-1
......@@ -5,7 +5,6 @@ pub const gzip = @import("compress/gzip.zig");
55pub const zlib = @import("compress/zlib.zig");
66
77test {
8 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
98 _ = deflate;
109 _ = gzip;
1110 _ = zlib;
lib/std/compress/deflate/compressor.zig+4-1
......@@ -254,7 +254,10 @@ pub fn Compressor(comptime WriterType: anytype) type {
254254
255255 // Inner writer wrapped in a HuffmanBitWriter
256256 hm_bw: hm_bw.HuffmanBitWriter(WriterType) = undefined,
257 bulk_hasher: fn ([]u8, []u32) u32,
257 bulk_hasher: if (@import("builtin").zig_backend == .stage1)
258 fn ([]u8, []u32) u32
259 else
260 *const fn ([]u8, []u32) u32,
258261
259262 sync: bool, // requesting flush
260263 best_speed_enc: *fast.DeflateFast, // Encoder for best_speed
lib/std/compress/deflate/compressor_test.zig+13-24
......@@ -122,11 +122,8 @@ fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, li
122122 try expect(compressed.items.len <= limit);
123123 }
124124
125 var decomp = try decompressor(
126 testing.allocator,
127 io.fixedBufferStream(compressed.items).reader(),
128 null,
129 );
125 var fib = io.fixedBufferStream(compressed.items);
126 var decomp = try decompressor(testing.allocator, fib.reader(), null);
130127 defer decomp.deinit();
131128
132129 var decompressed = try testing.allocator.alloc(u8, input.len);
......@@ -136,7 +133,9 @@ fn testToFromWithLevelAndLimit(level: deflate.Compression, input: []const u8, li
136133 try expect(read == input.len);
137134 try expect(mem.eql(u8, input, decompressed));
138135
139 try testSync(level, input);
136 if (builtin.zig_backend == .stage1) {
137 try testSync(level, input);
138 }
140139}
141140
142141fn testToFromWithLimit(input: []const u8, limit: [11]u32) !void {
......@@ -475,21 +474,16 @@ test "inflate reset" {
475474 try comp.close();
476475 }
477476
478 var decomp = try decompressor(
479 testing.allocator,
480 io.fixedBufferStream(compressed_strings[0].items).reader(),
481 null,
482 );
477 var fib = io.fixedBufferStream(compressed_strings[0].items);
478 var decomp = try decompressor(testing.allocator, fib.reader(), null);
483479 defer decomp.deinit();
484480
485481 var decompressed_0: []u8 = try decomp.reader()
486482 .readAllAlloc(testing.allocator, math.maxInt(usize));
487483 defer testing.allocator.free(decompressed_0);
488484
489 try decomp.reset(
490 io.fixedBufferStream(compressed_strings[1].items).reader(),
491 null,
492 );
485 fib = io.fixedBufferStream(compressed_strings[1].items);
486 try decomp.reset(fib.reader(), null);
493487
494488 var decompressed_1: []u8 = try decomp.reader()
495489 .readAllAlloc(testing.allocator, math.maxInt(usize));
......@@ -530,21 +524,16 @@ test "inflate reset dictionary" {
530524 try comp.close();
531525 }
532526
533 var decomp = try decompressor(
534 testing.allocator,
535 io.fixedBufferStream(compressed_strings[0].items).reader(),
536 dict,
537 );
527 var fib = io.fixedBufferStream(compressed_strings[0].items);
528 var decomp = try decompressor(testing.allocator, fib.reader(), dict);
538529 defer decomp.deinit();
539530
540531 var decompressed_0: []u8 = try decomp.reader()
541532 .readAllAlloc(testing.allocator, math.maxInt(usize));
542533 defer testing.allocator.free(decompressed_0);
543534
544 try decomp.reset(
545 io.fixedBufferStream(compressed_strings[1].items).reader(),
546 dict,
547 );
535 fib = io.fixedBufferStream(compressed_strings[1].items);
536 try decomp.reset(fib.reader(), dict);
548537
549538 var decompressed_1: []u8 = try decomp.reader()
550539 .readAllAlloc(testing.allocator, math.maxInt(usize));
lib/std/compress/deflate/decompressor.zig+19-6
......@@ -334,7 +334,10 @@ pub fn Decompressor(comptime ReaderType: type) type {
334334
335335 // Next step in the decompression,
336336 // and decompression state.
337 step: fn (*Self) Error!void,
337 step: if (@import("builtin").zig_backend == .stage1)
338 fn (*Self) Error!void
339 else
340 *const fn (*Self) Error!void,
338341 step_state: DecompressorState,
339342 final: bool,
340343 err: ?Error,
......@@ -479,7 +482,13 @@ pub fn Decompressor(comptime ReaderType: type) type {
479482 }
480483
481484 pub fn close(self: *Self) ?Error {
482 if (self.err == Error.EndOfStreamWithNoError) {
485 if (@import("builtin").zig_backend == .stage1) {
486 if (self.err == Error.EndOfStreamWithNoError) {
487 return null;
488 }
489 return self.err;
490 }
491 if (self.err == @as(?Error, error.EndOfStreamWithNoError)) {
483492 return null;
484493 }
485494 return self.err;
......@@ -920,7 +929,8 @@ test "truncated input" {
920929 };
921930
922931 for (tests) |t| {
923 var r = io.fixedBufferStream(t.input).reader();
932 var fib = io.fixedBufferStream(t.input);
933 const r = fib.reader();
924934 var z = try decompressor(testing.allocator, r, null);
925935 defer z.deinit();
926936 var zr = z.reader();
......@@ -959,7 +969,8 @@ test "Go non-regression test for 9842" {
959969 };
960970
961971 for (tests) |t| {
962 const reader = std.io.fixedBufferStream(t.input).reader();
972 var fib = std.io.fixedBufferStream(t.input);
973 const reader = fib.reader();
963974 var decomp = try decompressor(testing.allocator, reader, null);
964975 defer decomp.deinit();
965976
......@@ -1017,7 +1028,8 @@ test "inflate A Tale of Two Cities (1859) intro" {
10171028 \\
10181029 ;
10191030
1020 const reader = std.io.fixedBufferStream(&compressed).reader();
1031 var fib = std.io.fixedBufferStream(&compressed);
1032 const reader = fib.reader();
10211033 var decomp = try decompressor(testing.allocator, reader, null);
10221034 defer decomp.deinit();
10231035
......@@ -1082,7 +1094,8 @@ test "fuzzing" {
10821094
10831095fn decompress(input: []const u8) !void {
10841096 const allocator = testing.allocator;
1085 const reader = std.io.fixedBufferStream(input).reader();
1097 var fib = std.io.fixedBufferStream(input);
1098 const reader = fib.reader();
10861099 var decomp = try decompressor(allocator, reader, null);
10871100 defer decomp.deinit();
10881101 var output = try decomp.reader().readAllAlloc(allocator, math.maxInt(usize));
lib/std/compress/deflate/deflate_fast_test.zig+6-12
......@@ -78,11 +78,8 @@ test "best speed" {
7878 var decompressed = try testing.allocator.alloc(u8, want.items.len);
7979 defer testing.allocator.free(decompressed);
8080
81 var decomp = try inflate.decompressor(
82 testing.allocator,
83 io.fixedBufferStream(compressed.items).reader(),
84 null,
85 );
81 var fib = io.fixedBufferStream(compressed.items);
82 var decomp = try inflate.decompressor(testing.allocator, fib.reader(), null);
8683 defer decomp.deinit();
8784
8885 var read = try decomp.reader().readAll(decompressed);
......@@ -122,13 +119,13 @@ test "best speed max match offset" {
122119 // zeros1 is between 0 and 30 zeros.
123120 // The difference between the two abc's will be offset, which
124121 // is max_match_offset plus or minus a small adjustment.
125 var src_len: usize = @intCast(usize, offset + abc.len + @intCast(i32, extra));
122 var src_len: usize = @intCast(usize, offset + @as(i32, abc.len) + @intCast(i32, extra));
126123 var src = try testing.allocator.alloc(u8, src_len);
127124 defer testing.allocator.free(src);
128125
129126 mem.copy(u8, src, abc);
130127 if (!do_match_before) {
131 var src_offset: usize = @intCast(usize, offset - xyz.len);
128 var src_offset: usize = @intCast(usize, offset - @as(i32, xyz.len));
132129 mem.copy(u8, src[src_offset..], xyz);
133130 }
134131 var src_offset: usize = @intCast(usize, offset);
......@@ -149,11 +146,8 @@ test "best speed max match offset" {
149146 var decompressed = try testing.allocator.alloc(u8, src.len);
150147 defer testing.allocator.free(decompressed);
151148
152 var decomp = try inflate.decompressor(
153 testing.allocator,
154 io.fixedBufferStream(compressed.items).reader(),
155 null,
156 );
149 var fib = io.fixedBufferStream(compressed.items);
150 var decomp = try inflate.decompressor(testing.allocator, fib.reader(), null);
157151 defer decomp.deinit();
158152 var read = try decomp.reader().readAll(decompressed);
159153 _ = decomp.close();
lib/std/crypto/argon2.zig+2
......@@ -897,6 +897,7 @@ test "kdf" {
897897}
898898
899899test "phc format hasher" {
900 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
900901 const allocator = std.testing.allocator;
901902 const password = "testpass";
902903
......@@ -912,6 +913,7 @@ test "phc format hasher" {
912913}
913914
914915test "password hash and password verify" {
916 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
915917 const allocator = std.testing.allocator;
916918 const password = "testpass";
917919
lib/std/crypto/bcrypt.zig+1
......@@ -802,6 +802,7 @@ test "bcrypt crypt format" {
802802}
803803
804804test "bcrypt phc format" {
805 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
805806 const hash_options = HashOptions{
806807 .params = .{ .rounds_log = 5 },
807808 .encoding = .phc,
lib/std/crypto/phc_encoding.zig+1
......@@ -260,6 +260,7 @@ fn kvSplit(str: []const u8) !struct { key: []const u8, value: []const u8 } {
260260}
261261
262262test "phc format - encoding/decoding" {
263 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
263264 const Input = struct {
264265 str: []const u8,
265266 HashResult: type,
lib/std/crypto/scrypt.zig+1
......@@ -683,6 +683,7 @@ test "unix-scrypt" {
683683}
684684
685685test "crypt format" {
686 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
686687 const str = "$7$C6..../....SodiumChloride$kBGj9fHznVYFQMEn/qDCfrDevf9YDtcDdKvEqHJLV8D";
687688 const params = try crypt_format.deserialize(crypt_format.HashResult(32), str);
688689 var buf: [str.len]u8 = undefined;
lib/std/fmt.zig+48-22
......@@ -2111,7 +2111,6 @@ test "slice" {
21112111}
21122112
21132113test "escape non-printable" {
2114 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
21152114 try expectFmt("abc", "{s}", .{fmtSliceEscapeLower("abc")});
21162115 try expectFmt("ab\\xffc", "{s}", .{fmtSliceEscapeLower("ab\xffc")});
21172116 try expectFmt("ab\\xFFc", "{s}", .{fmtSliceEscapeUpper("ab\xffc")});
......@@ -2148,7 +2147,6 @@ test "cstr" {
21482147}
21492148
21502149test "filesize" {
2151 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
21522150 try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeDec(42)});
21532151 try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeBin(42)});
21542152 try expectFmt("file size: 63MB\n", "file size: {}\n", .{fmtIntSizeDec(63 * 1000 * 1000)});
......@@ -2192,18 +2190,22 @@ test "enum" {
21922190}
21932191
21942192test "non-exhaustive enum" {
2193 if (builtin.zig_backend == .stage1) {
2194 // stage1 fails to return fully qualified namespaces.
2195 return error.SkipZigTest;
2196 }
21952197 const Enum = enum(u16) {
21962198 One = 0x000f,
21972199 Two = 0xbeef,
21982200 _,
21992201 };
2200 try expectFmt("enum: Enum.One\n", "enum: {}\n", .{Enum.One});
2201 try expectFmt("enum: Enum.Two\n", "enum: {}\n", .{Enum.Two});
2202 try expectFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
2203 try expectFmt("enum: Enum.One\n", "enum: {x}\n", .{Enum.One});
2204 try expectFmt("enum: Enum.Two\n", "enum: {x}\n", .{Enum.Two});
2205 try expectFmt("enum: Enum.Two\n", "enum: {X}\n", .{Enum.Two});
2206 try expectFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
2202 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum.One\n", "enum: {}\n", .{Enum.One});
2203 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum.Two\n", "enum: {}\n", .{Enum.Two});
2204 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
2205 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum.One\n", "enum: {x}\n", .{Enum.One});
2206 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum.Two\n", "enum: {x}\n", .{Enum.Two});
2207 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum.Two\n", "enum: {X}\n", .{Enum.Two});
2208 try expectFmt("enum: fmt.test.non-exhaustive enum.Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
22072209}
22082210
22092211test "float.scientific" {
......@@ -2223,6 +2225,7 @@ test "float.scientific.precision" {
22232225}
22242226
22252227test "float.special" {
2228 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
22262229 try expectFmt("f64: nan", "f64: {}", .{math.nan_f64});
22272230 // negative nan is not defined by IEE 754,
22282231 // and ARM thus normalizes it to positive nan
......@@ -2234,6 +2237,7 @@ test "float.special" {
22342237}
22352238
22362239test "float.hexadecimal.special" {
2240 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
22372241 try expectFmt("f64: nan", "f64: {x}", .{math.nan_f64});
22382242 // negative nan is not defined by IEE 754,
22392243 // and ARM thus normalizes it to positive nan
......@@ -2359,6 +2363,10 @@ test "custom" {
23592363}
23602364
23612365test "struct" {
2366 if (builtin.zig_backend == .stage1) {
2367 // stage1 fails to return fully qualified namespaces.
2368 return error.SkipZigTest;
2369 }
23622370 const S = struct {
23632371 a: u32,
23642372 b: anyerror,
......@@ -2369,7 +2377,7 @@ test "struct" {
23692377 .b = error.Unused,
23702378 };
23712379
2372 try expectFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst});
2380 try expectFmt("fmt.test.struct.S{ .a = 456, .b = error.Unused }", "{}", .{inst});
23732381 // Tuples
23742382 try expectFmt("{ }", "{}", .{.{}});
23752383 try expectFmt("{ -1 }", "{}", .{.{-1}});
......@@ -2377,6 +2385,10 @@ test "struct" {
23772385}
23782386
23792387test "union" {
2388 if (builtin.zig_backend == .stage1) {
2389 // stage1 fails to return fully qualified namespaces.
2390 return error.SkipZigTest;
2391 }
23802392 const TU = union(enum) {
23812393 float: f32,
23822394 int: u32,
......@@ -2396,17 +2408,21 @@ test "union" {
23962408 const uu_inst = UU{ .int = 456 };
23972409 const eu_inst = EU{ .float = 321.123 };
23982410
2399 try expectFmt("TU{ .int = 123 }", "{}", .{tu_inst});
2411 try expectFmt("fmt.test.union.TU{ .int = 123 }", "{}", .{tu_inst});
24002412
24012413 var buf: [100]u8 = undefined;
24022414 const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst});
2403 try std.testing.expect(mem.eql(u8, uu_result[0..3], "UU@"));
2415 try std.testing.expect(mem.eql(u8, uu_result[0..18], "fmt.test.union.UU@"));
24042416
24052417 const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst});
2406 try std.testing.expect(mem.eql(u8, eu_result[0..3], "EU@"));
2418 try std.testing.expect(mem.eql(u8, eu_result[0..18], "fmt.test.union.EU@"));
24072419}
24082420
24092421test "enum" {
2422 if (builtin.zig_backend == .stage1) {
2423 // stage1 fails to return fully qualified namespaces.
2424 return error.SkipZigTest;
2425 }
24102426 const E = enum {
24112427 One,
24122428 Two,
......@@ -2415,10 +2431,14 @@ test "enum" {
24152431
24162432 const inst = E.Two;
24172433
2418 try expectFmt("E.Two", "{}", .{inst});
2434 try expectFmt("fmt.test.enum.E.Two", "{}", .{inst});
24192435}
24202436
24212437test "struct.self-referential" {
2438 if (builtin.zig_backend == .stage1) {
2439 // stage1 fails to return fully qualified namespaces.
2440 return error.SkipZigTest;
2441 }
24222442 const S = struct {
24232443 const SelfType = @This();
24242444 a: ?*SelfType,
......@@ -2429,10 +2449,14 @@ test "struct.self-referential" {
24292449 };
24302450 inst.a = &inst;
24312451
2432 try expectFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", .{inst});
2452 try expectFmt("fmt.test.struct.self-referential.S{ .a = fmt.test.struct.self-referential.S{ .a = fmt.test.struct.self-referential.S{ .a = fmt.test.struct.self-referential.S{ ... } } } }", "{}", .{inst});
24332453}
24342454
24352455test "struct.zero-size" {
2456 if (builtin.zig_backend == .stage1) {
2457 // stage1 fails to return fully qualified namespaces.
2458 return error.SkipZigTest;
2459 }
24362460 const A = struct {
24372461 fn foo() void {}
24382462 };
......@@ -2444,11 +2468,10 @@ test "struct.zero-size" {
24442468 const a = A{};
24452469 const b = B{ .a = a, .c = 0 };
24462470
2447 try expectFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b});
2471 try expectFmt("fmt.test.struct.zero-size.B{ .a = fmt.test.struct.zero-size.A{ }, .c = 0 }", "{}", .{b});
24482472}
24492473
24502474test "bytes.hex" {
2451 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
24522475 const some_bytes = "\xCA\xFE\xBA\xBE";
24532476 try expectFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{fmtSliceHexLower(some_bytes)});
24542477 try expectFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{fmtSliceHexUpper(some_bytes)});
......@@ -2480,7 +2503,6 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
24802503}
24812504
24822505test "hexToBytes" {
2483 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
24842506 var buf: [32]u8 = undefined;
24852507 try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
24862508 try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});
......@@ -2512,6 +2534,10 @@ test "formatFloatValue with comptime_float" {
25122534}
25132535
25142536test "formatType max_depth" {
2537 if (builtin.zig_backend == .stage1) {
2538 // stage1 fails to return fully qualified namespaces.
2539 return error.SkipZigTest;
2540 }
25152541 const Vec2 = struct {
25162542 const SelfType = @This();
25172543 x: f32,
......@@ -2562,19 +2588,19 @@ test "formatType max_depth" {
25622588 var buf: [1000]u8 = undefined;
25632589 var fbs = std.io.fixedBufferStream(&buf);
25642590 try formatType(inst, "", FormatOptions{}, fbs.writer(), 0);
2565 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }"));
2591 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ ... }"));
25662592
25672593 fbs.reset();
25682594 try formatType(inst, "", FormatOptions{}, fbs.writer(), 1);
2569 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));
2595 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
25702596
25712597 fbs.reset();
25722598 try formatType(inst, "", FormatOptions{}, fbs.writer(), 2);
2573 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }"));
2599 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
25742600
25752601 fbs.reset();
25762602 try formatType(inst, "", FormatOptions{}, fbs.writer(), 3);
2577 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }"));
2603 try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
25782604}
25792605
25802606test "positional" {
lib/std/io/stream_source.zig-1
......@@ -114,7 +114,6 @@ test "StreamSource (mutable buffer)" {
114114}
115115
116116test "StreamSource (const buffer)" {
117 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
118117 const buffer: [64]u8 = "Hello, World!".* ++ ([1]u8{0xAA} ** 51);
119118 var source = StreamSource{ .const_buffer = std.io.fixedBufferStream(&buffer) };
120119
lib/std/math/copysign.zig+1
......@@ -13,6 +13,7 @@ pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude)
1313}
1414
1515test "math.copysign" {
16 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
1617 inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
1718 try expect(copysign(@as(T, 1.0), @as(T, 1.0)) == 1.0);
1819 try expect(copysign(@as(T, 2.0), @as(T, -2.0)) == -2.0);
lib/std/math/signbit.zig+1
......@@ -10,6 +10,7 @@ pub fn signbit(x: anytype) bool {
1010}
1111
1212test "math.signbit" {
13 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
1314 inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
1415 try expect(!signbit(@as(T, 0.0)));
1516 try expect(!signbit(@as(T, 1.0)));
lib/std/mem.zig+1
......@@ -2080,6 +2080,7 @@ fn testReadIntImpl() !void {
20802080}
20812081
20822082test "writeIntSlice" {
2083 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
20832084 try testWriteIntImpl();
20842085 comptime try testWriteIntImpl();
20852086}
lib/std/priority_queue.zig+14
......@@ -286,6 +286,7 @@ const PQlt = PriorityQueue(u32, void, lessThan);
286286const PQgt = PriorityQueue(u32, void, greaterThan);
287287
288288test "std.PriorityQueue: add and remove min heap" {
289 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
289290 var queue = PQlt.init(testing.allocator, {});
290291 defer queue.deinit();
291292
......@@ -304,6 +305,7 @@ test "std.PriorityQueue: add and remove min heap" {
304305}
305306
306307test "std.PriorityQueue: add and remove same min heap" {
308 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
307309 var queue = PQlt.init(testing.allocator, {});
308310 defer queue.deinit();
309311
......@@ -353,6 +355,7 @@ test "std.PriorityQueue: peek" {
353355}
354356
355357test "std.PriorityQueue: sift up with odd indices" {
358 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
356359 var queue = PQlt.init(testing.allocator, {});
357360 defer queue.deinit();
358361 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
......@@ -367,6 +370,7 @@ test "std.PriorityQueue: sift up with odd indices" {
367370}
368371
369372test "std.PriorityQueue: addSlice" {
373 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
370374 var queue = PQlt.init(testing.allocator, {});
371375 defer queue.deinit();
372376 const items = [_]u32{ 15, 7, 21, 14, 13, 22, 12, 6, 7, 25, 5, 24, 11, 16, 15, 24, 2, 1 };
......@@ -412,6 +416,7 @@ test "std.PriorityQueue: fromOwnedSlice" {
412416}
413417
414418test "std.PriorityQueue: add and remove max heap" {
419 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
415420 var queue = PQgt.init(testing.allocator, {});
416421 defer queue.deinit();
417422
......@@ -430,6 +435,7 @@ test "std.PriorityQueue: add and remove max heap" {
430435}
431436
432437test "std.PriorityQueue: add and remove same max heap" {
438 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
433439 var queue = PQgt.init(testing.allocator, {});
434440 defer queue.deinit();
435441
......@@ -470,6 +476,7 @@ test "std.PriorityQueue: iterator" {
470476}
471477
472478test "std.PriorityQueue: remove at index" {
479 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
473480 var queue = PQlt.init(testing.allocator, {});
474481 defer queue.deinit();
475482
......@@ -505,6 +512,7 @@ test "std.PriorityQueue: iterator while empty" {
505512}
506513
507514test "std.PriorityQueue: shrinkAndFree" {
515 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
508516 var queue = PQlt.init(testing.allocator, {});
509517 defer queue.deinit();
510518
......@@ -528,6 +536,7 @@ test "std.PriorityQueue: shrinkAndFree" {
528536}
529537
530538test "std.PriorityQueue: update min heap" {
539 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
531540 var queue = PQlt.init(testing.allocator, {});
532541 defer queue.deinit();
533542
......@@ -543,6 +552,7 @@ test "std.PriorityQueue: update min heap" {
543552}
544553
545554test "std.PriorityQueue: update same min heap" {
555 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
546556 var queue = PQlt.init(testing.allocator, {});
547557 defer queue.deinit();
548558
......@@ -559,6 +569,7 @@ test "std.PriorityQueue: update same min heap" {
559569}
560570
561571test "std.PriorityQueue: update max heap" {
572 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
562573 var queue = PQgt.init(testing.allocator, {});
563574 defer queue.deinit();
564575
......@@ -574,6 +585,7 @@ test "std.PriorityQueue: update max heap" {
574585}
575586
576587test "std.PriorityQueue: update same max heap" {
588 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
577589 var queue = PQgt.init(testing.allocator, {});
578590 defer queue.deinit();
579591
......@@ -590,6 +602,7 @@ test "std.PriorityQueue: update same max heap" {
590602}
591603
592604test "std.PriorityQueue: siftUp in remove" {
605 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
593606 var queue = PQlt.init(testing.allocator, {});
594607 defer queue.deinit();
595608
......@@ -610,6 +623,7 @@ fn contextLessThan(context: []const u32, a: usize, b: usize) Order {
610623const CPQlt = PriorityQueue(usize, []const u32, contextLessThan);
611624
612625test "std.PriorityQueue: add and remove min heap with contextful comparator" {
626 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
613627 const context = [_]u32{ 5, 3, 4, 2, 2, 8, 0 };
614628
615629 var queue = CPQlt.init(testing.allocator, context[0..]);
lib/std/tz.zig+3
......@@ -214,6 +214,7 @@ pub const Tz = struct {
214214};
215215
216216test "slim" {
217 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
217218 const data = @embedFile("tz/asia_tokyo.tzif");
218219 var in_stream = std.io.fixedBufferStream(data);
219220
......@@ -227,6 +228,7 @@ test "slim" {
227228}
228229
229230test "fat" {
231 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
230232 const data = @embedFile("tz/antarctica_davis.tzif");
231233 var in_stream = std.io.fixedBufferStream(data);
232234
......@@ -239,6 +241,7 @@ test "fat" {
239241}
240242
241243test "legacy" {
244 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest; // TODO
242245 // Taken from Slackware 8.0, from 2001
243246 const data = @embedFile("tz/europe_vatican.tzif");
244247 var in_stream = std.io.fixedBufferStream(data);
lib/std/unicode.zig-1
......@@ -804,7 +804,6 @@ pub fn fmtUtf16le(utf16le: []const u16) std.fmt.Formatter(formatUtf16le) {
804804}
805805
806806test "fmtUtf16le" {
807 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
808807 const expectFmt = std.testing.expectFmt;
809808 try expectFmt("", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral(""))});
810809 try expectFmt("foo", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral("foo"))});
lib/std/x.zig-1
......@@ -13,7 +13,6 @@ pub const net = struct {
1313};
1414
1515test {
16 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
1716 inline for (.{ os, net }) |module| {
1817 std.testing.refAllDecls(module);
1918 }
lib/std/x/os/io.zig+1
......@@ -117,6 +117,7 @@ pub const Reactor = struct {
117117};
118118
119119test "reactor/linux: drive async tcp client/listener pair" {
120 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
120121 if (native_os.tag != .linux) return error.SkipZigTest;
121122
122123 const ip = std.x.net.ip;
lib/std/x/os/net.zig+1-1
......@@ -381,7 +381,7 @@ pub const IPv6 = extern struct {
381381 });
382382 }
383383
384 const zero_span = span: {
384 const zero_span: struct { from: usize, to: usize } = span: {
385385 var i: usize = 0;
386386 while (i < self.octets.len) : (i += 2) {
387387 if (self.octets[i] == 0 and self.octets[i + 1] == 0) break;
src/AstGen.zig+7
......@@ -2753,7 +2753,10 @@ fn varDecl(
27532753 const result_loc: ResultLoc = if (type_node != 0) .{
27542754 .ty = try typeExpr(gz, scope, type_node),
27552755 } else .none;
2756 const prev_anon_name_strategy = gz.anon_name_strategy;
2757 gz.anon_name_strategy = .dbg_var;
27562758 const init_inst = try reachableExpr(gz, scope, result_loc, var_decl.ast.init_node, node);
2759 gz.anon_name_strategy = prev_anon_name_strategy;
27572760
27582761 try gz.addDbgVar(.dbg_var_val, ident_name, init_inst);
27592762
......@@ -2777,6 +2780,7 @@ fn varDecl(
27772780 var init_scope = gz.makeSubBlock(scope);
27782781 // we may add more instructions to gz before stacking init_scope
27792782 init_scope.instructions_top = GenZir.unstacked_top;
2783 init_scope.anon_name_strategy = .dbg_var;
27802784 defer init_scope.unstack();
27812785
27822786 var resolve_inferred_alloc: Zir.Inst.Ref = .none;
......@@ -2956,7 +2960,10 @@ fn varDecl(
29562960 resolve_inferred_alloc = alloc;
29572961 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } };
29582962 };
2963 const prev_anon_name_strategy = gz.anon_name_strategy;
2964 gz.anon_name_strategy = .dbg_var;
29592965 _ = try reachableExprComptime(gz, scope, var_data.result_loc, var_decl.ast.init_node, node, is_comptime);
2966 gz.anon_name_strategy = prev_anon_name_strategy;
29602967 if (resolve_inferred_alloc != .none) {
29612968 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
29622969 }
src/Module.zig+5-2
......@@ -3790,9 +3790,12 @@ pub fn ensureFuncBodyAnalyzed(mod: *Module, func: *Fn) SemaError!void {
37903790 defer liveness.deinit(gpa);
37913791
37923792 if (builtin.mode == .Debug and mod.comp.verbose_air) {
3793 std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});
3793 const fqn = try decl.getFullyQualifiedName(mod);
3794 defer mod.gpa.free(fqn);
3795
3796 std.debug.print("# Begin Function AIR: {s}:\n", .{fqn});
37943797 @import("print_air.zig").dump(mod, air, liveness);
3795 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
3798 std.debug.print("# End Function AIR: {s}\n\n", .{fqn});
37963799 }
37973800
37983801 mod.comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) {
src/Sema.zig+82-60
......@@ -914,9 +914,9 @@ fn analyzeBodyInner(
914914 // zig fmt: off
915915 .variable => try sema.zirVarExtended( block, extended),
916916 .struct_decl => try sema.zirStructDecl( block, extended, inst),
917 .enum_decl => try sema.zirEnumDecl( block, extended),
917 .enum_decl => try sema.zirEnumDecl( block, extended, inst),
918918 .union_decl => try sema.zirUnionDecl( block, extended, inst),
919 .opaque_decl => try sema.zirOpaqueDecl( block, extended),
919 .opaque_decl => try sema.zirOpaqueDecl( block, extended, inst),
920920 .this => try sema.zirThis( block, extended),
921921 .ret_addr => try sema.zirRetAddr( block, extended),
922922 .builtin_src => try sema.zirBuiltinSrc( block, extended),
......@@ -2101,7 +2101,7 @@ fn zirStructDecl(
21012101 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
21022102 .ty = Type.type,
21032103 .val = struct_val,
2104 }, small.name_strategy, "struct");
2104 }, small.name_strategy, "struct", inst);
21052105 const new_decl = mod.declPtr(new_decl_index);
21062106 new_decl.owns_tv = true;
21072107 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -2133,6 +2133,7 @@ fn createAnonymousDeclTypeNamed(
21332133 typed_value: TypedValue,
21342134 name_strategy: Zir.Inst.NameStrategy,
21352135 anon_prefix: []const u8,
2136 inst: ?Zir.Inst.Index,
21362137) !Decl.Index {
21372138 const mod = sema.mod;
21382139 const namespace = block.namespace;
......@@ -2152,11 +2153,13 @@ fn createAnonymousDeclTypeNamed(
21522153 const name = try std.fmt.allocPrintZ(sema.gpa, "{s}__{s}_{d}", .{
21532154 src_decl.name, anon_prefix, @enumToInt(new_decl_index),
21542155 });
2156 errdefer sema.gpa.free(name);
21552157 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name);
21562158 return new_decl_index;
21572159 },
21582160 .parent => {
21592161 const name = try sema.gpa.dupeZ(u8, mem.sliceTo(sema.mod.declPtr(block.src_decl).name, 0));
2162 errdefer sema.gpa.free(name);
21602163 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name);
21612164 return new_decl_index;
21622165 },
......@@ -2188,9 +2191,31 @@ fn createAnonymousDeclTypeNamed(
21882191
21892192 try buf.appendSlice(")");
21902193 const name = try buf.toOwnedSliceSentinel(0);
2194 errdefer sema.gpa.free(name);
21912195 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name);
21922196 return new_decl_index;
21932197 },
2198 .dbg_var => {
2199 const ref = Zir.indexToRef(inst.?);
2200 const zir_tags = sema.code.instructions.items(.tag);
2201 const zir_data = sema.code.instructions.items(.data);
2202 var i = inst.?;
2203 while (i < zir_tags.len) : (i += 1) switch (zir_tags[i]) {
2204 .dbg_var_ptr, .dbg_var_val => {
2205 if (zir_data[i].str_op.operand != ref) continue;
2206
2207 const name = try std.fmt.allocPrintZ(sema.gpa, "{s}.{s}", .{
2208 src_decl.name, zir_data[i].str_op.getStr(sema.code),
2209 });
2210 errdefer sema.gpa.free(name);
2211
2212 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name);
2213 return new_decl_index;
2214 },
2215 else => {},
2216 };
2217 return sema.createAnonymousDeclTypeNamed(block, typed_value, .anon, anon_prefix, null);
2218 },
21942219 }
21952220}
21962221
......@@ -2198,6 +2223,7 @@ fn zirEnumDecl(
21982223 sema: *Sema,
21992224 block: *Block,
22002225 extended: Zir.Inst.Extended.InstData,
2226 inst: Zir.Inst.Index,
22012227) CompileError!Air.Inst.Ref {
22022228 const tracy = trace(@src());
22032229 defer tracy.end();
......@@ -2252,7 +2278,7 @@ fn zirEnumDecl(
22522278 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
22532279 .ty = Type.type,
22542280 .val = enum_val,
2255 }, small.name_strategy, "enum");
2281 }, small.name_strategy, "enum", inst);
22562282 const new_decl = mod.declPtr(new_decl_index);
22572283 new_decl.owns_tv = true;
22582284 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -2472,7 +2498,7 @@ fn zirUnionDecl(
24722498 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
24732499 .ty = Type.type,
24742500 .val = union_val,
2475 }, small.name_strategy, "union");
2501 }, small.name_strategy, "union", inst);
24762502 const new_decl = mod.declPtr(new_decl_index);
24772503 new_decl.owns_tv = true;
24782504 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -2504,6 +2530,7 @@ fn zirOpaqueDecl(
25042530 sema: *Sema,
25052531 block: *Block,
25062532 extended: Zir.Inst.Extended.InstData,
2533 inst: Zir.Inst.Index,
25072534) CompileError!Air.Inst.Ref {
25082535 const tracy = trace(@src());
25092536 defer tracy.end();
......@@ -2540,7 +2567,7 @@ fn zirOpaqueDecl(
25402567 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
25412568 .ty = Type.type,
25422569 .val = opaque_val,
2543 }, small.name_strategy, "opaque");
2570 }, small.name_strategy, "opaque", inst);
25442571 const new_decl = mod.declPtr(new_decl_index);
25452572 new_decl.owns_tv = true;
25462573 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -2589,7 +2616,7 @@ fn zirErrorSetDecl(
25892616 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
25902617 .ty = Type.type,
25912618 .val = error_set_val,
2592 }, name_strategy, "error");
2619 }, name_strategy, "error", inst);
25932620 const new_decl = mod.declPtr(new_decl_index);
25942621 new_decl.owns_tv = true;
25952622 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -4967,6 +4994,8 @@ fn lookupInNamespace(
49674994 var it = check_ns.usingnamespace_set.iterator();
49684995 while (it.next()) |entry| {
49694996 const sub_usingnamespace_decl_index = entry.key_ptr.*;
4997 // Skip the decl we're currently analysing.
4998 if (sub_usingnamespace_decl_index == sema.owner_decl_index) continue;
49704999 const sub_usingnamespace_decl = mod.declPtr(sub_usingnamespace_decl_index);
49715000 const sub_is_pub = entry.value_ptr.*;
49725001 if (!sub_is_pub and src_file != sub_usingnamespace_decl.getFileScope()) {
......@@ -6180,6 +6209,17 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
61806209 }
61816210 }
61826211
6212 const op_ty = sema.typeOf(op);
6213 try sema.resolveInferredErrorSetTy(block, src, op_ty);
6214 if (!op_ty.isAnyError()) {
6215 const names = op_ty.errorSetNames();
6216 switch (names.len) {
6217 0 => return sema.addConstant(result_ty, Value.zero),
6218 1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
6219 else => {},
6220 }
6221 }
6222
61836223 try sema.requireRuntimeBlock(block, src);
61846224 return block.addBitCast(result_ty, op_coerced);
61856225}
......@@ -6558,7 +6598,7 @@ fn analyzeErrUnionPayload(
65586598
65596599 // If the error set has no fields then no safety check is needed.
65606600 if (safety_check and block.wantSafety() and
6561 err_union_ty.errorUnionSet().errorSetCardinality() != .zero)
6601 !err_union_ty.errorUnionSet().errorSetIsEmpty())
65626602 {
65636603 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
65646604 }
......@@ -6644,7 +6684,7 @@ fn analyzeErrUnionPayloadPtr(
66446684
66456685 // If the error set has no fields then no safety check is needed.
66466686 if (safety_check and block.wantSafety() and
6647 err_union_ty.errorUnionSet().errorSetCardinality() != .zero)
6687 !err_union_ty.errorUnionSet().errorSetIsEmpty())
66486688 {
66496689 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr);
66506690 }
......@@ -11859,10 +11899,14 @@ fn zirBuiltinSrc(
1185911899 const file_name_val = blk: {
1186011900 var anon_decl = try block.startAnonDecl(src);
1186111901 defer anon_decl.deinit();
11862 const name = try fn_owner_decl.getFileScope().fullPathZ(anon_decl.arena());
11902 const relative_path = try fn_owner_decl.getFileScope().fullPath(sema.arena);
11903 const absolute_path = std.fs.realpathAlloc(sema.arena, relative_path) catch |err| {
11904 return sema.fail(block, src, "failed to get absolute path of file '{s}': {s}", .{ relative_path, @errorName(err) });
11905 };
11906 const aboslute_duped = try anon_decl.arena().dupeZ(u8, absolute_path);
1186311907 const new_decl = try anon_decl.finish(
11864 try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), name.len),
11865 try Value.Tag.bytes.create(anon_decl.arena(), name[0 .. name.len + 1]),
11908 try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), aboslute_duped.len),
11909 try Value.Tag.bytes.create(anon_decl.arena(), aboslute_duped[0 .. aboslute_duped.len + 1]),
1186611910 0, // default alignment
1186711911 );
1186811912 break :blk try Value.Tag.decl_ref.create(sema.arena, new_decl);
......@@ -11873,6 +11917,7 @@ fn zirBuiltinSrc(
1187311917 field_values[0] = file_name_val;
1187411918 // fn_name: [:0]const u8,
1187511919 field_values[1] = func_name_val;
11920 // TODO these should be runtime only!
1187611921 // line: u32
1187711922 field_values[2] = try Value.Tag.int_u64.create(sema.arena, extra.line + 1);
1187811923 // column: u32,
......@@ -13712,10 +13757,10 @@ fn zirStructInit(
1371213757 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
1371313758 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
1371413759 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);
13760 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
1371513761
1371613762 const init_inst = try sema.resolveInst(item.data.init);
1371713763 if (try sema.resolveMaybeUndefVal(block, field_src, init_inst)) |val| {
13718 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
1371913764 return sema.addConstantMaybeRef(
1372013765 block,
1372113766 src,
......@@ -13734,6 +13779,8 @@ fn zirStructInit(
1373413779 const alloc = try block.addTy(.alloc, alloc_ty);
1373513780 const field_ptr = try sema.unionFieldPtr(block, field_src, alloc, field_name, field_src, resolved_ty);
1373613781 try sema.storePtr(block, src, field_ptr, init_inst);
13782 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(), tag_val);
13783 _ = try block.addBinOp(.set_union_tag, alloc, new_tag);
1373713784 return alloc;
1373813785 }
1373913786
......@@ -14614,7 +14661,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1461414661 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1461514662 .ty = Type.type,
1461614663 .val = enum_val,
14617 }, .anon, "enum");
14664 }, .anon, "enum", null);
1461814665 const new_decl = mod.declPtr(new_decl_index);
1461914666 new_decl.owns_tv = true;
1462014667 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -14704,7 +14751,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1470414751 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1470514752 .ty = Type.type,
1470614753 .val = opaque_val,
14707 }, .anon, "opaque");
14754 }, .anon, "opaque", null);
1470814755 const new_decl = mod.declPtr(new_decl_index);
1470914756 new_decl.owns_tv = true;
1471014757 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -14755,7 +14802,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1475514802 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1475614803 .ty = Type.type,
1475714804 .val = new_union_val,
14758 }, .anon, "union");
14805 }, .anon, "union", null);
1475914806 const new_decl = mod.declPtr(new_decl_index);
1476014807 new_decl.owns_tv = true;
1476114808 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -14923,7 +14970,7 @@ fn reifyStruct(
1492314970 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1492414971 .ty = Type.type,
1492514972 .val = new_struct_val,
14926 }, .anon, "struct");
14973 }, .anon, "struct", null);
1492714974 const new_decl = mod.declPtr(new_decl_index);
1492814975 new_decl.owns_tv = true;
1492914976 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -19700,7 +19747,8 @@ fn coerce(
1970019747 // pointer to tuple to slice
1970119748 if (inst_ty.isSinglePointer() and
1970219749 inst_ty.childType().isTuple() and
19703 !dest_info.mutable and dest_info.size == .Slice)
19750 (!dest_info.mutable or inst_ty.ptrIsMutable() or inst_ty.childType().tupleFields().types.len == 0) and
19751 dest_info.size == .Slice)
1970419752 {
1970519753 return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src);
1970619754 }
......@@ -23540,7 +23588,17 @@ pub fn resolveTypeFully(
2354023588 const child_ty = try sema.resolveTypeFields(block, src, ty.childType());
2354123589 return resolveTypeFully(sema, block, src, child_ty);
2354223590 },
23543 .Struct => return resolveStructFully(sema, block, src, ty),
23591 .Struct => switch (ty.tag()) {
23592 .@"struct" => return resolveStructFully(sema, block, src, ty),
23593 .tuple, .anon_struct => {
23594 const tuple = ty.tupleFields();
23595
23596 for (tuple.types) |field_ty| {
23597 try sema.resolveTypeFully(block, src, field_ty);
23598 }
23599 },
23600 else => {},
23601 },
2354423602 .Union => return resolveUnionFully(sema, block, src, ty),
2354523603 .Array => return resolveTypeFully(sema, block, src, ty.childType()),
2354623604 .Optional => {
......@@ -23575,7 +23633,7 @@ fn resolveStructFully(
2357523633 try resolveStructLayout(sema, block, src, ty);
2357623634
2357723635 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
23578 const payload = resolved_ty.castTag(.@"struct") orelse return;
23636 const payload = resolved_ty.castTag(.@"struct").?;
2357923637 const struct_obj = payload.data;
2358023638
2358123639 switch (struct_obj.status) {
......@@ -24425,6 +24483,10 @@ pub fn typeHasOnePossibleValue(
2442524483 .bool,
2442624484 .type,
2442724485 .anyerror,
24486 .error_set_single,
24487 .error_set,
24488 .error_set_merged,
24489 .error_union,
2442824490 .fn_noreturn_no_args,
2442924491 .fn_void_no_args,
2443024492 .fn_naked_noreturn_no_args,
......@@ -24481,46 +24543,6 @@ pub fn typeHasOnePossibleValue(
2448124543 }
2448224544 },
2448324545
24484 .error_union => {
24485 const error_ty = ty.errorUnionSet();
24486 switch (error_ty.errorSetCardinality()) {
24487 .zero => {
24488 const payload_ty = ty.errorUnionPayload();
24489 if (try typeHasOnePossibleValue(sema, block, src, payload_ty)) |payload_val| {
24490 return try Value.Tag.eu_payload.create(sema.arena, payload_val);
24491 } else {
24492 return null;
24493 }
24494 },
24495 .one => {
24496 if (ty.errorUnionPayload().isNoReturn()) {
24497 const error_val = (try typeHasOnePossibleValue(sema, block, src, error_ty)).?;
24498 return error_val;
24499 } else {
24500 return null;
24501 }
24502 },
24503 .many => return null,
24504 }
24505 },
24506
24507 .error_set_single => {
24508 const name = ty.castTag(.error_set_single).?.data;
24509 return try Value.Tag.@"error".create(sema.arena, .{ .name = name });
24510 },
24511 .error_set => {
24512 const err_set_obj = ty.castTag(.error_set).?.data;
24513 const names = err_set_obj.names.keys();
24514 if (names.len > 1) return null;
24515 return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] });
24516 },
24517 .error_set_merged => {
24518 const name_map = ty.castTag(.error_set_merged).?.data;
24519 const names = name_map.keys();
24520 if (names.len > 1) return null;
24521 return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] });
24522 },
24523
2452424546 .@"struct" => {
2452524547 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
2452624548 const s = resolved_ty.castTag(.@"struct").?.data;
src/TypedValue.zig+64-11
......@@ -144,7 +144,41 @@ pub fn print(
144144 return writer.writeAll(".{ ... }");
145145 }
146146 const vals = val.castTag(.aggregate).?.data;
147 if (ty.zigTypeTag() == .Struct) {
147 if (ty.castTag(.anon_struct)) |anon_struct| {
148 const field_names = anon_struct.data.names;
149 const types = anon_struct.data.types;
150 const max_len = std.math.min(types.len, max_aggregate_items);
151
152 var i: u32 = 0;
153 while (i < max_len) : (i += 1) {
154 if (i != 0) try writer.writeAll(", ");
155 try writer.print(".{s} = ", .{field_names[i]});
156 try print(.{
157 .ty = types[i],
158 .val = vals[i],
159 }, writer, level - 1, mod);
160 }
161 if (types.len > max_aggregate_items) {
162 try writer.writeAll(", ...");
163 }
164 return writer.writeAll(" }");
165 } else if (ty.isTuple()) {
166 const fields = ty.tupleFields();
167 const max_len = std.math.min(fields.types.len, max_aggregate_items);
168
169 var i: u32 = 0;
170 while (i < max_len) : (i += 1) {
171 if (i != 0) try writer.writeAll(", ");
172 try print(.{
173 .ty = fields.types[i],
174 .val = vals[i],
175 }, writer, level - 1, mod);
176 }
177 if (fields.types.len > max_aggregate_items) {
178 try writer.writeAll(", ...");
179 }
180 return writer.writeAll(" }");
181 } else if (ty.zigTypeTag() == .Struct) {
148182 try writer.writeAll(".{ ");
149183 const struct_fields = ty.structFields();
150184 const len = struct_fields.count();
......@@ -194,7 +228,7 @@ pub fn print(
194228 try writer.writeAll(".{ ");
195229
196230 try print(.{
197 .ty = ty.unionTagType().?,
231 .ty = ty.cast(Type.Payload.Union).?.data.tag_ty,
198232 .val = union_val.tag,
199233 }, writer, level - 1, mod);
200234 try writer.writeAll(" = ");
......@@ -278,19 +312,27 @@ pub fn print(
278312 .elem_ptr => {
279313 const elem_ptr = val.castTag(.elem_ptr).?.data;
280314 try writer.writeAll("&");
281 try print(.{
282 .ty = elem_ptr.elem_ty,
283 .val = elem_ptr.array_ptr,
284 }, writer, level - 1, mod);
315 if (level == 0) {
316 try writer.writeAll("(ptr)");
317 } else {
318 try print(.{
319 .ty = elem_ptr.elem_ty,
320 .val = elem_ptr.array_ptr,
321 }, writer, level - 1, mod);
322 }
285323 return writer.print("[{}]", .{elem_ptr.index});
286324 },
287325 .field_ptr => {
288326 const field_ptr = val.castTag(.field_ptr).?.data;
289327 try writer.writeAll("&");
290 try print(.{
291 .ty = field_ptr.container_ty,
292 .val = field_ptr.container_ptr,
293 }, writer, level - 1, mod);
328 if (level == 0) {
329 try writer.writeAll("(ptr)");
330 } else {
331 try print(.{
332 .ty = field_ptr.container_ty,
333 .val = field_ptr.container_ptr,
334 }, writer, level - 1, mod);
335 }
294336
295337 if (field_ptr.container_ty.zigTypeTag() == .Struct) {
296338 const field_name = field_ptr.container_ty.structFields().keys()[field_ptr.field_index];
......@@ -344,6 +386,9 @@ pub fn print(
344386 return writer.writeAll(" }");
345387 },
346388 .slice => {
389 if (level == 0) {
390 return writer.writeAll(".{ ... }");
391 }
347392 const payload = val.castTag(.slice).?.data;
348393 try writer.writeAll(".{ ");
349394 const elem_ty = ty.elemType2();
......@@ -372,17 +417,25 @@ pub fn print(
372417 .@"error" => return writer.print("error.{s}", .{val.castTag(.@"error").?.data.name}),
373418 .eu_payload => {
374419 val = val.castTag(.eu_payload).?.data;
420 ty = ty.errorUnionPayload();
375421 },
376422 .opt_payload => {
377423 val = val.castTag(.opt_payload).?.data;
424 var buf: Type.Payload.ElemType = undefined;
425 ty = ty.optionalChild(&buf);
426 return print(.{ .ty = ty, .val = val }, writer, level, mod);
378427 },
379428 .eu_payload_ptr => {
380429 try writer.writeAll("&");
381430 val = val.castTag(.eu_payload_ptr).?.data.container_ptr;
431 ty = ty.elemType2().errorUnionPayload();
382432 },
383433 .opt_payload_ptr => {
384434 try writer.writeAll("&");
385 val = val.castTag(.opt_payload_ptr).?.data.container_ptr;
435 val = val.castTag(.opt_payload).?.data;
436 var buf: Type.Payload.ElemType = undefined;
437 ty = ty.elemType2().optionalChild(&buf);
438 return print(.{ .ty = ty, .val = val }, writer, level, mod);
386439 },
387440
388441 // TODO these should not appear in this function
src/Zir.zig+2
......@@ -3156,6 +3156,8 @@ pub const Inst = struct {
31563156 /// Create an anonymous name for this declaration.
31573157 /// Like this: "ParentDeclName_struct_69"
31583158 anon,
3159 /// Use the name specified in the next `dbg_var_{val,ptr}` instruction.
3160 dbg_var,
31593161 };
31603162
31613163 /// Trailing:
src/arch/aarch64/CodeGen.zig+3-8
......@@ -2277,7 +2277,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
22772277fn errUnionErr(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
22782278 const err_ty = error_union_ty.errorUnionSet();
22792279 const payload_ty = error_union_ty.errorUnionPayload();
2280 if (err_ty.errorSetCardinality() == .zero) {
2280 if (err_ty.errorSetIsEmpty()) {
22812281 return MCValue{ .immediate = 0 };
22822282 }
22832283 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -2311,7 +2311,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
23112311fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
23122312 const err_ty = error_union_ty.errorUnionSet();
23132313 const payload_ty = error_union_ty.errorUnionPayload();
2314 if (err_ty.errorSetCardinality() == .zero) {
2314 if (err_ty.errorSetIsEmpty()) {
23152315 return error_union_mcv;
23162316 }
23172317 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -3590,7 +3590,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
35903590 const error_type = ty.errorUnionSet();
35913591 const payload_type = ty.errorUnionPayload();
35923592
3593 if (error_type.errorSetCardinality() == .zero) {
3593 if (error_type.errorSetIsEmpty()) {
35943594 return MCValue{ .immediate = 0 }; // always false
35953595 }
35963596
......@@ -4687,11 +4687,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
46874687 const error_type = typed_value.ty.errorUnionSet();
46884688 const payload_type = typed_value.ty.errorUnionPayload();
46894689
4690 if (error_type.errorSetCardinality() == .zero) {
4691 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
4692 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
4693 }
4694
46954690 const is_pl = typed_value.val.errorUnionIsPayload();
46964691
46974692 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/arch/arm/CodeGen.zig+3-9
......@@ -1773,7 +1773,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void {
17731773fn errUnionErr(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
17741774 const err_ty = error_union_ty.errorUnionSet();
17751775 const payload_ty = error_union_ty.errorUnionPayload();
1776 if (err_ty.errorSetCardinality() == .zero) {
1776 if (err_ty.errorSetIsEmpty()) {
17771777 return MCValue{ .immediate = 0 };
17781778 }
17791779 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -1810,7 +1810,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
18101810fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
18111811 const err_ty = error_union_ty.errorUnionSet();
18121812 const payload_ty = error_union_ty.errorUnionPayload();
1813 if (err_ty.errorSetCardinality() == .zero) {
1813 if (err_ty.errorSetIsEmpty()) {
18141814 return error_union_mcv;
18151815 }
18161816 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -3922,7 +3922,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
39223922 const error_type = ty.errorUnionSet();
39233923 const error_int_type = Type.initTag(.u16);
39243924
3925 if (error_type.errorSetCardinality() == .zero) {
3925 if (error_type.errorSetIsEmpty()) {
39263926 return MCValue{ .immediate = 0 }; // always false
39273927 }
39283928
......@@ -5368,12 +5368,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
53685368 .ErrorUnion => {
53695369 const error_type = typed_value.ty.errorUnionSet();
53705370 const payload_type = typed_value.ty.errorUnionPayload();
5371
5372 if (error_type.errorSetCardinality() == .zero) {
5373 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
5374 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
5375 }
5376
53775371 const is_pl = typed_value.val.errorUnionIsPayload();
53785372
53795373 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/arch/wasm/CodeGen.zig+19-41
......@@ -1377,11 +1377,7 @@ fn isByRef(ty: Type, target: std.Target) bool {
13771377 .Int => return ty.intInfo(target).bits > 64,
13781378 .Float => return ty.floatBits(target) > 64,
13791379 .ErrorUnion => {
1380 const err_ty = ty.errorUnionSet();
13811380 const pl_ty = ty.errorUnionPayload();
1382 if (err_ty.errorSetCardinality() == .zero) {
1383 return isByRef(pl_ty, target);
1384 }
13851381 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
13861382 return false;
13871383 }
......@@ -1817,11 +1813,7 @@ fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
18171813fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void {
18181814 switch (ty.zigTypeTag()) {
18191815 .ErrorUnion => {
1820 const err_ty = ty.errorUnionSet();
18211816 const pl_ty = ty.errorUnionPayload();
1822 if (err_ty.errorSetCardinality() == .zero) {
1823 return self.store(lhs, rhs, pl_ty, 0);
1824 }
18251817 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
18261818 return self.store(lhs, rhs, Type.anyerror, 0);
18271819 }
......@@ -2357,10 +2349,6 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue {
23572349 },
23582350 .ErrorUnion => {
23592351 const error_type = ty.errorUnionSet();
2360 if (error_type.errorSetCardinality() == .zero) {
2361 const pl_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef);
2362 return self.lowerConstant(pl_val, ty.errorUnionPayload());
2363 }
23642352 const is_pl = val.errorUnionIsPayload();
23652353 const err_val = if (!is_pl) val else Value.initTag(.zero);
23662354 return self.lowerConstant(err_val, error_type);
......@@ -2929,7 +2917,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W
29292917 const err_union_ty = self.air.typeOf(un_op);
29302918 const pl_ty = err_union_ty.errorUnionPayload();
29312919
2932 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
2920 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
29332921 switch (opcode) {
29342922 .i32_ne => return WValue{ .imm32 = 0 },
29352923 .i32_eq => return WValue{ .imm32 = 1 },
......@@ -2962,10 +2950,6 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool)
29622950 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
29632951 const payload_ty = err_ty.errorUnionPayload();
29642952
2965 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
2966 return operand;
2967 }
2968
29692953 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
29702954
29712955 const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target));
......@@ -2984,7 +2968,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In
29842968 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
29852969 const payload_ty = err_ty.errorUnionPayload();
29862970
2987 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
2971 if (err_ty.errorUnionSet().errorSetIsEmpty()) {
29882972 return WValue{ .imm32 = 0 };
29892973 }
29902974
......@@ -3002,10 +2986,6 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
30022986 const operand = try self.resolveInst(ty_op.operand);
30032987 const err_ty = self.air.typeOfIndex(inst);
30042988
3005 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
3006 return operand;
3007 }
3008
30092989 const pl_ty = self.air.typeOf(ty_op.operand);
30102990 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
30112991 return operand;
......@@ -4633,29 +4613,27 @@ fn lowerTry(
46334613 return self.fail("TODO: lowerTry for pointers", .{});
46344614 }
46354615
4636 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
4637 return err_union;
4638 }
4639
46404616 const pl_ty = err_union_ty.errorUnionPayload();
46414617 const pl_has_bits = pl_ty.hasRuntimeBitsIgnoreComptime();
46424618
4643 // Block we can jump out of when error is not set
4644 try self.startBlock(.block, wasm.block_empty);
4645
4646 // check if the error tag is set for the error union.
4647 try self.emitWValue(err_union);
4648 if (pl_has_bits) {
4649 const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
4650 try self.addMemArg(.i32_load16_u, .{
4651 .offset = err_union.offset() + err_offset,
4652 .alignment = Type.anyerror.abiAlignment(self.target),
4653 });
4619 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
4620 // Block we can jump out of when error is not set
4621 try self.startBlock(.block, wasm.block_empty);
4622
4623 // check if the error tag is set for the error union.
4624 try self.emitWValue(err_union);
4625 if (pl_has_bits) {
4626 const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
4627 try self.addMemArg(.i32_load16_u, .{
4628 .offset = err_union.offset() + err_offset,
4629 .alignment = Type.anyerror.abiAlignment(self.target),
4630 });
4631 }
4632 try self.addTag(.i32_eqz);
4633 try self.addLabel(.br_if, 0); // jump out of block when error is '0'
4634 try self.genBody(body);
4635 try self.endBlock();
46544636 }
4655 try self.addTag(.i32_eqz);
4656 try self.addLabel(.br_if, 0); // jump out of block when error is '0'
4657 try self.genBody(body);
4658 try self.endBlock();
46594637
46604638 // if we reach here it means error was not set, and we want the payload
46614639 if (!pl_has_bits) {
src/arch/x86_64/CodeGen.zig+2-19
......@@ -1806,7 +1806,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
18061806 const operand = try self.resolveInst(ty_op.operand);
18071807
18081808 const result: MCValue = result: {
1809 if (err_ty.errorSetCardinality() == .zero) {
1809 if (err_ty.errorSetIsEmpty()) {
18101810 break :result MCValue{ .immediate = 0 };
18111811 }
18121812
......@@ -1857,14 +1857,8 @@ fn genUnwrapErrorUnionPayloadMir(
18571857 err_union: MCValue,
18581858) !MCValue {
18591859 const payload_ty = err_union_ty.errorUnionPayload();
1860 const err_ty = err_union_ty.errorUnionSet();
18611860
18621861 const result: MCValue = result: {
1863 if (err_ty.errorSetCardinality() == .zero) {
1864 // TODO check if we can reuse
1865 break :result err_union;
1866 }
1867
18681862 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
18691863 break :result MCValue.none;
18701864 }
......@@ -1991,15 +1985,10 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void {
19911985 }
19921986
19931987 const error_union_ty = self.air.getRefType(ty_op.ty);
1994 const error_ty = error_union_ty.errorUnionSet();
19951988 const payload_ty = error_union_ty.errorUnionPayload();
19961989 const operand = try self.resolveInst(ty_op.operand);
19971990
19981991 const result: MCValue = result: {
1999 if (error_ty.errorSetCardinality() == .zero) {
2000 break :result operand;
2001 }
2002
20031992 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
20041993 break :result operand;
20051994 }
......@@ -4651,7 +4640,7 @@ fn isNonNull(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCV
46514640fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, ty: Type, operand: MCValue) !MCValue {
46524641 const err_type = ty.errorUnionSet();
46534642
4654 if (err_type.errorSetCardinality() == .zero) {
4643 if (err_type.errorSetIsEmpty()) {
46554644 return MCValue{ .immediate = 0 }; // always false
46564645 }
46574646
......@@ -6909,12 +6898,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
69096898 .ErrorUnion => {
69106899 const error_type = typed_value.ty.errorUnionSet();
69116900 const payload_type = typed_value.ty.errorUnionPayload();
6912
6913 if (error_type.errorSetCardinality() == .zero) {
6914 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
6915 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
6916 }
6917
69186901 const is_pl = typed_value.val.errorUnionIsPayload();
69196902
69206903 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/codegen.zig-9
......@@ -705,15 +705,6 @@ pub fn generateSymbol(
705705 .ErrorUnion => {
706706 const error_ty = typed_value.ty.errorUnionSet();
707707 const payload_ty = typed_value.ty.errorUnionPayload();
708
709 if (error_ty.errorSetCardinality() == .zero) {
710 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
711 return generateSymbol(bin_file, src_loc, .{
712 .ty = payload_ty,
713 .val = payload_val,
714 }, code, debug_output, reloc_info);
715 }
716
717708 const is_payload = typed_value.val.errorUnionIsPayload();
718709
719710 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
src/codegen/c.zig+24-52
......@@ -752,12 +752,6 @@ pub const DeclGen = struct {
752752 const error_type = ty.errorUnionSet();
753753 const payload_type = ty.errorUnionPayload();
754754
755 if (error_type.errorSetCardinality() == .zero) {
756 // We use the payload directly as the type.
757 const payload_val = val.castTag(.eu_payload).?.data;
758 return dg.renderValue(writer, payload_type, payload_val, location);
759 }
760
761755 if (!payload_type.hasRuntimeBits()) {
762756 // We use the error type directly as the type.
763757 const err_val = if (val.errorUnionIsPayload()) Value.initTag(.zero) else val;
......@@ -1381,13 +1375,8 @@ pub const DeclGen = struct {
13811375 return w.writeAll("uint16_t");
13821376 },
13831377 .ErrorUnion => {
1384 const error_ty = t.errorUnionSet();
13851378 const payload_ty = t.errorUnionPayload();
13861379
1387 if (error_ty.errorSetCardinality() == .zero) {
1388 return dg.renderType(w, payload_ty);
1389 }
1390
13911380 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
13921381 return dg.renderType(w, Type.anyerror);
13931382 }
......@@ -2892,41 +2881,36 @@ fn lowerTry(
28922881 operand_is_ptr: bool,
28932882 result_ty: Type,
28942883) !CValue {
2895 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
2896 // If the error set has no fields, then the payload and the error
2897 // union are the same value.
2898 return err_union;
2899 }
2900
2884 const writer = f.object.writer();
29012885 const payload_ty = err_union_ty.errorUnionPayload();
29022886 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime();
29032887
2904 const writer = f.object.writer();
2905
2906 err: {
2907 if (!payload_has_bits) {
2908 if (operand_is_ptr) {
2909 try writer.writeAll("if(*");
2910 } else {
2888 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
2889 err: {
2890 if (!payload_has_bits) {
2891 if (operand_is_ptr) {
2892 try writer.writeAll("if(*");
2893 } else {
2894 try writer.writeAll("if(");
2895 }
2896 try f.writeCValue(writer, err_union);
2897 try writer.writeAll(")");
2898 break :err;
2899 }
2900 if (operand_is_ptr or isByRef(err_union_ty)) {
29112901 try writer.writeAll("if(");
2902 try f.writeCValue(writer, err_union);
2903 try writer.writeAll("->error)");
2904 break :err;
29122905 }
2913 try f.writeCValue(writer, err_union);
2914 try writer.writeAll(")");
2915 break :err;
2916 }
2917 if (operand_is_ptr or isByRef(err_union_ty)) {
29182906 try writer.writeAll("if(");
29192907 try f.writeCValue(writer, err_union);
2920 try writer.writeAll("->error)");
2921 break :err;
2908 try writer.writeAll(".error)");
29222909 }
2923 try writer.writeAll("if(");
2924 try f.writeCValue(writer, err_union);
2925 try writer.writeAll(".error)");
2926 }
29272910
2928 try genBody(f, body);
2929 try f.object.indent_writer.insertNewline();
2911 try genBody(f, body);
2912 try f.object.indent_writer.insertNewline();
2913 }
29302914
29312915 if (!payload_has_bits) {
29322916 if (!operand_is_ptr) {
......@@ -3466,7 +3450,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
34663450
34673451 if (operand_ty.zigTypeTag() == .Pointer) {
34683452 const err_union_ty = operand_ty.childType();
3469 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
3453 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
34703454 return CValue{ .bytes = "0" };
34713455 }
34723456 if (!err_union_ty.errorUnionPayload().hasRuntimeBits()) {
......@@ -3478,7 +3462,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
34783462 try writer.writeAll(";\n");
34793463 return local;
34803464 }
3481 if (operand_ty.errorUnionSet().errorSetCardinality() == .zero) {
3465 if (operand_ty.errorUnionSet().errorSetIsEmpty()) {
34823466 return CValue{ .bytes = "0" };
34833467 }
34843468 if (!operand_ty.errorUnionPayload().hasRuntimeBits()) {
......@@ -3507,10 +3491,6 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: [*:0]c
35073491 const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer;
35083492 const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
35093493
3510 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
3511 return operand;
3512 }
3513
35143494 if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) {
35153495 return CValue.none;
35163496 }
......@@ -3575,11 +3555,6 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
35753555 const error_ty = error_union_ty.errorUnionSet();
35763556 const payload_ty = error_union_ty.errorUnionPayload();
35773557
3578 if (error_ty.errorSetCardinality() == .zero) {
3579 // TODO: write undefined bytes through the pointer here
3580 return operand;
3581 }
3582
35833558 // First, set the non-error value.
35843559 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
35853560 try f.writeCValueDeref(writer, operand);
......@@ -3623,9 +3598,6 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
36233598 const operand = try f.resolveInst(ty_op.operand);
36243599
36253600 const inst_ty = f.air.typeOfIndex(inst);
3626 if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) {
3627 return operand;
3628 }
36293601 const local = try f.allocLocal(inst_ty, .Const);
36303602 try writer.writeAll(" = { .error = 0, .payload = ");
36313603 try f.writeCValue(writer, operand);
......@@ -3652,7 +3624,7 @@ fn airIsErr(
36523624
36533625 try writer.writeAll(" = ");
36543626
3655 if (error_ty.errorSetCardinality() == .zero) {
3627 if (error_ty.errorSetIsEmpty()) {
36563628 try writer.print("0 {s} 0;\n", .{op_str});
36573629 } else {
36583630 if (is_ptr) {
src/codegen/llvm.zig+38-79
......@@ -599,6 +599,13 @@ pub const Object = struct {
599599 self.llvm_module.dump();
600600 }
601601
602 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
603 defer arena_allocator.deinit();
604 const arena = arena_allocator.allocator();
605
606 const mod = comp.bin_file.options.module.?;
607 const cache_dir = mod.zig_cache_artifact_directory;
608
602609 if (std.debug.runtime_safety) {
603610 var error_message: [*:0]const u8 = undefined;
604611 // verifyModule always allocs the error_message even if there is no error
......@@ -606,17 +613,15 @@ pub const Object = struct {
606613
607614 if (self.llvm_module.verify(.ReturnStatus, &error_message).toBool()) {
608615 std.debug.print("\n{s}\n", .{error_message});
616
617 if (try locPath(arena, comp.emit_llvm_ir, cache_dir)) |emit_llvm_ir_path| {
618 _ = self.llvm_module.printModuleToFile(emit_llvm_ir_path, &error_message);
619 }
620
609621 @panic("LLVM module verification failed");
610622 }
611623 }
612624
613 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
614 defer arena_allocator.deinit();
615 const arena = arena_allocator.allocator();
616
617 const mod = comp.bin_file.options.module.?;
618 const cache_dir = mod.zig_cache_artifact_directory;
619
620625 var emit_bin_path: ?[*:0]const u8 = if (comp.bin_file.options.emit) |emit|
621626 try emit.basenamePath(arena, try arena.dupeZ(u8, comp.bin_file.intermediary_basename.?))
622627 else
......@@ -1566,22 +1571,6 @@ pub const Object = struct {
15661571 },
15671572 .ErrorUnion => {
15681573 const payload_ty = ty.errorUnionPayload();
1569 switch (ty.errorUnionSet().errorSetCardinality()) {
1570 .zero => {
1571 const payload_di_ty = try o.lowerDebugType(payload_ty, .full);
1572 // The recursive call to `lowerDebugType` means we can't use `gop` anymore.
1573 try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(payload_di_ty), .{ .mod = o.module });
1574 return payload_di_ty;
1575 },
1576 .one => {
1577 if (payload_ty.isNoReturn()) {
1578 const di_type = dib.createBasicType("void", 0, DW.ATE.signed);
1579 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type);
1580 return di_type;
1581 }
1582 },
1583 .many => {},
1584 }
15851574 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
15861575 const err_set_di_ty = try o.lowerDebugType(Type.anyerror, .full);
15871576 // The recursive call to `lowerDebugType` means we can't use `gop` anymore.
......@@ -2549,15 +2538,6 @@ pub const DeclGen = struct {
25492538 },
25502539 .ErrorUnion => {
25512540 const payload_ty = t.errorUnionPayload();
2552 switch (t.errorUnionSet().errorSetCardinality()) {
2553 .zero => return dg.lowerType(payload_ty),
2554 .one => {
2555 if (payload_ty.isNoReturn()) {
2556 return dg.context.voidType();
2557 }
2558 },
2559 .many => {},
2560 }
25612541 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
25622542 return try dg.lowerType(Type.anyerror);
25632543 }
......@@ -3217,10 +3197,6 @@ pub const DeclGen = struct {
32173197 },
32183198 .ErrorUnion => {
32193199 const payload_type = tv.ty.errorUnionPayload();
3220 if (tv.ty.errorUnionSet().errorSetCardinality() == .zero) {
3221 const payload_val = tv.val.castTag(.eu_payload).?.data;
3222 return dg.lowerValue(.{ .ty = payload_type, .val = payload_val });
3223 }
32243200 const is_pl = tv.val.errorUnionIsPayload();
32253201
32263202 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
......@@ -4790,40 +4766,37 @@ pub const FuncGen = struct {
47904766 }
47914767
47924768 fn lowerTry(fg: *FuncGen, err_union: *const llvm.Value, body: []const Air.Inst.Index, err_union_ty: Type, operand_is_ptr: bool, result_ty: Type) !?*const llvm.Value {
4793 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
4794 // If the error set has no fields, then the payload and the error
4795 // union are the same value.
4796 return err_union;
4797 }
4798
47994769 const payload_ty = err_union_ty.errorUnionPayload();
48004770 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime();
48014771 const target = fg.dg.module.getTarget();
4802 const is_err = err: {
4803 const err_set_ty = try fg.dg.lowerType(Type.anyerror);
4804 const zero = err_set_ty.constNull();
4805 if (!payload_has_bits) {
4806 const loaded = if (operand_is_ptr) fg.builder.buildLoad(err_union, "") else err_union;
4807 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4808 }
4809 const err_field_index = errUnionErrorOffset(payload_ty, target);
4810 if (operand_is_ptr or isByRef(err_union_ty)) {
4811 const err_field_ptr = fg.builder.buildStructGEP(err_union, err_field_index, "");
4812 const loaded = fg.builder.buildLoad(err_field_ptr, "");
4772
4773 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
4774 const is_err = err: {
4775 const err_set_ty = try fg.dg.lowerType(Type.anyerror);
4776 const zero = err_set_ty.constNull();
4777 if (!payload_has_bits) {
4778 const loaded = if (operand_is_ptr) fg.builder.buildLoad(err_union, "") else err_union;
4779 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4780 }
4781 const err_field_index = errUnionErrorOffset(payload_ty, target);
4782 if (operand_is_ptr or isByRef(err_union_ty)) {
4783 const err_field_ptr = fg.builder.buildStructGEP(err_union, err_field_index, "");
4784 const loaded = fg.builder.buildLoad(err_field_ptr, "");
4785 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4786 }
4787 const loaded = fg.builder.buildExtractValue(err_union, err_field_index, "");
48134788 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4814 }
4815 const loaded = fg.builder.buildExtractValue(err_union, err_field_index, "");
4816 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4817 };
4789 };
48184790
4819 const return_block = fg.context.appendBasicBlock(fg.llvm_func, "TryRet");
4820 const continue_block = fg.context.appendBasicBlock(fg.llvm_func, "TryCont");
4821 _ = fg.builder.buildCondBr(is_err, return_block, continue_block);
4791 const return_block = fg.context.appendBasicBlock(fg.llvm_func, "TryRet");
4792 const continue_block = fg.context.appendBasicBlock(fg.llvm_func, "TryCont");
4793 _ = fg.builder.buildCondBr(is_err, return_block, continue_block);
48224794
4823 fg.builder.positionBuilderAtEnd(return_block);
4824 try fg.genBody(body);
4795 fg.builder.positionBuilderAtEnd(return_block);
4796 try fg.genBody(body);
48254797
4826 fg.builder.positionBuilderAtEnd(continue_block);
4798 fg.builder.positionBuilderAtEnd(continue_block);
4799 }
48274800 if (!payload_has_bits) {
48284801 if (!operand_is_ptr) return null;
48294802
......@@ -5660,7 +5633,7 @@ pub const FuncGen = struct {
56605633 const err_set_ty = try self.dg.lowerType(Type.initTag(.anyerror));
56615634 const zero = err_set_ty.constNull();
56625635
5663 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5636 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
56645637 const llvm_i1 = self.context.intType(1);
56655638 switch (op) {
56665639 .EQ => return llvm_i1.constInt(1, .False), // 0 == 0
......@@ -5783,13 +5756,6 @@ pub const FuncGen = struct {
57835756
57845757 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
57855758 const operand = try self.resolveInst(ty_op.operand);
5786 const operand_ty = self.air.typeOf(ty_op.operand);
5787 const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
5788 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5789 // If the error set has no fields, then the payload and the error
5790 // union are the same value.
5791 return operand;
5792 }
57935759 const result_ty = self.air.typeOfIndex(inst);
57945760 const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty;
57955761 const target = self.dg.module.getTarget();
......@@ -5820,7 +5786,7 @@ pub const FuncGen = struct {
58205786 const operand = try self.resolveInst(ty_op.operand);
58215787 const operand_ty = self.air.typeOf(ty_op.operand);
58225788 const err_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
5823 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5789 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
58245790 const err_llvm_ty = try self.dg.lowerType(Type.anyerror);
58255791 if (operand_is_ptr) {
58265792 return self.builder.buildBitCast(operand, err_llvm_ty.pointerType(0), "");
......@@ -5851,10 +5817,6 @@ pub const FuncGen = struct {
58515817 const operand = try self.resolveInst(ty_op.operand);
58525818 const error_union_ty = self.air.typeOf(ty_op.operand).childType();
58535819
5854 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5855 // TODO: write undefined bytes through the pointer here
5856 return operand;
5857 }
58585820 const payload_ty = error_union_ty.errorUnionPayload();
58595821 const non_error_val = try self.dg.lowerValue(.{ .ty = Type.anyerror, .val = Value.zero });
58605822 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -5933,9 +5895,6 @@ pub const FuncGen = struct {
59335895 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
59345896 const inst_ty = self.air.typeOfIndex(inst);
59355897 const operand = try self.resolveInst(ty_op.operand);
5936 if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) {
5937 return operand;
5938 }
59395898 const payload_ty = self.air.typeOf(ty_op.operand);
59405899 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
59415900 return operand;
src/codegen/llvm/bindings.zig+3
......@@ -390,6 +390,9 @@ pub const Module = opaque {
390390
391391 pub const setModuleInlineAsm2 = LLVMSetModuleInlineAsm2;
392392 extern fn LLVMSetModuleInlineAsm2(M: *const Module, Asm: [*]const u8, Len: usize) void;
393
394 pub const printModuleToFile = LLVMPrintModuleToFile;
395 extern fn LLVMPrintModuleToFile(M: *const Module, Filename: [*:0]const u8, ErrorMessage: *[*:0]const u8) Bool;
393396};
394397
395398pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
src/print_air.zig+30-8
......@@ -4,6 +4,7 @@ const fmtIntSizeBin = std.fmt.fmtIntSizeBin;
44
55const Module = @import("Module.zig");
66const Value = @import("value.zig").Value;
7const Type = @import("type.zig").Type;
78const Air = @import("Air.zig");
89const Liveness = @import("Liveness.zig");
910
......@@ -304,14 +305,27 @@ const Writer = struct {
304305 // no-op, no argument to write
305306 }
306307
308 fn writeType(w: *Writer, s: anytype, ty: Type) !void {
309 const t = ty.tag();
310 switch (t) {
311 .inferred_alloc_const => try s.writeAll("(inferred_alloc_const)"),
312 .inferred_alloc_mut => try s.writeAll("(inferred_alloc_mut)"),
313 .generic_poison => try s.writeAll("(generic_poison)"),
314 .var_args_param => try s.writeAll("(var_args_param)"),
315 .bound_fn => try s.writeAll("(bound_fn)"),
316 else => try ty.print(s, w.module),
317 }
318 }
319
307320 fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
308321 const ty = w.air.instructions.items(.data)[inst].ty;
309 try s.print("{}", .{ty.fmtDebug()});
322 try w.writeType(s, ty);
310323 }
311324
312325 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
313326 const ty_op = w.air.instructions.items(.data)[inst].ty_op;
314 try s.print("{}, ", .{w.air.getRefType(ty_op.ty).fmtDebug()});
327 try w.writeType(s, w.air.getRefType(ty_op.ty));
328 try s.writeAll(", ");
315329 try w.writeOperand(s, inst, 0, ty_op.operand);
316330 }
317331
......@@ -320,7 +334,8 @@ const Writer = struct {
320334 const extra = w.air.extraData(Air.Block, ty_pl.payload);
321335 const body = w.air.extra[extra.end..][0..extra.data.body_len];
322336
323 try s.print("{}, {{\n", .{w.air.getRefType(ty_pl.ty).fmtDebug()});
337 try w.writeType(s, w.air.getRefType(ty_pl.ty));
338 try s.writeAll(", {\n");
324339 const old_indent = w.indent;
325340 w.indent += 2;
326341 try w.writeBody(s, body);
......@@ -335,7 +350,8 @@ const Writer = struct {
335350 const len = @intCast(usize, vector_ty.arrayLen());
336351 const elements = @ptrCast([]const Air.Inst.Ref, w.air.extra[ty_pl.payload..][0..len]);
337352
338 try s.print("{}, [", .{vector_ty.fmtDebug()});
353 try w.writeType(s, vector_ty);
354 try s.writeAll(", [");
339355 for (elements) |elem, i| {
340356 if (i != 0) try s.writeAll(", ");
341357 try w.writeOperand(s, inst, i, elem);
......@@ -408,7 +424,8 @@ const Writer = struct {
408424 const extra = w.air.extraData(Air.Bin, pl_op.payload).data;
409425
410426 const elem_ty = w.air.typeOfIndex(inst).childType();
411 try s.print("{}, ", .{elem_ty.fmtDebug()});
427 try w.writeType(s, elem_ty);
428 try s.writeAll(", ");
412429 try w.writeOperand(s, inst, 0, pl_op.operand);
413430 try s.writeAll(", ");
414431 try w.writeOperand(s, inst, 1, extra.lhs);
......@@ -511,7 +528,9 @@ const Writer = struct {
511528 fn writeConstant(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
512529 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
513530 const val = w.air.values[ty_pl.payload];
514 try s.print("{}, {}", .{ w.air.getRefType(ty_pl.ty).fmtDebug(), val.fmtDebug() });
531 const ty = w.air.getRefType(ty_pl.ty);
532 try w.writeType(s, ty);
533 try s.print(", {}", .{val.fmtValue(ty, w.module)});
515534 }
516535
517536 fn writeAssembly(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
......@@ -523,7 +542,7 @@ const Writer = struct {
523542 var op_index: usize = 0;
524543
525544 const ret_ty = w.air.typeOfIndex(inst);
526 try s.print("{}", .{ret_ty.fmtDebug()});
545 try w.writeType(s, ret_ty);
527546
528547 if (is_volatile) {
529548 try s.writeAll(", volatile");
......@@ -647,7 +666,10 @@ const Writer = struct {
647666 const body = w.air.extra[extra.end..][0..extra.data.body_len];
648667
649668 try w.writeOperand(s, inst, 0, extra.data.ptr);
650 try s.print(", {}, {{\n", .{w.air.getRefType(ty_pl.ty).fmtDebug()});
669
670 try s.writeAll(", ");
671 try w.writeType(s, w.air.getRefType(ty_pl.ty));
672 try s.writeAll(", {\n");
651673 const old_indent = w.indent;
652674 w.indent += 2;
653675 try w.writeBody(s, body);
src/type.zig+29-163
......@@ -2366,6 +2366,10 @@ pub const Type = extern union {
23662366 .anyopaque,
23672367 .@"opaque",
23682368 .type_info,
2369 .error_set_single,
2370 .error_union,
2371 .error_set,
2372 .error_set_merged,
23692373 => return true,
23702374
23712375 // These are false because they are comptime-only types.
......@@ -2389,20 +2393,8 @@ pub const Type = extern union {
23892393 .fn_void_no_args,
23902394 .fn_naked_noreturn_no_args,
23912395 .fn_ccc_void_no_args,
2392 .error_set_single,
23932396 => return false,
23942397
2395 .error_set => {
2396 const err_set_obj = ty.castTag(.error_set).?.data;
2397 const names = err_set_obj.names.keys();
2398 return names.len > 1;
2399 },
2400 .error_set_merged => {
2401 const name_map = ty.castTag(.error_set_merged).?.data;
2402 const names = name_map.keys();
2403 return names.len > 1;
2404 },
2405
24062398 // These types have more than one possible value, so the result is the same as
24072399 // asking whether they are comptime-only types.
24082400 .anyframe_T,
......@@ -2443,25 +2435,6 @@ pub const Type = extern union {
24432435 }
24442436 },
24452437
2446 .error_union => {
2447 // This code needs to be kept in sync with the equivalent switch prong
2448 // in abiSizeAdvanced.
2449 const data = ty.castTag(.error_union).?.data;
2450 switch (data.error_set.errorSetCardinality()) {
2451 .zero => return hasRuntimeBitsAdvanced(data.payload, ignore_comptime_only, sema_kit),
2452 .one => return !data.payload.isNoReturn(),
2453 .many => {
2454 if (ignore_comptime_only) {
2455 return true;
2456 } else if (sema_kit) |sk| {
2457 return !(try sk.sema.typeRequiresComptime(sk.block, sk.src, ty));
2458 } else {
2459 return !comptimeOnly(ty);
2460 }
2461 },
2462 }
2463 },
2464
24652438 .@"struct" => {
24662439 const struct_obj = ty.castTag(.@"struct").?.data;
24672440 if (struct_obj.status == .field_types_wip) {
......@@ -2926,27 +2899,11 @@ pub const Type = extern union {
29262899 .anyerror_void_error_union,
29272900 .anyerror,
29282901 .error_set_inferred,
2902 .error_set_single,
2903 .error_set,
2904 .error_set_merged,
29292905 => return AbiAlignmentAdvanced{ .scalar = 2 },
29302906
2931 .error_set => {
2932 const err_set_obj = ty.castTag(.error_set).?.data;
2933 const names = err_set_obj.names.keys();
2934 if (names.len <= 1) {
2935 return AbiAlignmentAdvanced{ .scalar = 0 };
2936 } else {
2937 return AbiAlignmentAdvanced{ .scalar = 2 };
2938 }
2939 },
2940 .error_set_merged => {
2941 const name_map = ty.castTag(.error_set_merged).?.data;
2942 const names = name_map.keys();
2943 if (names.len <= 1) {
2944 return AbiAlignmentAdvanced{ .scalar = 0 };
2945 } else {
2946 return AbiAlignmentAdvanced{ .scalar = 2 };
2947 }
2948 },
2949
29502907 .array, .array_sentinel => return ty.elemType().abiAlignmentAdvanced(target, strat),
29512908
29522909 // TODO audit this - is there any more complicated logic to determine
......@@ -2971,12 +2928,7 @@ pub const Type = extern union {
29712928
29722929 switch (child_type.zigTypeTag()) {
29732930 .Pointer => return AbiAlignmentAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) },
2974 .ErrorSet => switch (child_type.errorSetCardinality()) {
2975 // `?error{}` is comptime-known to be null.
2976 .zero => return AbiAlignmentAdvanced{ .scalar = 0 },
2977 .one => return AbiAlignmentAdvanced{ .scalar = 1 },
2978 .many => return abiAlignmentAdvanced(Type.anyerror, target, strat),
2979 },
2931 .ErrorSet => return abiAlignmentAdvanced(Type.anyerror, target, strat),
29802932 .NoReturn => return AbiAlignmentAdvanced{ .scalar = 0 },
29812933 else => {},
29822934 }
......@@ -2999,15 +2951,6 @@ pub const Type = extern union {
29992951 // This code needs to be kept in sync with the equivalent switch prong
30002952 // in abiSizeAdvanced.
30012953 const data = ty.castTag(.error_union).?.data;
3002 switch (data.error_set.errorSetCardinality()) {
3003 .zero => return abiAlignmentAdvanced(data.payload, target, strat),
3004 .one => {
3005 if (data.payload.isNoReturn()) {
3006 return AbiAlignmentAdvanced{ .scalar = 0 };
3007 }
3008 },
3009 .many => {},
3010 }
30112954 const code_align = abiAlignment(Type.anyerror, target);
30122955 switch (strat) {
30132956 .eager, .sema_kit => {
......@@ -3118,7 +3061,6 @@ pub const Type = extern union {
31183061 .@"undefined",
31193062 .enum_literal,
31203063 .type_info,
3121 .error_set_single,
31223064 => return AbiAlignmentAdvanced{ .scalar = 0 },
31233065
31243066 .noreturn,
......@@ -3237,7 +3179,6 @@ pub const Type = extern union {
32373179 .empty_struct_literal,
32383180 .empty_struct,
32393181 .void,
3240 .error_set_single,
32413182 => return AbiSizeAdvanced{ .scalar = 0 },
32423183
32433184 .@"struct", .tuple, .anon_struct => switch (ty.containerLayout()) {
......@@ -3396,27 +3337,11 @@ pub const Type = extern union {
33963337 .anyerror_void_error_union,
33973338 .anyerror,
33983339 .error_set_inferred,
3340 .error_set,
3341 .error_set_merged,
3342 .error_set_single,
33993343 => return AbiSizeAdvanced{ .scalar = 2 },
34003344
3401 .error_set => {
3402 const err_set_obj = ty.castTag(.error_set).?.data;
3403 const names = err_set_obj.names.keys();
3404 if (names.len <= 1) {
3405 return AbiSizeAdvanced{ .scalar = 0 };
3406 } else {
3407 return AbiSizeAdvanced{ .scalar = 2 };
3408 }
3409 },
3410 .error_set_merged => {
3411 const name_map = ty.castTag(.error_set_merged).?.data;
3412 const names = name_map.keys();
3413 if (names.len <= 1) {
3414 return AbiSizeAdvanced{ .scalar = 0 };
3415 } else {
3416 return AbiSizeAdvanced{ .scalar = 2 };
3417 }
3418 },
3419
34203345 .i16, .u16 => return AbiSizeAdvanced{ .scalar = intAbiSize(16, target) },
34213346 .u29 => return AbiSizeAdvanced{ .scalar = intAbiSize(29, target) },
34223347 .i32, .u32 => return AbiSizeAdvanced{ .scalar = intAbiSize(32, target) },
......@@ -3467,24 +3392,6 @@ pub const Type = extern union {
34673392 // This code needs to be kept in sync with the equivalent switch prong
34683393 // in abiAlignmentAdvanced.
34693394 const data = ty.castTag(.error_union).?.data;
3470 // Here we need to care whether or not the error set is *empty* or whether
3471 // it only has *one possible value*. In the former case, it means there
3472 // cannot possibly be an error, meaning the ABI size is equivalent to the
3473 // payload ABI size. In the latter case, we need to account for the "tag"
3474 // because even if both the payload type and the error set type of an
3475 // error union have no runtime bits, an error union still has
3476 // 1 bit of data which is whether or not the value is an error.
3477 // Zig still uses the error code encoding at runtime, even when only 1 bit
3478 // would suffice. This prevents coercions from needing to branch.
3479 switch (data.error_set.errorSetCardinality()) {
3480 .zero => return abiSizeAdvanced(data.payload, target, strat),
3481 .one => {
3482 if (data.payload.isNoReturn()) {
3483 return AbiSizeAdvanced{ .scalar = 0 };
3484 }
3485 },
3486 .many => {},
3487 }
34883395 const code_size = abiSize(Type.anyerror, target);
34893396 if (!data.payload.hasRuntimeBits()) {
34903397 // Same as anyerror.
......@@ -3727,11 +3634,7 @@ pub const Type = extern union {
37273634
37283635 .error_union => {
37293636 const payload = ty.castTag(.error_union).?.data;
3730 if (!payload.error_set.hasRuntimeBits() and !payload.payload.hasRuntimeBits()) {
3731 return 0;
3732 } else if (!payload.error_set.hasRuntimeBits()) {
3733 return payload.payload.bitSizeAdvanced(target, sema_kit);
3734 } else if (!payload.payload.hasRuntimeBits()) {
3637 if (!payload.payload.hasRuntimeBits()) {
37353638 return payload.error_set.bitSizeAdvanced(target, sema_kit);
37363639 }
37373640 @panic("TODO bitSize error union");
......@@ -4351,30 +4254,25 @@ pub const Type = extern union {
43514254 };
43524255 }
43534256
4354 const ErrorSetCardinality = enum { zero, one, many };
4355
4356 pub fn errorSetCardinality(ty: Type) ErrorSetCardinality {
4257 /// Returns false for unresolved inferred error sets.
4258 pub fn errorSetIsEmpty(ty: Type) bool {
43574259 switch (ty.tag()) {
4358 .anyerror => return .many,
4359 .error_set_inferred => return .many,
4360 .error_set_single => return .one,
4260 .anyerror => return false,
4261 .error_set_inferred => {
4262 const inferred_error_set = ty.castTag(.error_set_inferred).?.data;
4263 // Can't know for sure.
4264 if (!inferred_error_set.is_resolved) return false;
4265 if (inferred_error_set.is_anyerror) return false;
4266 return inferred_error_set.errors.count() == 0;
4267 },
4268 .error_set_single => return false,
43614269 .error_set => {
43624270 const err_set_obj = ty.castTag(.error_set).?.data;
4363 const names = err_set_obj.names.keys();
4364 switch (names.len) {
4365 0 => return .zero,
4366 1 => return .one,
4367 else => return .many,
4368 }
4271 return err_set_obj.names.count() == 0;
43694272 },
43704273 .error_set_merged => {
43714274 const name_map = ty.castTag(.error_set_merged).?.data;
4372 const names = name_map.keys();
4373 switch (names.len) {
4374 0 => return .zero,
4375 1 => return .one,
4376 else => return .many,
4377 }
4275 return name_map.count() == 0;
43784276 },
43794277 else => unreachable,
43804278 }
......@@ -4883,6 +4781,10 @@ pub const Type = extern union {
48834781 .bool,
48844782 .type,
48854783 .anyerror,
4784 .error_union,
4785 .error_set_single,
4786 .error_set,
4787 .error_set_merged,
48864788 .fn_noreturn_no_args,
48874789 .fn_void_no_args,
48884790 .fn_naked_noreturn_no_args,
......@@ -4939,42 +4841,6 @@ pub const Type = extern union {
49394841 }
49404842 },
49414843
4942 .error_union => {
4943 const error_ty = ty.errorUnionSet();
4944 switch (error_ty.errorSetCardinality()) {
4945 .zero => {
4946 const payload_ty = ty.errorUnionPayload();
4947 if (onePossibleValue(payload_ty)) |payload_val| {
4948 _ = payload_val;
4949 return Value.initTag(.the_only_possible_value);
4950 } else {
4951 return null;
4952 }
4953 },
4954 .one => {
4955 if (ty.errorUnionPayload().isNoReturn()) {
4956 const error_val = onePossibleValue(error_ty).?;
4957 return error_val;
4958 } else {
4959 return null;
4960 }
4961 },
4962 .many => return null,
4963 }
4964 },
4965
4966 .error_set_single => return Value.initTag(.the_only_possible_value),
4967 .error_set => {
4968 const err_set_obj = ty.castTag(.error_set).?.data;
4969 if (err_set_obj.names.count() > 1) return null;
4970 return Value.initTag(.the_only_possible_value);
4971 },
4972 .error_set_merged => {
4973 const name_map = ty.castTag(.error_set_merged).?.data;
4974 if (name_map.count() > 1) return null;
4975 return Value.initTag(.the_only_possible_value);
4976 },
4977
49784844 .@"struct" => {
49794845 const s = ty.castTag(.@"struct").?.data;
49804846 assert(s.haveFieldTypes());
src/value.zig+1
......@@ -1062,6 +1062,7 @@ pub const Value = extern union {
10621062 sema_kit: ?Module.WipAnalysis,
10631063 ) Module.CompileError!BigIntConst {
10641064 switch (val.tag()) {
1065 .null_value,
10651066 .zero,
10661067 .bool_false,
10671068 .the_only_possible_value, // i0, u0
test/behavior/basic.zig+23
......@@ -1086,3 +1086,26 @@ test "inline call of function with a switch inside the return statement" {
10861086 };
10871087 try expect(S.foo(1) == 1);
10881088}
1089
1090test "namespace lookup ignores decl causing the lookup" {
1091 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1092 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1093 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1094 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1095 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1096
1097 const S = struct {
1098 fn Mixin(comptime T: type) type {
1099 return struct {
1100 fn foo() void {
1101 const set = std.EnumSet(T.E).init(undefined);
1102 _ = set;
1103 }
1104 };
1105 }
1106
1107 const E = enum { a, b };
1108 usingnamespace Mixin(@This());
1109 };
1110 _ = S.foo();
1111}
test/behavior/cast.zig+1
......@@ -1426,6 +1426,7 @@ test "coerce undefined single-item pointer of array to error union of slice" {
14261426}
14271427
14281428test "pointer to empty struct literal to mutable slice" {
1429 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
14291430 var x: []i32 = &.{};
14301431 try expect(x.len == 0);
14311432}
test/behavior/error.zig-59
......@@ -453,65 +453,6 @@ test "optional error set is the same size as error set" {
453453 comptime try expect(S.returnsOptErrSet() == null);
454454}
455455
456test "optional error set with only one error is the same size as bool" {
457 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
458 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
459 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
460
461 const E = error{only};
462 comptime try expect(@sizeOf(?E) == @sizeOf(bool));
463 comptime try expect(@alignOf(?E) == @alignOf(bool));
464 const S = struct {
465 fn gimmeNull() ?E {
466 return null;
467 }
468 fn gimmeErr() ?E {
469 return error.only;
470 }
471 };
472 try expect(S.gimmeNull() == null);
473 try expect(error.only == S.gimmeErr().?);
474 comptime try expect(S.gimmeNull() == null);
475 comptime try expect(error.only == S.gimmeErr().?);
476}
477
478test "optional empty error set" {
479 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
480
481 comptime try expect(@sizeOf(error{}!void) == @sizeOf(void));
482 comptime try expect(@alignOf(error{}!void) == @alignOf(void));
483
484 var x: ?error{} = undefined;
485 if (x != null) {
486 @compileError("test failed");
487 }
488}
489
490test "empty error set plus zero-bit payload" {
491 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
492 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
493 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
494
495 comptime try expect(@sizeOf(error{}!void) == @sizeOf(void));
496 comptime try expect(@alignOf(error{}!void) == @alignOf(void));
497
498 var x: error{}!void = undefined;
499 if (x) |payload| {
500 if (payload != {}) {
501 @compileError("test failed");
502 }
503 } else |_| {
504 @compileError("test failed");
505 }
506 const S = struct {
507 fn empty() error{}!void {}
508 fn inferred() !void {
509 return empty();
510 }
511 };
512 try S.inferred();
513}
514
515456test "nested catch" {
516457 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
517458 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
test/behavior/typename.zig+25-36
......@@ -137,43 +137,8 @@ const A_Enum = enum {
137137
138138fn regular() void {}
139139
140test "fn body decl" {
141 if (builtin.zig_backend == .stage1) {
142 // stage1 fails to return fully qualified namespaces.
143 return error.SkipZigTest;
144 }
145
146 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
147 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
148 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
149 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
150
151 try B.doTest();
152}
153
154140const B = struct {
155 fn doTest() !void {
156 const B_Struct = struct {};
157 const B_Union = union {
158 unused: u8,
159 };
160 const B_Enum = enum {
161 unused,
162 };
163
164 try expectEqualStringsIgnoreDigits(
165 "behavior.typename.B.doTest__struct_0",
166 @typeName(B_Struct),
167 );
168 try expectEqualStringsIgnoreDigits(
169 "behavior.typename.B.doTest__union_0",
170 @typeName(B_Union),
171 );
172 try expectEqualStringsIgnoreDigits(
173 "behavior.typename.B.doTest__enum_0",
174 @typeName(B_Enum),
175 );
176 }
141 fn doTest() !void {}
177142};
178143
179144test "fn param" {
......@@ -246,3 +211,27 @@ pub fn expectEqualStringsIgnoreDigits(expected: []const u8, actual: []const u8)
246211 }
247212 return expectEqualStrings(expected, actual_buf[0..actual_i]);
248213}
214
215test "local variable" {
216 if (builtin.zig_backend == .stage1) {
217 // stage1 fails to return fully qualified namespaces.
218 return error.SkipZigTest;
219 }
220
221 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
222 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
223 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
224 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
225
226 const Foo = struct { a: u32 };
227 const Bar = union { a: u32 };
228 const Baz = enum { a, b };
229 const Qux = enum { a, b };
230 const Quux = enum { a, b };
231
232 try expectEqualStrings("behavior.typename.test.local variable.Foo", @typeName(Foo));
233 try expectEqualStrings("behavior.typename.test.local variable.Bar", @typeName(Bar));
234 try expectEqualStrings("behavior.typename.test.local variable.Baz", @typeName(Baz));
235 try expectEqualStrings("behavior.typename.test.local variable.Qux", @typeName(Qux));
236 try expectEqualStrings("behavior.typename.test.local variable.Quux", @typeName(Quux));
237}
test/behavior/union.zig+18
......@@ -1183,3 +1183,21 @@ test "comptime equality of extern unions with same tag" {
11831183 const b = S.U{ .a = 1234 };
11841184 try expect(S.foo(a) == S.foo(b));
11851185}
1186
1187test "union tag is set when initiated as a temporary value at runtime" {
1188 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1189 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1190 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1191
1192 const U = union(enum) {
1193 a,
1194 b: u32,
1195 c,
1196
1197 fn doTheTest(u: @This()) !void {
1198 try expect(u == .b);
1199 }
1200 };
1201 var b: u32 = 1;
1202 try (U{ .b = b }).doTheTest();
1203}