fixes
This commit is contained in:
36
examples/issue-0013.sx
Normal file
36
examples/issue-0013.sx
Normal file
@@ -0,0 +1,36 @@
|
||||
// issue-0013: += on global variables reads initial value instead of current value
|
||||
//
|
||||
// `g_counter += 1` compiles as `store(initial_value + 1)` instead of
|
||||
// `store(load(g_counter) + 1)`. So it always produces the same result.
|
||||
//
|
||||
// Workaround: use `g_counter = g_counter + 1` instead of `g_counter += 1`
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
g_counter : s64 = 0;
|
||||
|
||||
tick :: () {
|
||||
g_counter += 1;
|
||||
print("counter={}\n", g_counter);
|
||||
}
|
||||
|
||||
main :: () -> void {
|
||||
// Test 1: += always produces 1 (BUG)
|
||||
out("--- Test 1: += (broken) ---\n");
|
||||
out("Expected: 1, 2, 3\n");
|
||||
i : s64 = 0;
|
||||
while i < 3 {
|
||||
tick();
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Test 2: manual read-modify-write works correctly
|
||||
out("--- Test 2: = x + 1 (works) ---\n");
|
||||
out("Expected: 2, 3, 4\n");
|
||||
g_counter = g_counter + 1;
|
||||
print("counter={}\n", g_counter);
|
||||
g_counter = g_counter + 1;
|
||||
print("counter={}\n", g_counter);
|
||||
g_counter = g_counter + 1;
|
||||
print("counter={}\n", g_counter);
|
||||
}
|
||||
Reference in New Issue
Block a user