Skip to content

don't do feature detection with @hasDecl #19352

Closed
@andrewrk

Description

@andrewrk

Violations:

../lib/std/os.zig:pub const have_sigpipe_support = @hasDecl(@This(), "SIG") and @hasDecl(SIG, "PIPE");
../lib/std/os.zig:    if (@hasDecl(system, "pipe2")) {
../lib/std/debug.zig:pub const have_ucontext = @hasDecl(os.system, "ucontext_t") and
../lib/std/debug.zig:pub const have_getcontext = @hasDecl(os.system, "getcontext") and
../lib/std/debug.zig:        } else if (@hasDecl(os.system, "msync") and native_os != .wasi and native_os != .emscripten) {
../lib/std/debug.zig:        if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
../lib/std/debug.zig:    .freebsd, .openbsd => @hasDecl(os.system, "ucontext_t"),
../lib/std/debug.zig:    if (@hasDecl(windows, "CONTEXT")) {
../lib/std/os/test.zig:const dl_phdr_info = if (@hasDecl(os.system, "dl_phdr_info")) os.dl_phdr_info else anyopaque;
../lib/std/os/test.zig:    if (!@hasDecl(os.system, "rlimit")) {
../lib/std/os/uefi/tables/boot_services.zig:        if (!@hasDecl(protocol, "guid"))
../lib/std/os/linux.zig:    if (@hasDecl(VDSO, "CGT_SYM")) {
../lib/std/crypto/tlcsprng.zig:const os_has_arc4random = builtin.link_libc and @hasDecl(std.c, "arc4random_buf");
../lib/std/crypto/tlcsprng.zig:    if (builtin.link_libc and @hasDecl(std.c, "arc4random_buf")) {
../lib/std/heap.zig:    pub const malloc_size = if (@hasDecl(c, "malloc_size"))
../lib/std/heap.zig:    else if (@hasDecl(c, "malloc_usable_size"))
../lib/std/heap.zig:    else if (@hasDecl(c, "_msize"))
../lib/std/heap.zig:    pub const supports_posix_memalign = @hasDecl(c, "posix_memalign");
../lib/std/fs/File.zig:        if (!@hasDecl(@TypeOf(self.stat), "birthtime")) return null;
../lib/std/fs/Dir.zig:                const next_index = self.index + if (@hasDecl(posix.system.dirent, "reclen")) bsd_entry.reclen() else bsd_entry.reclen;
../lib/std/fs/Dir.zig:    if (@hasDecl(posix.system, "LOCK")) {
../lib/std/builtin.zig:    if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem;

Marking as bug because there's probably a bug among these instances. This is a brittle way to do feature detection. As a general rule: don't do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions