Skip to content

Peer type resolution cannot handle nested branches with a nullable struct result #3750

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

Closed
ghost opened this issue Nov 23, 2019 · 1 comment
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@ghost
Copy link

ghost commented Nov 23, 2019

It appears that the peer type resolution is able to infer a nullable struct result in a simple one-level if or switch expression, but not a nested one.

However it does work if the result is a bool, i32, or []const u8.

pub fn main() void {
    const Thing = struct { n: i32 };
    var a = false;
    var b = false;

    // this works
    var result1: ?Thing =
        if (a)
            Thing { .n = 1 }
        else
            null;

    // this does not work
    var result2: ?Thing =
        if (a)
            Thing { .n = 0 }
        else if (b)
            Thing { .n = 1 }
        else
            null;
}
Semantic Analysis [705/709] /home/dbandstra/host/asdf.zig:20:13: error: expected type 'Thing', found '(null)'
            null;
            ^
/home/dbandstra/host/asdf.zig:2:19: note: Thing declared here
    const Thing = struct { n: i32 };
                  ^

Edit: it even fails if I do an explicit cast on the null in the last branch:

pub fn main() void {
    const Thing = struct { n: i32 };
    var a = false;
    var b = false;

    var result2: ?Thing =
        if (a)
            Thing { .n = 0 }
        else if (b)
            Thing { .n = 1 }
        else
            @as(?Thing, null);
}
Semantic Analysis [722/740] /home/dbandstra/host/asdf.zig:13:13: error: expected type 'Thing', found '?Thing'
            @as(?Thing, null);
            ^
/home/dbandstra/host/asdf.zig:2:19: note: Thing declared here
    const Thing = struct { n: i32 };
                  ^

To get it to work I need to cast the FIRST branch result, like @as(?Thing, Thing { .n = 0 }) .

Edit 2: actually, if I do that, I get runtime glitches.

pub fn main() void {
    const Thing = struct { n: i32 };
    var a = true;
    var b = false;

    var result2: ?Thing =
        if (a)
            @as(?Thing, Thing { .n = 0 })
        else if (b)
            Thing { .n = 1 }
        else
            null;

    if (result2) |r|
        @import("std").debug.warn("{}\n", r);
}

This program sometimes prints nothing, sometimes prints things like Thing{ .n = 1534746352 }.

edit: fixed typo

@daurnimator daurnimator added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Nov 24, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Nov 27, 2019
@ghost
Copy link
Author

ghost commented Jan 20, 2020

This is probably mislabeled, I think it's the result location type failing to carry into nested conditional expressions, not peer type resolution.

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 4, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 29, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 29, 2022
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

2 participants