authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-07-20 02:47:26-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-20 02:47:26-04:00
log17255bed41a4b2c31a3f4342d51aa80a6f9d6b0b
treefc98a4630e04378699dbf7c51513e59f385524f0
parent996eb01746498e0ec5e162ed40d1f3c913891150
parente7ea7d3480bd015053d34474a23cdf407a6a545f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16443 from notcancername/cbe-empty-enum

cbe: fix bug where empty enum would be generated

2 files changed, 47 insertions(+), 16 deletions(-)

src/codegen/c.zig+16-14
...@@ -2406,22 +2406,24 @@ pub fn genErrDecls(o: *Object) !void {...@@ -2406,22 +2406,24 @@ pub fn genErrDecls(o: *Object) !void {
2406 const mod = o.dg.module;2406 const mod = o.dg.module;
2407 const writer = o.writer();2407 const writer = o.writer();
24082408
2409 try writer.writeAll("enum {\n");
2410 o.indent_writer.pushIndent();
2411 var max_name_len: usize = 0;2409 var max_name_len: usize = 0;
2412 for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {2410 // do not generate an invalid empty enum when the global error set is empty
2413 const name = mod.intern_pool.stringToSlice(name_nts);2411 if (mod.global_error_set.keys().len > 1) {
2414 max_name_len = @max(name.len, max_name_len);2412 try writer.writeAll("enum {\n");
2415 const err_val = try mod.intern(.{ .err = .{2413 o.indent_writer.pushIndent();
2416 .ty = .anyerror_type,2414 for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {
2417 .name = name_nts,2415 const name = mod.intern_pool.stringToSlice(name_nts);
2418 } });2416 max_name_len = @max(name.len, max_name_len);
2419 try o.dg.renderValue(writer, Type.anyerror, err_val.toValue(), .Other);2417 const err_val = try mod.intern(.{ .err = .{
2420 try writer.print(" = {d}u,\n", .{value});2418 .ty = .anyerror_type,
2419 .name = name_nts,
2420 } });
2421 try o.dg.renderValue(writer, Type.anyerror, err_val.toValue(), .Other);
2422 try writer.print(" = {d}u,\n", .{value});
2423 }
2424 o.indent_writer.popIndent();
2425 try writer.writeAll("};\n");
2421 }2426 }
2422 o.indent_writer.popIndent();
2423 try writer.writeAll("};\n");
2424
2425 const array_identifier = "zig_errorName";2427 const array_identifier = "zig_errorName";
2426 const name_prefix = array_identifier ++ "_";2428 const name_prefix = array_identifier ++ "_";
2427 const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len);2429 const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len);
test/src/Cases.zig+31-2
...@@ -467,6 +467,9 @@ pub fn lowerToBuildSteps(...@@ -467,6 +467,9 @@ pub fn lowerToBuildSteps(
467 cases_dir_path: []const u8,467 cases_dir_path: []const u8,
468 incremental_exe: *std.Build.Step.Compile,468 incremental_exe: *std.Build.Step.Compile,
469) void {469) void {
470 const host = std.zig.system.NativeTargetInfo.detect(.{}) catch |err|
471 std.debug.panic("unable to detect notive host: {s}\n", .{@errorName(err)});
472
470 for (self.incremental_cases.items) |incr_case| {473 for (self.incremental_cases.items) |incr_case| {
471 if (true) {474 if (true) {
472 // TODO: incremental tests are disabled for now, as incremental compilation bugs were475 // TODO: incremental tests are disabled for now, as incremental compilation bugs were
...@@ -569,8 +572,34 @@ pub fn lowerToBuildSteps(...@@ -569,8 +572,34 @@ pub fn lowerToBuildSteps(
569 artifact.expect_errors = expected_msgs;572 artifact.expect_errors = expected_msgs;
570 parent_step.dependOn(&artifact.step);573 parent_step.dependOn(&artifact.step);
571 },574 },
572 .Execution => |expected_stdout| {575 .Execution => |expected_stdout| no_exec: {
573 const run = b.addRunArtifact(artifact);576 const run = if (case.target.ofmt == .c) run_step: {
577 const target_info = std.zig.system.NativeTargetInfo.detect(case.target) catch |err|
578 std.debug.panic("unable to detect notive host: {s}\n", .{@errorName(err)});
579 if (host.getExternalExecutor(target_info, .{ .link_libc = true }) != .native) {
580 // We wouldn't be able to run the compiled C code.
581 break :no_exec;
582 }
583 const run_c = b.addSystemCommand(&.{
584 b.zig_exe,
585 "run",
586 "-cflags",
587 "-Ilib",
588 "-std=c99",
589 "-pedantic",
590 "-Werror",
591 "-Wno-dollar-in-identifier-extension",
592 "-Wno-incompatible-library-redeclaration", // https://github.com/ziglang/zig/issues/875
593 "-Wno-incompatible-pointer-types",
594 "-Wno-overlength-strings",
595 "--",
596 "-lc",
597 "-target",
598 case.target.zigTriple(b.allocator) catch @panic("OOM"),
599 });
600 run_c.addArtifactArg(artifact);
601 break :run_step run_c;
602 } else b.addRunArtifact(artifact);
574 run.skip_foreign_checks = true;603 run.skip_foreign_checks = true;
575 if (!case.is_test) {604 if (!case.is_test) {
576 run.expectStdOutEqual(expected_stdout);605 run.expectStdOutEqual(expected_stdout);