New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comptime incorrectly reuses struct instances under some circumstances #828

Closed
tgschultz opened this Issue Mar 12, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@tgschultz
Contributor

tgschultz commented Mar 12, 2018

const CountBy = struct {
    a: usize,
    
    const One = CountBy {
        .a = 1,
    };
    
    pub fn counter(self: &const CountBy) Counter {
        return Counter {
            .i = 0,
        };
    }
};

const Counter = struct {
    i: usize,
    
    pub fn count(self: &Counter) bool {
        self.i += 1;
        return self.i <= 10;
    }
};

fn constCount(comptime cb: &const CountBy, comptime unused: u32) void {
    comptime {
        var cnt = cb.counter();
        if(cnt.i != 0) @compileError("Counter instance reused!");
        while(cnt.count()){}
    }
}

test "Counter" {
    //the first parameter must be passed by reference to trigger the bug
    //a second parameter is required to trigger the bug
    const ValA = constCount(&CountBy.One, 12);
    const ValB = constCount(&CountBy.One, 15);
}
test.zig:27:24: error: Counter instance reused!
        if(cnt.i != 0) @compileError("Counter instance reused!");
                       ^
test.zig:36:28: note: called from here
    const ValB = constCount(&CountBy.One, 15);
                           ^

@andrewrk andrewrk added this to the 0.2.0 milestone Mar 12, 2018

@andrewrk andrewrk added the bug label Mar 12, 2018

@andrewrk andrewrk closed this in bcce777 Mar 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment