~andrewrk/ziglang

1

Function pointer being called as normal even though it's an async function

Details
Message ID
<20210515141530.mbbix6vsj5crknrp@host.localdomain>
DKIM signature
missing
Download raw message
Hi! I'm having an issue which took like 2 days to diagnose which I
believe is a compiler issue. I mentioned it in #zig @ freenode but it
didn't get much attention.

Here's the code sample:

```zig
pub const io_mode = .evented;

const std = @import("std");

const Channel = std.event.Channel(u32);
const S2 = struct {
    exampleFn: fn (self: *S2) u32 = example,

    channel: Channel,
    buffer: [4]u32,
};

pub fn main() !void {
    var s2 = S2{
        .channel = undefined,
        .buffer = undefined,
    };
    s2.channel.init(&s2.buffer);
    std.debug.print("{}\n", .{@ptrToInt(&s2)});

    // Comment out the one to test

    // Calling the function directly, works fine
    //_ = example(&s2);

    // Calling the function pointer, fails weirdly (UB?)
    //_ = s2.exampleFn(&s2);

    // Forcing it to run as async, works fine
    //const funcType = fn (self: *S2) callconv(.Async) u32;
    //var testt: [640]u8 align(@alignOf(@Frame(example))) = undefined;
    //var result: u32 = undefined;
    //_ = await @asyncCall(&testt, &result, @ptrCast(funcType, s2.exampleFn), .{&s2});

    while (true) {}
}

// Actually async because of .get()
fn example(s2: *S2) u32 {
    std.debug.print("{}\n", .{@ptrToInt(s2)});
    return s2.channel.get();
}
```

I believe this is because it believes it's a normal function according
to the type of the function pointer. No clue how this could be solved.
Details
Message ID
<CAAP90jMWTvm9yxp3g7zhR+bo=6jiAN7p56+sw3s0mPTSjHwVjw@mail.gmail.com>
In-Reply-To
<20210515141530.mbbix6vsj5crknrp@host.localdomain> (view parent)
DKIM signature
missing
Download raw message
[Resending in plain text mode.]

Hey, I just wanted to say that you may get more traction simply
opening an issue on the zig github repo.

Cheers.
Gonzo


On Sat, 15 May 2021 at 16:15, void <v0id@riseup.net> wrote:
>
> Hi! I'm having an issue which took like 2 days to diagnose which I
> believe is a compiler issue. I mentioned it in #zig @ freenode but it
> didn't get much attention.
>
> Here's the code sample:
>
> ```zig
> pub const io_mode = .evented;
>
> const std = @import("std");
>
> const Channel = std.event.Channel(u32);
> const S2 = struct {
>     exampleFn: fn (self: *S2) u32 = example,
>
>     channel: Channel,
>     buffer: [4]u32,
> };
>
> pub fn main() !void {
>     var s2 = S2{
>         .channel = undefined,
>         .buffer = undefined,
>     };
>     s2.channel.init(&s2.buffer);
>     std.debug.print("{}\n", .{@ptrToInt(&s2)});
>
>     // Comment out the one to test
>
>     // Calling the function directly, works fine
>     //_ = example(&s2);
>
>     // Calling the function pointer, fails weirdly (UB?)
>     //_ = s2.exampleFn(&s2);
>
>     // Forcing it to run as async, works fine
>     //const funcType = fn (self: *S2) callconv(.Async) u32;
>     //var testt: [640]u8 align(@alignOf(@Frame(example))) = undefined;
>     //var result: u32 = undefined;
>     //_ = await @asyncCall(&testt, &result, @ptrCast(funcType, s2.exampleFn), .{&s2});
>
>     while (true) {}
> }
>
> // Actually async because of .get()
> fn example(s2: *S2) u32 {
>     std.debug.print("{}\n", .{@ptrToInt(s2)});
>     return s2.channel.get();
> }
> ```
>
> I believe this is because it believes it's a normal function according
> to the type of the function pointer. No clue how this could be solved.



-- 
Gonzalo Diethelm
gonzalo.diethelm@gmail.com
Reply to thread Export thread (mbox)