2018-03-13 00:56:25 +08:00
|
|
|
const CountBy = struct {
|
|
|
|
a: usize,
|
2018-05-01 08:35:54 +08:00
|
|
|
|
2018-05-29 08:23:55 +08:00
|
|
|
const One = CountBy{ .a = 1 };
|
2018-05-01 08:35:54 +08:00
|
|
|
|
2018-05-31 22:56:59 +08:00
|
|
|
pub fn counter(self: *const CountBy) Counter {
|
2018-05-29 08:23:55 +08:00
|
|
|
return Counter{ .i = 0 };
|
2018-03-13 00:56:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const Counter = struct {
|
|
|
|
i: usize,
|
2018-05-01 08:35:54 +08:00
|
|
|
|
2018-05-31 22:56:59 +08:00
|
|
|
pub fn count(self: *Counter) bool {
|
2018-03-13 00:56:25 +08:00
|
|
|
self.i += 1;
|
|
|
|
return self.i <= 10;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-31 22:56:59 +08:00
|
|
|
fn constCount(comptime cb: *const CountBy, comptime unused: u32) void {
|
2018-03-13 00:56:25 +08:00
|
|
|
comptime {
|
|
|
|
var cnt = cb.counter();
|
2018-05-01 08:35:54 +08:00
|
|
|
if (cnt.i != 0) @compileError("Counter instance reused!");
|
|
|
|
while (cnt.count()) {}
|
2018-03-13 00:56:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "comptime struct return should not return the same instance" {
|
|
|
|
//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);
|
|
|
|
}
|