| author | |
| committer | |
| log | 4a7f1339ac08bd61ddb6a25127a0ae34dc55c26d |
| tree | b5f082ffcc69a89b5cd228b6b0b8aca5c4d8d5bd |
| parent | 1fcc9cbe3d040ce102739760cc14476ec6726bbe |
| parent | 04742ad6f58ae5860efe9b4846be2caafca079e7 |
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37043
Reviewed-by: Andrew Kelley <andrew@ziglang.org>4 files changed, 38 insertions(+), 2 deletions(-)
src/InternPool.zig+1-1| ... | ... | @@ -3851,7 +3851,7 @@ pub fn loadEnumType(ip: *const InternPool, index: Index) LoadedEnumType { |
| 3851 | 3851 | .generated_union_tag => info: { |
| 3852 | 3852 | const owner_union: Index = @fromBackingInt(@intCast(extra_items[extra_index])); |
| 3853 | 3853 | extra_index += 1; |
| 3854 | break :info .{ .none, .empty, owner_union }; | |
| 3854 | break :info .{ .none, ip.loadUnionType(owner_union).captures, owner_union }; | |
| 3855 | 3855 | }, |
| 3856 | 3856 | _ => |n| info: { |
| 3857 | 3857 | const zir_index: TrackedInst.Index = @fromBackingInt(@intCast(extra_items[extra_index])); |
src/Sema/type_resolution.zig+1-1| ... | ... | @@ -1478,7 +1478,7 @@ pub fn resolveEnumLayout(sema: *Sema, enum_ty: Type) CompileError!void { |
| 1478 | 1478 | }; |
| 1479 | 1479 | const uncoerced = try sema.resolveInlineBody(&block, value_body, zir_index); |
| 1480 | 1480 | const coerced = try sema.coerce(&block, int_tag_ty, uncoerced, field_val_src); |
| 1481 | const val = try sema.resolveConstValue(&block, field_val_src, coerced, null); | |
| 1481 | const val = try sema.resolveConstDefinedValue(&block, field_val_src, coerced, null); | |
| 1482 | 1482 | enum_obj.field_values.get(ip)[zir_field.idx] = val.toIntern(); |
| 1483 | 1483 | } |
| 1484 | 1484 | } |
test/behavior/union.zig+17| ... | ... | @@ -2174,6 +2174,23 @@ test "matching captures causes union equivalence" { |
| 2174 | 2174 | try expect(a.u == b.u); |
| 2175 | 2175 | } |
| 2176 | 2176 | |
| 2177 | test "capture used as explicit field value" { | |
| 2178 | const S = struct { | |
| 2179 | fn Union(n: u8) type { | |
| 2180 | return union(enum(u8)) { | |
| 2181 | a: u32 = n, | |
| 2182 | b, | |
| 2183 | }; | |
| 2184 | } | |
| 2185 | }; | |
| 2186 | ||
| 2187 | comptime assert(@typeInfo(S.Union(0)).@"union".tag_type.? != @typeInfo(S.Union(1)).@"union".tag_type.?); | |
| 2188 | ||
| 2189 | const s: S.Union(1) = .{ .a = 0 }; | |
| 2190 | try expect(@backingInt(s) == 1); | |
| 2191 | try expect(@backingInt(@as(@typeInfo(S.Union(1)).@"union".tag_type.?, s)) == 1); | |
| 2192 | } | |
| 2193 | ||
| 2177 | 2194 | test "signed enum tag with negative value" { |
| 2178 | 2195 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 2179 | 2196 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
test/cases/compile_errors/undefined_enum_value.zig created+19| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | const E = enum(u8) { | |
| 2 | a = undefined, | |
| 3 | b, | |
| 4 | }; | |
| 5 | const U = union(enum(u8)) { | |
| 6 | a: u32 = undefined, | |
| 7 | b, | |
| 8 | }; | |
| 9 | comptime { | |
| 10 | _ = E.a; | |
| 11 | } | |
| 12 | comptime { | |
| 13 | _ = U.a; | |
| 14 | } | |
| 15 | ||
| 16 | // error | |
| 17 | // | |
| 18 | // :2:9: error: use of undefined value here causes illegal behavior | |
| 19 | // :6:14: error: use of undefined value here causes illegal behavior |