#possible miscompilation of a forloop?

1 messages · Page 1 of 1 (latest)

rotund hill
#

hello,

i'm having kind of an issue with my code. Why does a for-loop changes the value of what it is iterating on?

Code :

const Node = @This();

enabled: bool = true,
position: Point = .{},
size: Point = .{},
min_size: Point = .{},

parent: ?*Node = null,
children: []const *Node = &.{},

update_layout_fn: ?UpdateLayoutFn = null,

pub fn updateLayout(
    self: *Node
) void {
    if( !self.enabled )
        return;

    if( self.update_layout_fn ) |f|
        f(self);
    
    for( self.children ) |child|
        child.updateLayout();

    self.size.y = @max(self.size.y, self.min_size.y);
    self.size.x = @max(self.size.x, self.min_size.x);
}

do any of you would have an idea of what's going on? thanks

#

rewriting the loop as

    for( 0..self.children.len ) |childidx| {
        var child = self.children[childidx];
        child.updateLayout();
    }
``` fixes the issue