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

Bug: zig test results in no output #1103

Closed
bheads opened this Issue Jun 12, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@bheads

bheads commented Jun 12, 2018

Running: zig test test.zig results in no output. But commenting out the last two lines works as expected:

const std = @import("std");
const mem = std.mem;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
const TypeInfo = builtin.TypeInfo;

const assert = std.debug.assert;

////////////////////////// Type utils ///////////////////////

pub fn isTypeId(comptime T: var, comptime Tid: TypeId) bool {
    comptime return @typeId(asType(T)) == Tid;
}


pub fn asType(comptime T: var) type { // T may not be a type, get the type or return T
    comptime return switch(@typeId(@typeOf(T))) {
        TypeId.Type => T,
        else => @typeOf(T),
    };
}

///////////////////// Function Utils /////////////////////

pub fn isFunction(comptime T: var) bool {
    comptime return isTypeId(T, TypeId.Fn);
}

///////////////////// Pointer Utils /////////////////////

pub fn isPointer(comptime T: var) bool {    
    comptime return isTypeId(T, TypeId.Pointer);
}

pub fn pointerOf(comptime T: var) type {
    comptime if (!isPointer(T)) unreachable; // must be a pointer
    comptime return @typeInfo(asType(T)).Pointer.child;
}

///////////////////// Struct Utils /////////////////////

pub fn isStruct(comptime T: var) bool {    
    comptime return isTypeId(T, TypeId.Struct);
}

pub fn isStructPtr(comptime T: var) bool {    
    comptime return isPointer(T) and isStruct(pointerOf(T));
}

// Is there a function with this name, TODO: can this also handle unions?
pub fn structHasFunction(comptime T: var, comptime name: []const u8, comptime FT: type) bool {
    comptime if (!isStruct(T) and !isStructPtr(T)) unreachable;
    comptime if (!isFunction(FT)) unreachable; // T must be a struct and FT must be a function
    comptime const ST = asType(comptime if (isStructPtr(T)) pointerOf(T) else T); // unwrap the struct pointer

    comptime const defs = @typeInfo(ST).Struct.defs;
    comptime for(defs) |def| {           
        if (def.data == TypeInfo.Definition.Data.Fn and def.data.Fn.fn_type == FT and mem.eql(u8, name, def.name)) return true;
    };
    comptime return false;
}

test "structHasFunction" {
    assert(structHasFunction(struct { fn foo() void {}}, "foo", fn() void));
    assert(structHasFunction(struct { fn foo(a: f64, b: ?i32) void {} }, "foo", fn(f64, ?i32) void));
    assert(!structHasFunction(struct { fn foo() void {}}, "foo2", fn() void));

    // BUG: these last two lines causes the test to not report (not sure if its crashing)
    std.debug.warn("{}\n", structHasFunction(mem.Allocator, "alloc", fn(*mem.Allocator, var, var) var));
    assert(structHasFunction(mem.Allocator, "alloc", fn(*mem.Allocator, var, var) var));
}

@andrewrk andrewrk added this to the 0.3.0 milestone Jun 12, 2018

@andrewrk andrewrk added the bug label Jun 12, 2018

@mlouielu

This comment has been minimized.

mlouielu commented Jun 13, 2018

I get segmentation fault when the last two line appears, on master commit.

@mlouielu

This comment has been minimized.

mlouielu commented Jun 13, 2018

@andrewrk does this related to #758?

Since when I change the FT type to others (not var), then it will not crash.

@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 3, 2018

@mlouielu yes you are correct, this is the same main issue as #758. I will add a similar compile error for this.

@andrewrk andrewrk closed this in a11e73b Sep 3, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 3, 2018

now this code gives:

/home/andy/dev/zig/build/test.zig:74:70: error: TODO implement inferred return types https://github.com/ziglang/zig/issues/447
    std.debug.warn("{}\n", structHasFunction(mem.Allocator, "alloc", fn (*mem.Allocator, var, var) var));
                                                                     ^

#447 is currently scheduled for 0.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment