Am I crazy or is this a compiler bug?

            std.debug.print("new index: {}\n", .{index});
            std.debug.print("self.anchor.index: {}\n", .{self.anchor.index});
            self.anchor = .{
                .index = index,
                .offset = @divTrunc(
                    self.anchor.offset * @as(isize, @intCast(index)),
                    @as(isize, @intCast(self.anchor.index)),
                ),
            };
new index: 0
self.anchor.index: 3
thread 514712 panic: division by zero
/home/ra/Documents/zig-stuff/decent-ui/src/components/List.zig:81:27: 0x1382c73 in layout (root.zig)
                .offset = @divTrunc(
                          ^

if i save self.anchor.index in a variable it goes away.
it seems as if .index = index is immediately setting self.anchor.index ???

Is this really a compiler bug?
If this is working as expected, why? this seems like a footgun to me.

full source: https://git.sr.ht/~ra/decent-ui/tree/compiler-bug/item/src/components/List.zig
zig version: 0.15.1

1 Like

Yes, currently .{} is not encapsulated. This, combined with the result positional semantics, leads to this behavior.
A workaround for now is to change it to:

            self.anchor = Anchor{
                .index = index,
                .offset = @divTrunc(
                    self.anchor.offset * @as(isize, @intCast(index)),
                    @as(isize, @intCast(self.anchor.index)),
                ),
            };

Currently T{} is designed to encapsulate.(Although the T{} syntax itself may be removed in the future)

Related:

4 Likes

Do you happen to have a link for that? I’d just like to keep an eye on it.

3 Likes

Thank you!

Thanks for the fast answer. Would not have found that myself. Good to see that they are working on this, even if this is confusing right now.