Commit e5ce87f1b1

Jakub Konka <kubkon@jakubkonka.com>
2022-02-08 20:33:45
stage2: handle decl ref to void types
Fixes behavior test 1914
1 parent 4d1e5ef
Changed files (2)
src
test
behavior
src/codegen.zig
@@ -487,19 +487,14 @@ fn lowerDeclRef(
         return Result{ .appended = {} };
     }
 
+    const target = bin_file.options.target;
+    const ptr_width = target.cpu.arch.ptrBitWidth();
     const is_fn_body = decl.ty.zigTypeTag() == .Fn;
     if (!is_fn_body and !decl.ty.hasRuntimeBits()) {
-        return Result{
-            .fail = try ErrorMsg.create(
-                bin_file.allocator,
-                src_loc,
-                "TODO handle void types when lowering decl ref",
-                .{},
-            ),
-        };
+        try code.writer().writeByteNTimes(0xaa, @divExact(ptr_width, 8));
+        return Result{ .appended = {} };
     }
 
-    if (decl.analysis != .complete) return error.AnalysisFail;
     decl.markAlive();
     const vaddr = vaddr: {
         if (bin_file.cast(link.File.MachO)) |macho_file| {
@@ -510,8 +505,8 @@ fn lowerDeclRef(
         break :vaddr bin_file.getDeclVAddr(decl);
     };
 
-    const endian = bin_file.options.target.cpu.arch.endian();
-    switch (bin_file.options.target.cpu.arch.ptrBitWidth()) {
+    const endian = target.cpu.arch.endian();
+    switch (ptr_width) {
         16 => mem.writeInt(u16, try code.addManyAsArray(2), @intCast(u16, vaddr), endian),
         32 => mem.writeInt(u32, try code.addManyAsArray(4), @intCast(u32, vaddr), endian),
         64 => mem.writeInt(u64, try code.addManyAsArray(8), vaddr, endian),
test/behavior/bugs/1914.zig
@@ -13,8 +13,6 @@ const a = A{ .b_list_pointer = &b_list };
 
 test "segfault bug" {
     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
-    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
     const assert = std.debug.assert;
     const obj = B{ .a_pointer = &a };
     assert(obj.a_pointer == &a); // this makes zig crash
@@ -31,8 +29,6 @@ pub const B2 = struct {
 var b_value = B2{ .pointer_array = &[_]*A2{} };
 
 test "basic stuff" {
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
-    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
     std.debug.assert(&b_value == &b_value);
 }