diff options
| author | HydroH <iodizon@163.com> | 2024-03-28 18:23:32 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-28 10:23:32 +0000 |
| commit | 7aa42f47b79f289829a1b43a68c8c08e374aa6a2 (patch) | |
| tree | b4562be62e7f23bad2ed9429fa6451871b65ff4a /src/Sema.zig | |
| parent | 17053887d080bd125d2f5ccbb39238f23c706328 (diff) | |
| download | zig-7aa42f47b79f289829a1b43a68c8c08e374aa6a2.tar.gz zig-7aa42f47b79f289829a1b43a68c8c08e374aa6a2.zip | |
allow `@errorcast` to cast error sets to error unions
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index d21fed6910..001d841959 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22626,20 +22626,18 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData const base_operand_ty = sema.typeOf(operand); const dest_tag = base_dest_ty.zigTypeTag(mod); const operand_tag = base_operand_ty.zigTypeTag(mod); - if (dest_tag != operand_tag) { - return sema.fail(block, src, "expected source and destination types to match, found '{s}' and '{s}'", .{ - @tagName(operand_tag), @tagName(dest_tag), - }); - } else if (dest_tag != .ErrorSet and dest_tag != .ErrorUnion) { + + if (dest_tag != .ErrorSet and dest_tag != .ErrorUnion) { return sema.fail(block, src, "expected error set or error union type, found '{s}'", .{@tagName(dest_tag)}); } - const dest_ty, const operand_ty = if (dest_tag == .ErrorUnion) .{ - base_dest_ty.errorUnionSet(mod), - base_operand_ty.errorUnionSet(mod), - } else .{ - base_dest_ty, - base_operand_ty, - }; + if (operand_tag != .ErrorSet and operand_tag != .ErrorUnion) { + return sema.fail(block, src, "expected error set or error union type, found '{s}'", .{@tagName(operand_tag)}); + } + if (dest_tag == .ErrorSet and operand_tag == .ErrorUnion) { + return sema.fail(block, src, "cannot cast an error union type to error set", .{}); + } + const dest_ty = if (dest_tag == .ErrorUnion) base_dest_ty.errorUnionSet(mod) else base_dest_ty; + const operand_ty = if (operand_tag == .ErrorUnion) base_operand_ty.errorUnionSet(mod) else base_operand_ty; // operand must be defined since it can be an invalid error value const maybe_operand_val = try sema.resolveDefinedValue(block, operand_src, operand); @@ -22681,7 +22679,7 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData if (!dest_ty.isAnyError(mod)) check: { const operand_val = mod.intern_pool.indexToKey(val.toIntern()); var error_name: InternPool.NullTerminatedString = undefined; - if (dest_tag == .ErrorUnion) { + if (operand_tag == .ErrorUnion) { if (operand_val.error_union.val != .err_name) break :check; error_name = operand_val.error_union.val.err_name; } else { |
