commit 7a045ede7ca26338dc553e1a0113d1de4daf1b95 (tree)
parent 3e79c0f18ce05c302c6117e66275db4fcdf033e4
Author: John Schmidt <john.schmidt.h@gmail.com>
Date: Fri, 9 Feb 2024 01:51:26 +0100
Check for inactive union field when calling fn at comptime
Reuse `unionFieldPtr` here to ensure that all the safety checks are
included.
Closes https://github.com/ziglang/zig/issues/18546.
Diffstat:
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/src/Sema.zig b/src/Sema.zig
@@ -27200,10 +27200,10 @@ fn fieldCallBind(
.Union => {
try sema.resolveTypeFields(concrete_ty);
const union_obj = mod.typeToUnion(concrete_ty).?;
- const field_index = union_obj.nameIndex(ip, field_name) orelse break :find_field;
- const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
+ _ = union_obj.nameIndex(ip, field_name) orelse break :find_field;
- return sema.finishFieldCallBind(block, src, ptr_ty, field_ty, field_index, object_ptr);
+ const field_ptr = try unionFieldPtr(sema, block, src, object_ptr, field_name, field_name_src, concrete_ty, false);
+ return .{ .direct = try sema.analyzeLoad(block, src, field_ptr, src) };
},
.Type => {
const namespace = try sema.analyzeLoad(block, src, object_ptr, src);
diff --git a/test/cases/compile_errors/union_calling_inactive_field_as_fn.zig b/test/cases/compile_errors/union_calling_inactive_field_as_fn.zig
@@ -0,0 +1,16 @@
+const U = union(enum) {
+ int: isize,
+ float: f64,
+};
+
+export fn entry() void {
+ const f = U{ .int = 20 };
+ _ = f.float();
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :8:10: error: access of union field 'float' while field 'int' is active
+// :1:11: note: union declared here