From cb0e22db4e18dd803e1edd681c836fadbae0ea6f Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sat, 15 Oct 2022 02:02:21 -0400 Subject: [PATCH] llvm: fix lowering of runtime refs to comptime-only decls When we want a runtime pointer to a zero-bit value we use an undef pointer, but what if we want a runtime pointer to a comptime-only value? Normally, if `T` is a comptime-only type such as `*const comptime_int`, then `*const T` would also be a comptime-only type, so anything referencing a comptime-only value is usually also comptime-only, and therefore not emitted to the executable. However, what if instead we have a `*const anyopaque` pointing to a comptime-only value? Certainly, `*const anyopaque` is a runtime type, and so we need some runtime value to store, even when it happens to be pointing to a comptime-only value. In this case we want to do the same thing as we do when pointing to a zero-bit value, so we use `hasRuntimeBits` to handle both cases instead of ignoring comptime. Closes #12025 --- src/codegen/llvm.zig | 2 +- test/behavior.zig | 1 + test/behavior/bugs/12025.zig | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/behavior/bugs/12025.zig diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 4a0978af5b..3a02cd630b 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -4077,7 +4077,7 @@ pub const DeclGen = struct { } const is_fn_body = decl.ty.zigTypeTag() == .Fn; - if (!is_fn_body and !decl.ty.hasRuntimeBitsIgnoreComptime()) { + if (!is_fn_body and !decl.ty.hasRuntimeBits()) { return self.lowerPtrToVoid(tv.ty); } diff --git a/test/behavior.zig b/test/behavior.zig index 45b607692b..7849c65582 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -83,6 +83,7 @@ test { _ = @import("behavior/bugs/11213.zig"); _ = @import("behavior/bugs/11816.zig"); _ = @import("behavior/bugs/12003.zig"); + _ = @import("behavior/bugs/12025.zig"); _ = @import("behavior/bugs/12033.zig"); _ = @import("behavior/bugs/12430.zig"); _ = @import("behavior/bugs/12486.zig"); diff --git a/test/behavior/bugs/12025.zig b/test/behavior/bugs/12025.zig new file mode 100644 index 0000000000..7b1804cf20 --- /dev/null +++ b/test/behavior/bugs/12025.zig @@ -0,0 +1,10 @@ +test { + comptime var st = .{ + .foo = &1, + .bar = &2, + }; + + inline for (@typeInfo(@TypeOf(st)).Struct.fields) |field| { + _ = field; + } +}