srctree

John Schmidt parent 3e79c0f1 7a045ede
Check for inactive union field when calling fn at comptime

Reuse unionFieldPtr here to ensure that all the safety checks areincluded.

Closes https://github.com/ziglang/zig/issues/18546.

inlinesplit
src/Sema.zig added: 21, removed: 5, total 16
@@ -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);
 
filename was Deleted added: 21, removed: 5, total 16
@@ -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