zig icon indicating copy to clipboard operation
zig copied to clipboard

Exporting declarations directly through `@field` results in a false error

Open moosichu opened this issue 3 years ago • 0 comments

Zig Version

0.11.0-dev.6840+28288dcbb

Steps to Reproduce and Observed Behavior

This snippet results in a compile failure:

comptime {
    const prefix = "myprefix_";
    for (@typeInfo(@This()).Struct.decls) |declaration| {
        if (declaration.is_pub) {
            @export(@field(@This(), declaration.name), .{ .name = prefix ++ declaration.name });
        }
    }
}

pub fn Test() callconv(.C) void {}

The error being:

test.zig:8:21: error: symbol to export must identify a declaration
            @export(@field(@This(), declaration.name), .{ .name = prefix ++ declaration.name });
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For the record, introducing an intermediate variable results in a compilation success:

comptime {
    const prefix = "myprefix_";
    for (@typeInfo(@This()).Struct.decls) |declaration| {
        if (declaration.is_pub) {
            const test_decl = @field(@This(), declaration.name);
            @export(test_decl, .{ .name = prefix ++ declaration.name });
        }
    }
}

pub fn Test() callconv(.C) void {}

Expected Behavior

The above snippet to successfully compile without an error - correctly exporting the declaration.

moosichu avatar Nov 05 '22 17:11 moosichu