Skip to content

Commit 43770c0

Browse files
squeek502Vexu
authored andcommitted
Fix checkAllAllocationFailures being too strict when checking arg types
Before this would fail to compile: ``` fn testFn(alloc: std.mem.Allocator, arr: []const u8) !void { _ = alloc; _ = arr; } test "checkAll" { var arr = [_]u8{ 1, 2, 3 }; try std.testing.checkAllAllocationFailures(std.testing.allocator, testFn, .{arr[0..]}); } ``` with the error `error: Unexpected type for extra argument at index 0: expected []const u8, found *[3]u8` By removing this strict equality check, we allow the type checking to be done during the `@field(args, arg_i_str) = @field(extra_args, field.name);` instead, which then allows for things like type coercion to work, but still will give a compile error if the types are incorrect. So, after this change, the above succeeds (because `*[3]u8` can be coerced to `[]const u8`). The new compile error when providing an incorrect type that can't be coerced looks like this: ``` zig/lib/std/testing.zig:639:35: error: expected type '[]const u8', found '*[3]u32' @field(args, arg_i_str) = @field(extra_args, field.name); ^ ```
1 parent b2486fb commit 43770c0

File tree

1 file changed

+0
-4
lines changed

1 file changed

+0
-4
lines changed

lib/std/testing.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,6 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
664664
// the failing allocator in field @"0" before each @call)
665665
var args: ArgsTuple = undefined;
666666
inline for (@typeInfo(@TypeOf(extra_args)).Struct.fields) |field, i| {
667-
const expected_type = fn_args_fields[i + 1].field_type;
668-
if (expected_type != field.field_type) {
669-
@compileError("Unexpected type for extra argument at index " ++ (comptime std.fmt.comptimePrint("{d}", .{i})) ++ ": expected " ++ @typeName(expected_type) ++ ", found " ++ @typeName(field.field_type));
670-
}
671667
const arg_i_str = comptime str: {
672668
var str_buf: [100]u8 = undefined;
673669
const args_i = i + 1;

0 commit comments

Comments
 (0)