Skip to content

Commit 17255be

Browse files
authored
Merge pull request #16443 from notcancername/cbe-empty-enum
cbe: fix bug where empty enum would be generated
2 parents 996eb01 + e7ea7d3 commit 17255be

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

src/codegen/c.zig

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,22 +2406,24 @@ pub fn genErrDecls(o: *Object) !void {
24062406
const mod = o.dg.module;
24072407
const writer = o.writer();
24082408

2409-
try writer.writeAll("enum {\n");
2410-
o.indent_writer.pushIndent();
24112409
var max_name_len: usize = 0;
2412-
for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {
2413-
const name = mod.intern_pool.stringToSlice(name_nts);
2414-
max_name_len = @max(name.len, max_name_len);
2415-
const err_val = try mod.intern(.{ .err = .{
2416-
.ty = .anyerror_type,
2417-
.name = name_nts,
2418-
} });
2419-
try o.dg.renderValue(writer, Type.anyerror, err_val.toValue(), .Other);
2420-
try writer.print(" = {d}u,\n", .{value});
2410+
// do not generate an invalid empty enum when the global error set is empty
2411+
if (mod.global_error_set.keys().len > 1) {
2412+
try writer.writeAll("enum {\n");
2413+
o.indent_writer.pushIndent();
2414+
for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {
2415+
const name = mod.intern_pool.stringToSlice(name_nts);
2416+
max_name_len = @max(name.len, max_name_len);
2417+
const err_val = try mod.intern(.{ .err = .{
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");
24212426
}
2422-
o.indent_writer.popIndent();
2423-
try writer.writeAll("};\n");
2424-
24252427
const array_identifier = "zig_errorName";
24262428
const name_prefix = array_identifier ++ "_";
24272429
const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len);

test/src/Cases.zig

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ pub fn lowerToBuildSteps(
467467
cases_dir_path: []const u8,
468468
incremental_exe: *std.Build.Step.Compile,
469469
) 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+
470473
for (self.incremental_cases.items) |incr_case| {
471474
if (true) {
472475
// TODO: incremental tests are disabled for now, as incremental compilation bugs were
@@ -569,8 +572,34 @@ pub fn lowerToBuildSteps(
569572
artifact.expect_errors = expected_msgs;
570573
parent_step.dependOn(&artifact.step);
571574
},
572-
.Execution => |expected_stdout| {
573-
const run = b.addRunArtifact(artifact);
575+
.Execution => |expected_stdout| no_exec: {
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);
574603
run.skip_foreign_checks = true;
575604
if (!case.is_test) {
576605
run.expectStdOutEqual(expected_stdout);

0 commit comments

Comments
 (0)