aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/basic.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-10-26 22:29:43 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-10-26 22:41:19 -0700
commit3af973160031fd573f46489bee519217e635839a (patch)
tree0e48a3002f16340e95186cdf53d595e60f5051b7 /test/behavior/basic.zig
parentc1fd459f14d9e89c0b38b1afc531a7b67e3a2759 (diff)
downloadzig-3af973160031fd573f46489bee519217e635839a.tar.gz
zig-3af973160031fd573f46489bee519217e635839a.zip
stage2: implement runtime pointer access to global constants
The main problem that motivated these changes is that global constants which are referenced by pointer would not be emitted into the binary. This happened because `semaDecl` did not add `codegen_decl` tasks for global constants, instead relying on the constant values being copied as necessary. However when the global constants are referenced by pointer, they need to be sent to the linker to be emitted. After making global const arrays, structs, and unions get emitted, this uncovered a latent issue: the anonymous decls that they referenced would get garbage collected (via `deleteUnusedDecl`) even though they would later be referenced by the global const. In order to solve this problem, I introduced `anon_work_queue` which is the same as `work_queue` except a lower priority. The `codegen_decl` task for anon decls goes into the `anon_work_queue` ensuring that the owner decl gets a chance to mark its anon decls as alive before they are possibly deleted. This caused a few regressions, which I made the judgement call to add workarounds for. Two steps forward, one step back, is still progress. The regressions were: * Two behavior tests having to do with unions. These tests were intentionally exercising the LLVM constant value lowering, however, due to the bug with garbage collection that was fixed in this commit, the LLVM code was not getting exercised, and union types/values were not implemented correctly, due to me forgetting that LLVM does not allow bitcasting aggregate values. - This is worked around by allowing those 2 test cases to regress, moving them to the "passing for stage1 only" section. * The test-stage2 test cases (in test/cases/*) for non-LLVM backends previously did not have any calls to lower struct values, but now they do. The code that was there was just `@panic("TODO")`. I replaced that code with a stub that generates the wrong value. This is an intentional miscompilation that will obviously need to get fixed before any struct behavior tests pass. None of the current tests we have exercise loading any values from these global const structs, so there is not a problem until we try to improve these backends.
Diffstat (limited to 'test/behavior/basic.zig')
-rw-r--r--test/behavior/basic.zig15
1 files changed, 15 insertions, 0 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index f3d511916f..08d29fdb30 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -459,3 +459,18 @@ var gdt = [_]GDTEntry{
GDTEntry{ .field = 2 },
};
var global_ptr = &gdt[0];
+
+test "global constant is loaded with a runtime-known index" {
+ const S = struct {
+ fn doTheTest() !void {
+ var index: usize = 1;
+ const ptr = &pieces[index].field;
+ try expect(ptr.* == 2);
+ }
+ const Piece = struct {
+ field: i32,
+ };
+ const pieces = [_]Piece{ Piece{ .field = 1 }, Piece{ .field = 2 }, Piece{ .field = 3 } };
+ };
+ try S.doTheTest();
+}