Skip to content

different behaviours when using callconv(.c) function #25452

@olafmustafar

Description

@olafmustafar

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

Very simple to reproduce:

const std = @import("std");

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

pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
    return Vec3{ .x = x, .y = y, .z = z };
}

pub fn vec3add(arg_v1: Vec3, arg_v2: Vec3) Vec3 {
    return Vec3{ .x = arg_v1.x + arg_v2.x, .y = arg_v1.y + arg_v2.y, .z = arg_v1.z + arg_v2.z };
}

pub fn vec3addConvC(arg_v1: Vec3, arg_v2: Vec3) callconv(.c) Vec3 {
    return Vec3{ .x = arg_v1.x + arg_v2.x, .y = arg_v1.y + arg_v2.y, .z = arg_v1.z + arg_v2.z };
}

pub fn main() !void {
    var position = vec3(5, 5, 0);

    var target = vec3(5, 5, 5); //this doenst happen if target is const
    target = vec3(5, 5, 5);

    std.debug.print("expected {any}\n", .{target}); // vec3{ 5,5,5 }
    position = vec3addConvC(vec3(5, 5, 0), target);
    std.debug.print("actual {any}\n", .{target}); // vec3{ 0,5,5 }
}

If we use vec3add instead of vec3addConvC, the issue doesn't happen.
If we initialize the position after the target, the issue also doens't happen.

Expected Behavior

It is expected that the target doesn't change after adding it through the vec3addConvC. The expected value is to remain {5,5,5}, but we get {0,5,5} instead.

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