Skip to content

Commit

Permalink
variadic: enforce as Optional type
Browse files Browse the repository at this point in the history
Signed-off-by: Francis Bouvier <[email protected]>
  • Loading branch information
francisbouvier committed Oct 6, 2023
1 parent 195352b commit ee6e992
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/reflect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1238,6 +1246,7 @@ const Error = error{
TypeTaggedUnion,
TypeNestedPtr,
TypeVariadicNested, // TODO: test
TypeVariadicNotOptional,
TypeLookup,
};

Expand Down Expand Up @@ -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
Expand All @@ -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 {}
Expand Down Expand Up @@ -1464,6 +1476,10 @@ pub fn tests() !void {
.{TestTypeNestedPtr},
error.TypeNestedPtr,
);
try ensureErr(
.{TestTypeVariadicNotOptional},
error.TypeVariadicNotOptional,
);
try ensureErr(
.{TestTypeLookup},
error.TypeLookup,
Expand Down
17 changes: 11 additions & 6 deletions src/tests/types_complex_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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);
}

0 comments on commit ee6e992

Please sign in to comment.