Skip to content

Can't branch inside tuple literal (peer result location does not work for inferred tuple literal fields) #3882

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 Dec 10, 2019 · 6 comments
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 Dec 10, 2019

Update: this has been narrowed down since OP. See andrewrk's last comment


const std = @import("std");

pub fn main() void {
    var good = true;
    std.debug.warn("I feel {}\n", .{if (good) "good" else "bad"});
}
Semantic Analysis [758/776] /home/dbandstra/host/asdf.zig:5:47: error: duplicate struct field: '0'
    std.debug.warn("I feel {}\n", .{if (good) "good" else "bad"});
                                              ^
/home/dbandstra/host/asdf.zig:5:47: note: other field here
    std.debug.warn("I feel {}\n", .{if (good) "good" else "bad"});
                                              ^

These also don't work (different problem):

std.debug.warn("I feel {}\n", if (good) .{"good"} else .{"bad"});
std.debug.warn("I feel {}\n", if (good) .{&"good"} else .{&"bad"});
Semantic Analysis [483/672] /home/dbandstra/host/asdf.zig:5:35: error: incompatible types: 'struct:5:46' and 'struct:5:61'
    std.debug.warn("I feel {}\n", if (good) .{"good"} else .{"bad"});
                                  ^
/home/dbandstra/host/asdf.zig:5:35: note: type 'struct:5:46' here
    std.debug.warn("I feel {}\n", if (good) .{"good"} else .{"bad"});
                                  ^
/home/dbandstra/host/asdf.zig:5:35: note: type 'struct:5:61' here
    std.debug.warn("I feel {}\n", if (good) .{"good"} else .{"bad"});
                                  ^

Switch expressions don't work either.

These work (forcing a list type):

std.debug.warn("I feel {}\n", [_][]const u8{ if (good) "good" else "bad" });
std.debug.warn("I feel {}\n", if (good) [1][]const u8{ "good" } else [1][]const u8{ "bad" });
@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 Dec 10, 2019
@giann
Copy link
Contributor

giann commented Dec 12, 2019

Happens to with orelse:

const std = @import("std");

test "orelse in tuple" {
    var opt: ?u32 = 1;
    std.debug.warn("A tuple: ({}, {}, {})\n", .{ 1, 2, opt orelse 3 });
}
λ zig test tuples.zig

/Users/giann/tmp-code/tuples.zig:5:67: error: duplicate struct field: '2'
    std.debug.warn("A tuple: ({}, {}, {})\n", .{ 1, 2, opt orelse 3 });
                                                                  ^
/Users/giann/tmp-code/tuples.zig:5:67: note: other field here
    std.debug.warn("A tuple: ({}, {}, {})\n", .{ 1, 2, opt orelse 3 });

deingithub added a commit to deingithub/OidaVM that referenced this issue Dec 18, 2019
This is currently broken because of ziglang/zig#3882,
should work after it's been fixed though.
Can't be bothered to work around that bug.
@andrewrk andrewrk added this to the 0.7.0 milestone Dec 31, 2019
@ghost
Copy link
Author

ghost commented Jan 2, 2020

The workaround I've been using (below) no longer works as of 2505183.

std.debug.warn("I feel {}\n", [_][]const u8{ if (good) "good" else "bad" });

I think the only way around now is to put the conditional around the whole function call.

@andrewrk
Copy link
Member

andrewrk commented Jan 2, 2020

Sorry, will prioritize fixing this bug

@pixelherodev
Copy link
Contributor

"I think the only way around now is to put the conditional around the whole function call."

My workaround is to use a temp variable before the function call, which is slightly smaller.

e.g.

{const t = if (good) "good" else "bad";
std.debug.warn("The world is {}\n", .{t});}

Not sure if this works on master though.

@andrewrk
Copy link
Member

This now gives:

./test2.zig:5:59: error: expected type '*const [4:0]u8', found '*const [3:0]u8'
    std.debug.warn("I feel {}\n", .{if (good) "good" else "bad"});
                                                          ^
./test2.zig:5:59: note: pointer type child '[3:0]u8' cannot cast into pointer type child '[4:0]u8'
    std.debug.warn("I feel {}\n", .{if (good) "good" else "bad"});
                                                          ^

Workaround noted by @pixelherodev is:

const std = @import("std");

pub fn main() void {
    var good = true;
    const t = if (good) "good" else "bad";
    std.debug.warn("I feel {}\n", .{t});
}

Making this a normal-priority bug since the workaround is not so bad.

@EspeuteClement
Copy link
Contributor

Note : the bug is still present in 0.10.0-dev.3072+d1d892c83
If the strings are the same size, the program compiles properly but the branch is still not properly evaluated.

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

5 participants