authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-07-28 20:43:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-28 21:43:07-07:00
loge8e9a4ac664ca803514ca5f177f3384ce66e31fc
tree49a4f6ffbdaecc5786d5f656a2c55923aa854051
parent2826f78a61c014b7cfb9e7ce1a7efce31b24d0c9

Build: use optionals again

Closes #14952

1 files changed, 19 insertions(+), 44 deletions(-)

lib/std/Build/Step/Run.zig+19-44
...@@ -755,24 +755,19 @@ fn runCommand(...@@ -755,24 +755,19 @@ fn runCommand(
755 // Capture stdout and stderr to GeneratedFile objects.755 // Capture stdout and stderr to GeneratedFile objects.
756 const Stream = struct {756 const Stream = struct {
757 captured: ?*Output,757 captured: ?*Output,
758 is_null: bool,758 bytes: ?[]const u8,
759 bytes: []const u8,
760 };759 };
761 for ([_]Stream{760 for ([_]Stream{
762 .{761 .{
763 .captured = self.captured_stdout,762 .captured = self.captured_stdout,
764 .is_null = result.stdio.stdout_null,
765 .bytes = result.stdio.stdout,763 .bytes = result.stdio.stdout,
766 },764 },
767 .{765 .{
768 .captured = self.captured_stderr,766 .captured = self.captured_stderr,
769 .is_null = result.stdio.stderr_null,
770 .bytes = result.stdio.stderr,767 .bytes = result.stdio.stderr,
771 },768 },
772 }) |stream| {769 }) |stream| {
773 if (stream.captured) |output| {770 if (stream.captured) |output| {
774 assert(!stream.is_null);
775
776 const output_components = .{ "o", digest.?, output.basename };771 const output_components = .{ "o", digest.?, output.basename };
777 const output_path = try b.cache_root.join(arena, &output_components);772 const output_path = try b.cache_root.join(arena, &output_components);
778 output.generated_file.path = output_path;773 output.generated_file.path = output_path;
...@@ -784,7 +779,7 @@ fn runCommand(...@@ -784,7 +779,7 @@ fn runCommand(
784 b.cache_root, sub_path_dirname, @errorName(err),779 b.cache_root, sub_path_dirname, @errorName(err),
785 });780 });
786 };781 };
787 b.cache_root.handle.writeFile(sub_path, stream.bytes) catch |err| {782 b.cache_root.handle.writeFile(sub_path, stream.bytes.?) catch |err| {
788 return step.fail("unable to write file '{}{s}': {s}", .{783 return step.fail("unable to write file '{}{s}': {s}", .{
789 b.cache_root, sub_path, @errorName(err),784 b.cache_root, sub_path, @errorName(err),
790 });785 });
...@@ -797,8 +792,7 @@ fn runCommand(...@@ -797,8 +792,7 @@ fn runCommand(
797 switch (self.stdio) {792 switch (self.stdio) {
798 .check => |checks| for (checks.items) |check| switch (check) {793 .check => |checks| for (checks.items) |check| switch (check) {
799 .expect_stderr_exact => |expected_bytes| {794 .expect_stderr_exact => |expected_bytes| {
800 assert(!result.stdio.stderr_null);795 if (!mem.eql(u8, expected_bytes, result.stdio.stderr.?)) {
801 if (!mem.eql(u8, expected_bytes, result.stdio.stderr)) {
802 return step.fail(796 return step.fail(
803 \\797 \\
804 \\========= expected this stderr: =========798 \\========= expected this stderr: =========
...@@ -809,14 +803,13 @@ fn runCommand(...@@ -809,14 +803,13 @@ fn runCommand(
809 \\{s}803 \\{s}
810 , .{804 , .{
811 expected_bytes,805 expected_bytes,
812 result.stdio.stderr,806 result.stdio.stderr.?,
813 try Step.allocPrintCmd(arena, self.cwd, final_argv),807 try Step.allocPrintCmd(arena, self.cwd, final_argv),
814 });808 });
815 }809 }
816 },810 },
817 .expect_stderr_match => |match| {811 .expect_stderr_match => |match| {
818 assert(!result.stdio.stderr_null);812 if (mem.indexOf(u8, result.stdio.stderr.?, match) == null) {
819 if (mem.indexOf(u8, result.stdio.stderr, match) == null) {
820 return step.fail(813 return step.fail(
821 \\814 \\
822 \\========= expected to find in stderr: =========815 \\========= expected to find in stderr: =========
...@@ -827,14 +820,13 @@ fn runCommand(...@@ -827,14 +820,13 @@ fn runCommand(
827 \\{s}820 \\{s}
828 , .{821 , .{
829 match,822 match,
830 result.stdio.stderr,823 result.stdio.stderr.?,
831 try Step.allocPrintCmd(arena, self.cwd, final_argv),824 try Step.allocPrintCmd(arena, self.cwd, final_argv),
832 });825 });
833 }826 }
834 },827 },
835 .expect_stdout_exact => |expected_bytes| {828 .expect_stdout_exact => |expected_bytes| {
836 assert(!result.stdio.stdout_null);829 if (!mem.eql(u8, expected_bytes, result.stdio.stdout.?)) {
837 if (!mem.eql(u8, expected_bytes, result.stdio.stdout)) {
838 return step.fail(830 return step.fail(
839 \\831 \\
840 \\========= expected this stdout: =========832 \\========= expected this stdout: =========
...@@ -845,14 +837,13 @@ fn runCommand(...@@ -845,14 +837,13 @@ fn runCommand(
845 \\{s}837 \\{s}
846 , .{838 , .{
847 expected_bytes,839 expected_bytes,
848 result.stdio.stdout,840 result.stdio.stdout.?,
849 try Step.allocPrintCmd(arena, self.cwd, final_argv),841 try Step.allocPrintCmd(arena, self.cwd, final_argv),
850 });842 });
851 }843 }
852 },844 },
853 .expect_stdout_match => |match| {845 .expect_stdout_match => |match| {
854 assert(!result.stdio.stdout_null);846 if (mem.indexOf(u8, result.stdio.stdout.?, match) == null) {
855 if (mem.indexOf(u8, result.stdio.stdout, match) == null) {
856 return step.fail(847 return step.fail(
857 \\848 \\
858 \\========= expected to find in stdout: =========849 \\========= expected to find in stdout: =========
...@@ -863,7 +854,7 @@ fn runCommand(...@@ -863,7 +854,7 @@ fn runCommand(
863 \\{s}854 \\{s}
864 , .{855 , .{
865 match,856 match,
866 result.stdio.stdout,857 result.stdio.stdout.?,
867 try Step.allocPrintCmd(arena, self.cwd, final_argv),858 try Step.allocPrintCmd(arena, self.cwd, final_argv),
868 });859 });
869 }860 }
...@@ -982,12 +973,8 @@ fn spawnChildAndCollect(...@@ -982,12 +973,8 @@ fn spawnChildAndCollect(
982}973}
983974
984const StdIoResult = struct {975const StdIoResult = struct {
985 // These use boolean flags instead of optionals as a workaround for976 stdout: ?[]const u8,
986 // https://github.com/ziglang/zig/issues/14783977 stderr: ?[]const u8,
987 stdout: []const u8,
988 stderr: []const u8,
989 stdout_null: bool,
990 stderr_null: bool,
991 test_results: Step.TestResults,978 test_results: Step.TestResults,
992 test_metadata: ?TestMetadata,979 test_metadata: ?TestMetadata,
993};980};
...@@ -1115,10 +1102,8 @@ fn evalZigTest(...@@ -1115,10 +1102,8 @@ fn evalZigTest(
1115 child.stdin = null;1102 child.stdin = null;
11161103
1117 return .{1104 return .{
1118 .stdout = &.{},1105 .stdout = null,
1119 .stderr = &.{},1106 .stderr = null,
1120 .stdout_null = true,
1121 .stderr_null = true,
1122 .test_results = .{1107 .test_results = .{
1123 .test_count = test_count,1108 .test_count = test_count,
1124 .fail_count = fail_count,1109 .fail_count = fail_count,
...@@ -1204,12 +1189,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {...@@ -1204,12 +1189,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {
1204 .none => {},1189 .none => {},
1205 }1190 }
12061191
1207 // These are not optionals, as a workaround for1192 var stdout_bytes: ?[]const u8 = null;
1208 // https://github.com/ziglang/zig/issues/147831193 var stderr_bytes: ?[]const u8 = null;
1209 var stdout_bytes: []const u8 = undefined;
1210 var stderr_bytes: []const u8 = undefined;
1211 var stdout_null = true;
1212 var stderr_null = true;
12131194
1214 if (child.stdout) |stdout| {1195 if (child.stdout) |stdout| {
1215 if (child.stderr) |stderr| {1196 if (child.stderr) |stderr| {
...@@ -1228,33 +1209,27 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {...@@ -1228,33 +1209,27 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {
12281209
1229 stdout_bytes = try poller.fifo(.stdout).toOwnedSlice();1210 stdout_bytes = try poller.fifo(.stdout).toOwnedSlice();
1230 stderr_bytes = try poller.fifo(.stderr).toOwnedSlice();1211 stderr_bytes = try poller.fifo(.stderr).toOwnedSlice();
1231 stdout_null = false;
1232 stderr_null = false;
1233 } else {1212 } else {
1234 stdout_bytes = try stdout.reader().readAllAlloc(arena, self.max_stdio_size);1213 stdout_bytes = try stdout.reader().readAllAlloc(arena, self.max_stdio_size);
1235 stdout_null = false;
1236 }1214 }
1237 } else if (child.stderr) |stderr| {1215 } else if (child.stderr) |stderr| {
1238 stderr_bytes = try stderr.reader().readAllAlloc(arena, self.max_stdio_size);1216 stderr_bytes = try stderr.reader().readAllAlloc(arena, self.max_stdio_size);
1239 stderr_null = false;
1240 }1217 }
12411218
1242 if (!stderr_null and stderr_bytes.len > 0) {1219 if (stderr_bytes) |bytes| if (bytes.len > 0) {
1243 // Treat stderr as an error message.1220 // Treat stderr as an error message.
1244 const stderr_is_diagnostic = self.captured_stderr == null and switch (self.stdio) {1221 const stderr_is_diagnostic = self.captured_stderr == null and switch (self.stdio) {
1245 .check => |checks| !checksContainStderr(checks.items),1222 .check => |checks| !checksContainStderr(checks.items),
1246 else => true,1223 else => true,
1247 };1224 };
1248 if (stderr_is_diagnostic) {1225 if (stderr_is_diagnostic) {
1249 try self.step.result_error_msgs.append(arena, stderr_bytes);1226 try self.step.result_error_msgs.append(arena, bytes);
1250 }1227 }
1251 }1228 };
12521229
1253 return .{1230 return .{
1254 .stdout = stdout_bytes,1231 .stdout = stdout_bytes,
1255 .stderr = stderr_bytes,1232 .stderr = stderr_bytes,
1256 .stdout_null = stdout_null,
1257 .stderr_null = stderr_null,
1258 .test_results = .{},1233 .test_results = .{},
1259 .test_metadata = null,1234 .test_metadata = null,
1260 };1235 };