diff --git a/src/Sema.zig b/src/Sema.zig index 153a2aad72..2b9fa82ce8 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -28498,6 +28498,10 @@ fn unionFieldPtr( if (try sema.resolveDefinedValue(block, src, union_ptr)) |union_ptr_val| ct: { switch (union_obj.flagsUnordered(ip).layout) { .auto => if (initializing) { + if (!sema.isComptimeMutablePtr(union_ptr_val)) { + // The initialization is a runtime operation. + break :ct; + } // Store to the union to initialize the tag. const field_tag = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); const payload_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); diff --git a/test/behavior/union.zig b/test/behavior/union.zig index ef269c2688..364a83814a 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -2303,3 +2303,22 @@ test "extern union @FieldType" { comptime assert(@FieldType(U, "b") == f64); comptime assert(@FieldType(U, "c") == *U); } + +test "assign global tagged union" { + const U = union(enum) { + a: u16, + b: u32, + + var global: @This() = undefined; + }; + + U.global = .{ .a = 123 }; + try expect(U.global == .a); + try expect(U.global != .b); + try expect(U.global.a == 123); + + U.global = .{ .b = 123456 }; + try expect(U.global != .a); + try expect(U.global == .b); + try expect(U.global.b == 123456); +}