mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
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.
This commit is contained in:
parent
3e79c0f18c
commit
7a045ede7c
@ -27200,10 +27200,10 @@ fn fieldCallBind(
|
|||||||
.Union => {
|
.Union => {
|
||||||
try sema.resolveTypeFields(concrete_ty);
|
try sema.resolveTypeFields(concrete_ty);
|
||||||
const union_obj = mod.typeToUnion(concrete_ty).?;
|
const union_obj = mod.typeToUnion(concrete_ty).?;
|
||||||
const field_index = union_obj.nameIndex(ip, field_name) orelse break :find_field;
|
_ = union_obj.nameIndex(ip, field_name) orelse break :find_field;
|
||||||
const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
|
|
||||||
|
|
||||||
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 => {
|
.Type => {
|
||||||
const namespace = try sema.analyzeLoad(block, src, object_ptr, src);
|
const namespace = try sema.analyzeLoad(block, src, object_ptr, src);
|
||||||
|
|||||||
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user