aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/bugs/828.zig
blob: 220d98ce090e5322126ece3a93971b47511f55f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const builtin = @import("builtin");

const CountBy = struct {
    a: usize,

    const One = CountBy{ .a = 1 };

    pub fn counter(self: *const CountBy) Counter {
        _ = self;
        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 {
    _ = unused;
    comptime {
        var cnt = cb.counter();
        if (cnt.i != 0) @compileError("Counter instance reused!");
        while (cnt.count()) {}
    }
}

test "comptime struct return should not return the same instance" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;

    //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);
    if (false) {
        ValA;
        ValB;
    }
}