authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-15 02:02:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-15 14:17:25-04:00
logcb0e22db4e18dd803e1edd681c836fadbae0ea6f
tree0377d1540686d1e34998a171abb3a7f7ce53ca5e
parentfeab1ebe1bfea3955320a6acba69f0c6c79c0730

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

3 files changed, 12 insertions(+), 1 deletions(-)

src/codegen/llvm.zig+1-1
......@@ -4077,7 +4077,7 @@ pub const DeclGen = struct {
40774077 }
40784078
40794079 const is_fn_body = decl.ty.zigTypeTag() == .Fn;
4080 if (!is_fn_body and !decl.ty.hasRuntimeBitsIgnoreComptime()) {
4080 if (!is_fn_body and !decl.ty.hasRuntimeBits()) {
40814081 return self.lowerPtrToVoid(tv.ty);
40824082 }
40834083
test/behavior.zig+1
......@@ -83,6 +83,7 @@ test {
8383 _ = @import("behavior/bugs/11213.zig");
8484 _ = @import("behavior/bugs/11816.zig");
8585 _ = @import("behavior/bugs/12003.zig");
86 _ = @import("behavior/bugs/12025.zig");
8687 _ = @import("behavior/bugs/12033.zig");
8788 _ = @import("behavior/bugs/12430.zig");
8889 _ = @import("behavior/bugs/12486.zig");
test/behavior/bugs/12025.zig created+10
......@@ -0,0 +1,10 @@
1test {
2 comptime var st = .{
3 .foo = &1,
4 .bar = &2,
5 };
6
7 inline for (@typeInfo(@TypeOf(st)).Struct.fields) |field| {
8 _ = field;
9 }
10}