Skip to content

introduce @hasField builtin function #1439

@shawnl

Description

@shawnl

Accepted Proposal


Using a undefined symbol is a compiler error:

/home/shawn/git/zig/std/os/linux/index.zig:840:21: error: use of undeclared identifier 'SYS_mmap2'
    return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset / 4096));

It is possible to work around this by using buildin.arch and listing which arches have mmap2 (the 32-bit ones)

    if (builtin.arch == builtin.Arch.armv7) {
        return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset / 4096));

But instead of making all these assumption with code like that it would be cleaner to be able to comptime branch of a identifier existing:

    if (@defined(SYS_mmap2)) {
        return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset / 4096));
    } else {
        return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions