Skip to content
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

if expression doesn't coerce to slice in anonymous struct #7197

Closed
mkeeter opened this issue Nov 22, 2020 · 1 comment
Closed

if expression doesn't coerce to slice in anonymous struct #7197

mkeeter opened this issue Nov 22, 2020 · 1 comment
Milestone

Comments

@mkeeter
Copy link
Contributor

mkeeter commented Nov 22, 2020

const std = @import("std");

pub fn main() !void {
    // Read from user input to convince compiler to not optimize out conditional
    const stdin = std.io.getStdIn().reader();
    var buf: [10]u8 = undefined;
    const n = try stdin.read(&buf);

    const f = if (n % 2 == 0) "hi" else "world"; // works
    const g = .{if (n % 2 == 0) "hi" else "world"}; // doesn't work
    std.debug.print("{}, {}\n", .{ f, g });
}

f correctly recognizes that it should be a []const u8, but that coercion doesn't happen inside of the anonymous struct g, which is unexpected!

@ghost
Copy link

ghost commented Nov 22, 2020

Duplicate of #3882

@andrewrk andrewrk added this to the 0.8.0 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants