Skip to content

Spooky action at a distance when calling a callconv(.c) function that returns an extern struct #25259

@freakmangd

Description

@freakmangd

Zig Version

0.16.0-dev.235+377a8b2a3

Steps to Reproduce and Observed Behavior

pub fn main() void {
    var cond = false;
    _ = &cond;

    const num: f32 = if (cond) 100 else 200;

    var a = f();
    _ = &a;

    std.debug.print("{}\n", .{num});
}

pub fn f() callconv(.c) A {
    return .{};
}

pub const A = extern struct {
    x: f32 = 0,
    y: f32 = 0,
    z: f32 = 0,
};

On Fedora linux x86_64, running this snippet with zig run src/main.zig outputs 0
Running with zig run src/main.zig -fllvm outputs 200

Notes:

Inserting a print before f is called gives a correct output of 200 twice.

Replacing

const num: f32 = if (cond) 100 else 200;

with

var num: f32 = 200;
if (cond) num = 100;

also gives the correct output of 200.

Expected Behavior

Expected an output of 200 with the original snippet

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-x86_6464-bit x86backend-self-hostedbugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions