zig icon indicating copy to clipboard operation
zig copied to clipboard

Miscompilation with struct result from orelse

Open pfgithub opened this issue 2 months ago • 0 comments

Zig Version

0.13.0-dev.75+5c9eb4081

Steps to Reproduce and Observed Behavior

// a.zig
const std = @import("std");

pub const Selection = struct {
    value: u8,
};
pub const CursorPosition = struct {
    pos: Selection,
    opt_pos: ?Selection = null,
};

test "miscompilation" {
    var self = CursorPosition {
        .pos = .{.value = 3},
    };
    const start_pos = self.opt_pos orelse self.pos;

    try std.testing.expectEqual(@as(u8, 3), start_pos.value); // OK
    self.pos.value = 10;
    try std.testing.expectEqual(@as(u8, 3), start_pos.value); // Error
}

zig test a.zig

Expected Behavior

Test should pass. Modifying self.pos.value should have no effect on start_pos because it is copied into the stack.

pfgithub avatar May 09 '24 01:05 pfgithub