diff --git a/src/reflect.zig b/src/reflect.zig index f01b4a8..8f63268 100644 --- a/src/reflect.zig +++ b/src/reflect.zig @@ -229,6 +229,14 @@ pub const Type = struct { under_ptr = info.Pointer.child; } + // variadic types must be optional + if (under_opt == null) { + const under = under_ptr orelse T; + if (Type._is_variadic(under)) { + return error.TypeVariadicNotOptional; + } + } + var t = Type{ .T = T, .name = name, @@ -1238,6 +1246,7 @@ const Error = error{ TypeTaggedUnion, TypeNestedPtr, TypeVariadicNested, // TODO: test + TypeVariadicNotOptional, TypeLookup, }; @@ -1336,10 +1345,10 @@ const TestFuncMultiCbk = struct { }; const VariadicBool = Variadic(bool); const TestFuncVariadicNotLastOne = struct { - pub fn _example(_: TestFuncVariadicNotLastOne, _: VariadicBool, _: bool) void {} + pub fn _example(_: TestFuncVariadicNotLastOne, _: ?VariadicBool, _: bool) void {} }; const TestFuncReturnTypeVariadic = struct { - pub fn _example(_: TestFuncReturnTypeVariadic) VariadicBool {} + pub fn _example(_: TestFuncReturnTypeVariadic) ?VariadicBool {} }; // types tests @@ -1354,6 +1363,9 @@ const TestTypeNestedPtr = struct { pub const TestTypeNestedBase = struct {}; pub fn _example(_: TestTypeNestedPtr, _: *TestTypeNestedBase) void {} }; +const TestTypeVariadicNotOptional = struct { + pub fn _example(_: TestTypeVariadicNotOptional, _: VariadicBool) void {} +}; const TestType = struct {}; const TestTypeLookup = struct { pub fn _example(_: TestTypeLookup, _: TestType) void {} @@ -1464,6 +1476,10 @@ pub fn tests() !void { .{TestTypeNestedPtr}, error.TypeNestedPtr, ); + try ensureErr( + .{TestTypeVariadicNotOptional}, + error.TypeVariadicNotOptional, + ); try ensureErr( .{TestTypeLookup}, error.TypeLookup, diff --git a/src/tests/types_complex_test.zig b/src/tests/types_complex_test.zig index 22d8ffd..05a19f2 100644 --- a/src/tests/types_complex_test.zig +++ b/src/tests/types_complex_test.zig @@ -34,16 +34,20 @@ const MyVariadic = struct { return .{ .member = 0 }; } - pub fn _len(_: MyVariadic, variadic: VariadicBool) u64 { - return @as(u64, variadic.slice.len); + pub fn _len(_: MyVariadic, variadic: ?VariadicBool) u64 { + return @as(u64, variadic.?.slice.len); } - pub fn _first(_: MyVariadic, _: []const u8, variadic: VariadicBool) bool { - return variadic.slice[0]; + pub fn _first(_: MyVariadic, _: []const u8, variadic: ?VariadicBool) bool { + return variadic.?.slice[0]; } - pub fn _last(_: MyVariadic, variadic: VariadicBool) bool { - return variadic.slice[variadic.slice.len - 1]; + pub fn _last(_: MyVariadic, variadic: ?VariadicBool) bool { + return variadic.?.slice[variadic.?.slice.len - 1]; + } + + pub fn _empty(_: MyVariadic, _: ?VariadicBool) bool { + return true; } }; @@ -82,6 +86,7 @@ pub fn exec( .{ .src = "myVariadic.len(true, false, true)", .ex = "3" }, .{ .src = "myVariadic.first('a_str', true, false, true, false)", .ex = "true" }, .{ .src = "myVariadic.last(true, false)", .ex = "false" }, + .{ .src = "myVariadic.empty()", .ex = "true" }, }; try tests.checkCases(js_env, &variadic); }