From fce3dd72783cd9991c0961905a155a689e8e073e Mon Sep 17 00:00:00 2001 From: Jacob Enders Date: Sat, 8 Aug 2026 15:47:26 -0400 Subject: [PATCH] Block systems with tags in ADD_SYSTEM comptime function --- build.zig | 22 ++++++++++++++++++++++ src/comptime_error_tests.zig | 31 +++++++++++++++++++++++++++++++ src/zflecs.zig | 7 +++++++ 3 files changed, 60 insertions(+) create mode 100644 src/comptime_error_tests.zig diff --git a/build.zig b/build.zig index ce74cf4..c4e4b70 100644 --- a/build.zig +++ b/build.zig @@ -683,4 +683,26 @@ pub fn build(b: *std.Build) void { b.installArtifact(tests); test_step.dependOn(&b.addRunArtifact(tests).step); + + const comptime_test_step = b.step("test-comptime", "Run zflecs comptime tests"); + const comptime_tests_module = b.createModule(.{ + .root_source_file = b.path("src/comptime_error_tests.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + }); + comptime_tests_module.addOptions("build-options", options); + comptime_tests_module.addIncludePath(b.path("libs/flecs")); + comptime_tests_module.linkLibrary(lib); + + const comptime_tests = b.addTest(.{ + .name = "zflecs-comptime-tests", + .root_module = comptime_tests_module, + }); + comptime_tests.expect_errors = .{ + .contains = "Tags are not allowed in system functions.", + }; + b.installArtifact(comptime_tests); + + comptime_test_step.dependOn(&comptime_tests.step); } diff --git a/src/comptime_error_tests.zig b/src/comptime_error_tests.zig new file mode 100644 index 0000000..cd818dd --- /dev/null +++ b/src/comptime_error_tests.zig @@ -0,0 +1,31 @@ +const std = @import("std"); +const ecs = @import("zflecs.zig"); +const builtin = @import("builtin"); + +const print = std.log.info; + +const Position = struct { x: f32, y: f32 }; +const Velocity = struct { x: f32, y: f32 }; + +const Apples = struct {}; + +fn move_apples_system(positions: []Position, velocities: []const Velocity, _: []const Apples) void { + for (positions, velocities) |*p, v| { + p.x += v.x; + p.y += v.y; + } +} + +test "zflecs.block_tags_systemcomptime" { + print("\n", .{}); + + const world = ecs.init(); + defer _ = ecs.fini(world); + + ecs.COMPONENT(world, Position); + ecs.COMPONENT(world, Velocity); + + ecs.TAG(world, Apples); + + _ = ecs.ADD_SYSTEM(world, "move system", ecs.OnUpdate, move_apples_system); +} diff --git a/src/zflecs.zig b/src/zflecs.zig index 2ece485..ef33aa5 100644 --- a/src/zflecs.zig +++ b/src/zflecs.zig @@ -3006,6 +3006,13 @@ pub fn SYSTEM_DESC(comptime fn_system: anytype) system_desc_t { const param_type_info = @typeInfo(p.type.?).pointer; const inout = if (param_type_info.is_const) .In else .InOut; system_desc.query.terms[i - start_index] = .{ .id = id(param_type_info.child), .inout = inout }; + + const child_info = @typeInfo(param_type_info.child).@"struct"; + if (child_info.fields.len == 0) { + @compileError( + "Tags are not allowed in system functions.", + ); + } } return system_desc;