commit 17255bed41a4b2c31a3f4342d51aa80a6f9d6b0b (tree)
parent 996eb01746498e0ec5e162ed40d1f3c913891150
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date: Thu, 20 Jul 2023 02:47:26 -0400
Merge pull request #16443 from notcancername/cbe-empty-enum
cbe: fix bug where empty enum would be generated
Diffstat:
2 files changed, 47 insertions(+), 16 deletions(-)
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
@@ -2406,22 +2406,24 @@ pub fn genErrDecls(o: *Object) !void {
const mod = o.dg.module;
const writer = o.writer();
- try writer.writeAll("enum {\n");
- o.indent_writer.pushIndent();
var max_name_len: usize = 0;
- for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {
- const name = mod.intern_pool.stringToSlice(name_nts);
- max_name_len = @max(name.len, max_name_len);
- const err_val = try mod.intern(.{ .err = .{
- .ty = .anyerror_type,
- .name = name_nts,
- } });
- try o.dg.renderValue(writer, Type.anyerror, err_val.toValue(), .Other);
- try writer.print(" = {d}u,\n", .{value});
+ // do not generate an invalid empty enum when the global error set is empty
+ if (mod.global_error_set.keys().len > 1) {
+ try writer.writeAll("enum {\n");
+ o.indent_writer.pushIndent();
+ for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| {
+ const name = mod.intern_pool.stringToSlice(name_nts);
+ max_name_len = @max(name.len, max_name_len);
+ const err_val = try mod.intern(.{ .err = .{
+ .ty = .anyerror_type,
+ .name = name_nts,
+ } });
+ try o.dg.renderValue(writer, Type.anyerror, err_val.toValue(), .Other);
+ try writer.print(" = {d}u,\n", .{value});
+ }
+ o.indent_writer.popIndent();
+ try writer.writeAll("};\n");
}
- o.indent_writer.popIndent();
- try writer.writeAll("};\n");
-
const array_identifier = "zig_errorName";
const name_prefix = array_identifier ++ "_";
const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len);
diff --git a/test/src/Cases.zig b/test/src/Cases.zig
@@ -467,6 +467,9 @@ pub fn lowerToBuildSteps(
cases_dir_path: []const u8,
incremental_exe: *std.Build.Step.Compile,
) void {
+ const host = std.zig.system.NativeTargetInfo.detect(.{}) catch |err|
+ std.debug.panic("unable to detect notive host: {s}\n", .{@errorName(err)});
+
for (self.incremental_cases.items) |incr_case| {
if (true) {
// TODO: incremental tests are disabled for now, as incremental compilation bugs were
@@ -569,8 +572,34 @@ pub fn lowerToBuildSteps(
artifact.expect_errors = expected_msgs;
parent_step.dependOn(&artifact.step);
},
- .Execution => |expected_stdout| {
- const run = b.addRunArtifact(artifact);
+ .Execution => |expected_stdout| no_exec: {
+ const run = if (case.target.ofmt == .c) run_step: {
+ const target_info = std.zig.system.NativeTargetInfo.detect(case.target) catch |err|
+ std.debug.panic("unable to detect notive host: {s}\n", .{@errorName(err)});
+ if (host.getExternalExecutor(target_info, .{ .link_libc = true }) != .native) {
+ // We wouldn't be able to run the compiled C code.
+ break :no_exec;
+ }
+ const run_c = b.addSystemCommand(&.{
+ b.zig_exe,
+ "run",
+ "-cflags",
+ "-Ilib",
+ "-std=c99",
+ "-pedantic",
+ "-Werror",
+ "-Wno-dollar-in-identifier-extension",
+ "-Wno-incompatible-library-redeclaration", // https://github.com/ziglang/zig/issues/875
+ "-Wno-incompatible-pointer-types",
+ "-Wno-overlength-strings",
+ "--",
+ "-lc",
+ "-target",
+ case.target.zigTriple(b.allocator) catch @panic("OOM"),
+ });
+ run_c.addArtifactArg(artifact);
+ break :run_step run_c;
+ } else b.addRunArtifact(artifact);
run.skip_foreign_checks = true;
if (!case.is_test) {
run.expectStdOutEqual(expected_stdout);