Skip to content

checkAllAllocationFailures: provide result type to 'extra_args' #23042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Techatrix
Copy link
Contributor

The removal of anonymous struct types (#21817) has broken the following usage of std.testing.checkAllAllocationFailures:

const std = @import("std");
test {
    try std.testing.checkAllAllocationFailures(
        std.testing.allocator,
        struct {
            fn f(ally: std.mem.Allocator, params: struct {
                foo_len: u32,
                bar_len: u32,
            }) !void {
                _ = ally;
                _ = params;
            }
        }.f,
        .{
            .{
                .foo_len = 3,
                .bar_len = 5,
            },
        },
    );
}

The signature of checkAllAllocationFailures looks like this:

pub fn checkAllAllocationFailures(
    backing_allocator: std.mem.Allocator,
    comptime test_fn: anytype,
    extra_args: anytype,
) !void

The parameter types of test_fn are not provided as result types to extra_args which means that it will construct a "normal" struct type that won't be able to coerce to the @TypeOf(params) which will cause a compile error similar to this:

lib/std/testing.zig:1097:35: error: expected type 'sample.test_0__struct_1680.f__struct_1681', found 'sample.test_0__struct_1688'
        @field(args, arg_i_str) = @field(extra_args, field.name);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sample.zig:15:14: note: struct declared here
            .{
            ~^
sample.zig:6:51: note: struct declared here
            fn f(ally: std.mem.Allocator, params: struct {
                                                  ^~~~~~

This fix should also improve the compile error messages if the provide extra arguments are incorrect because the validation is performed at the callsite instead of inside of the checkAllAllocationFailures function.

I have also tested this fix with https://github.com/ziglibs/diffz which frequently uses this code pattern in its tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant