| ... | @@ -6800,8 +6800,51 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air | ... | @@ -6800,8 +6800,51 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 6800 | const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; | 6800 | const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 6801 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; | 6801 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; |
| 6802 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; | 6802 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| | 6803 | const target = sema.mod.getTarget(); |
| 6803 | | 6804 | |
| 6804 | const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs); | 6805 | const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs); |
| | 6806 | switch (dest_ty.zigTypeTag()) { |
| | 6807 | .AnyFrame, |
| | 6808 | .ComptimeFloat, |
| | 6809 | .ComptimeInt, |
| | 6810 | .Enum, |
| | 6811 | .EnumLiteral, |
| | 6812 | .ErrorSet, |
| | 6813 | .ErrorUnion, |
| | 6814 | .Fn, |
| | 6815 | .Frame, |
| | 6816 | .NoReturn, |
| | 6817 | .Null, |
| | 6818 | .Opaque, |
| | 6819 | .Optional, |
| | 6820 | .Type, |
| | 6821 | .Undefined, |
| | 6822 | .Void, |
| | 6823 | => return sema.fail(block, dest_ty_src, "invalid type '{}' for @bitCast", .{dest_ty.fmt(target)}), |
| | 6824 | |
| | 6825 | .Pointer => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', use @ptrCast to cast to a pointer", .{ |
| | 6826 | dest_ty.fmt(target), |
| | 6827 | }), |
| | 6828 | .Struct, .Union => if (dest_ty.containerLayout() == .Auto) { |
| | 6829 | const container = switch (dest_ty.zigTypeTag()) { |
| | 6830 | .Struct => "struct", |
| | 6831 | .Union => "union", |
| | 6832 | else => unreachable, |
| | 6833 | }; |
| | 6834 | return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', {s} does not have a guaranteed in-memory layout", .{ |
| | 6835 | dest_ty.fmt(target), container, |
| | 6836 | }); |
| | 6837 | }, |
| | 6838 | .BoundFn => @panic("TODO remove this type from the language and compiler"), |
| | 6839 | |
| | 6840 | .Array, |
| | 6841 | .Bool, |
| | 6842 | .Float, |
| | 6843 | .Int, |
| | 6844 | .Vector, |
| | 6845 | => {}, |
| | 6846 | } |
| | 6847 | |
| 6805 | const operand = sema.resolveInst(extra.rhs); | 6848 | const operand = sema.resolveInst(extra.rhs); |
| 6806 | return sema.bitCast(block, dest_ty, operand, operand_src); | 6849 | return sema.bitCast(block, dest_ty, operand, operand_src); |
| 6807 | } | 6850 | } |
| ... | @@ -19137,7 +19180,19 @@ fn bitCast( | ... | @@ -19137,7 +19180,19 @@ fn bitCast( |
| 19137 | const old_ty = try sema.resolveTypeFields(block, inst_src, sema.typeOf(inst)); | 19180 | const old_ty = try sema.resolveTypeFields(block, inst_src, sema.typeOf(inst)); |
| 19138 | try sema.resolveTypeLayout(block, inst_src, old_ty); | 19181 | try sema.resolveTypeLayout(block, inst_src, old_ty); |
| 19139 | | 19182 | |
| 19140 | // TODO validate the type size and other compile errors | 19183 | const target = sema.mod.getTarget(); |
| | 19184 | var dest_bits = dest_ty.bitSize(target); |
| | 19185 | var old_bits = old_ty.bitSize(target); |
| | 19186 | |
| | 19187 | if (old_bits != dest_bits) { |
| | 19188 | return sema.fail(block, inst_src, "@bitCast size mismatch: destination type '{}' has {d} bits but source type '{}' has {d} bits", .{ |
| | 19189 | dest_ty.fmt(target), |
| | 19190 | dest_bits, |
| | 19191 | old_ty.fmt(target), |
| | 19192 | old_bits, |
| | 19193 | }); |
| | 19194 | } |
| | 19195 | |
| 19141 | if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| { | 19196 | if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| { |
| 19142 | const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0); | 19197 | const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0); |
| 19143 | return sema.addConstant(dest_ty, result_val); | 19198 | return sema.addConstant(dest_ty, result_val); |