| ... | ... | @@ -1820,8 +1820,25 @@ pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Ins |
| 1820 | 1820 | return ty; |
| 1821 | 1821 | } |
| 1822 | 1822 | |
| 1823 | | fn resolveCastDestType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, builtin_name: []const u8) !Type { |
| 1824 | | return sema.resolveType(block, src, zir_ref) catch |err| switch (err) { |
| 1823 | fn resolveCastDestType( |
| 1824 | sema: *Sema, |
| 1825 | block: *Block, |
| 1826 | src: LazySrcLoc, |
| 1827 | zir_ref: Zir.Inst.Ref, |
| 1828 | strat: enum { remove_eu_opt, remove_eu, remove_opt }, |
| 1829 | builtin_name: []const u8, |
| 1830 | ) !Type { |
| 1831 | const mod = sema.mod; |
| 1832 | const remove_eu = switch (strat) { |
| 1833 | .remove_eu_opt, .remove_eu => true, |
| 1834 | .remove_opt => false, |
| 1835 | }; |
| 1836 | const remove_opt = switch (strat) { |
| 1837 | .remove_eu_opt, .remove_opt => true, |
| 1838 | .remove_eu => false, |
| 1839 | }; |
| 1840 | |
| 1841 | const raw_ty = sema.resolveType(block, src, zir_ref) catch |err| switch (err) { |
| 1825 | 1842 | error.GenericPoison => { |
| 1826 | 1843 | // Cast builtins use their result type as the destination type, but |
| 1827 | 1844 | // it could be an anytype argument, which we can't catch in AstGen. |
| ... | ... | @@ -1836,6 +1853,18 @@ fn resolveCastDestType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir |
| 1836 | 1853 | }, |
| 1837 | 1854 | else => |e| return e, |
| 1838 | 1855 | }; |
| 1856 | |
| 1857 | if (remove_eu and raw_ty.zigTypeTag(mod) == .ErrorUnion) { |
| 1858 | const eu_child = raw_ty.errorUnionPayload(mod); |
| 1859 | if (remove_opt and eu_child.zigTypeTag(mod) == .Optional) { |
| 1860 | return eu_child.childType(mod); |
| 1861 | } |
| 1862 | return eu_child; |
| 1863 | } |
| 1864 | if (remove_opt and raw_ty.zigTypeTag(mod) == .Optional) { |
| 1865 | return raw_ty.childType(mod); |
| 1866 | } |
| 1867 | return raw_ty; |
| 1839 | 1868 | } |
| 1840 | 1869 | |
| 1841 | 1870 | fn analyzeAsType( |
| ... | ... | @@ -8304,7 +8333,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8304 | 8333 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 8305 | 8334 | const src = inst_data.src(); |
| 8306 | 8335 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 8307 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@enumFromInt"); |
| 8336 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@enumFromInt"); |
| 8308 | 8337 | const operand = try sema.resolveInst(extra.rhs); |
| 8309 | 8338 | |
| 8310 | 8339 | if (dest_ty.zigTypeTag(mod) != .Enum) { |
| ... | ... | @@ -9600,7 +9629,7 @@ fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 9600 | 9629 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 9601 | 9630 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 9602 | 9631 | |
| 9603 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@intCast"); |
| 9632 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@intCast"); |
| 9604 | 9633 | const operand = try sema.resolveInst(extra.rhs); |
| 9605 | 9634 | |
| 9606 | 9635 | return sema.intCast(block, inst_data.src(), dest_ty, src, operand, operand_src, true); |
| ... | ... | @@ -9761,7 +9790,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 9761 | 9790 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 9762 | 9791 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 9763 | 9792 | |
| 9764 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@bitCast"); |
| 9793 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@bitCast"); |
| 9765 | 9794 | const operand = try sema.resolveInst(extra.rhs); |
| 9766 | 9795 | const operand_ty = sema.typeOf(operand); |
| 9767 | 9796 | switch (dest_ty.zigTypeTag(mod)) { |
| ... | ... | @@ -9904,7 +9933,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 9904 | 9933 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 9905 | 9934 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 9906 | 9935 | |
| 9907 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@floatCast"); |
| 9936 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@floatCast"); |
| 9908 | 9937 | const operand = try sema.resolveInst(extra.rhs); |
| 9909 | 9938 | |
| 9910 | 9939 | const target = mod.getTarget(); |
| ... | ... | @@ -20706,7 +20735,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 20706 | 20735 | const src = inst_data.src(); |
| 20707 | 20736 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 20708 | 20737 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 20709 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@intFromFloat"); |
| 20738 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@intFromFloat"); |
| 20710 | 20739 | const operand = try sema.resolveInst(extra.rhs); |
| 20711 | 20740 | const operand_ty = sema.typeOf(operand); |
| 20712 | 20741 | |
| ... | ... | @@ -20746,7 +20775,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 20746 | 20775 | const src = inst_data.src(); |
| 20747 | 20776 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 20748 | 20777 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 20749 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@floatFromInt"); |
| 20778 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@floatFromInt"); |
| 20750 | 20779 | const operand = try sema.resolveInst(extra.rhs); |
| 20751 | 20780 | const operand_ty = sema.typeOf(operand); |
| 20752 | 20781 | |
| ... | ... | @@ -20775,7 +20804,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 20775 | 20804 | const operand_res = try sema.resolveInst(extra.rhs); |
| 20776 | 20805 | const operand_coerced = try sema.coerce(block, Type.usize, operand_res, operand_src); |
| 20777 | 20806 | |
| 20778 | | const ptr_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@ptrFromInt"); |
| 20807 | const ptr_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, "@ptrFromInt"); |
| 20779 | 20808 | try sema.checkPtrType(block, src, ptr_ty); |
| 20780 | 20809 | const elem_ty = ptr_ty.elemType2(mod); |
| 20781 | 20810 | const ptr_align = try ptr_ty.ptrAlignmentAdvanced(mod, sema); |
| ... | ... | @@ -20833,7 +20862,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat |
| 20833 | 20862 | const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; |
| 20834 | 20863 | const src = LazySrcLoc.nodeOffset(extra.node); |
| 20835 | 20864 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; |
| 20836 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@errSetCast"); |
| 20865 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@errSetCast"); |
| 20837 | 20866 | const operand = try sema.resolveInst(extra.rhs); |
| 20838 | 20867 | const operand_ty = sema.typeOf(operand); |
| 20839 | 20868 | try sema.checkErrorSetType(block, src, dest_ty); |
| ... | ... | @@ -20915,12 +20944,12 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat |
| 20915 | 20944 | } |
| 20916 | 20945 | |
| 20917 | 20946 | fn zirPtrCastFull(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 20918 | | const flags = @as(Zir.Inst.FullPtrCastFlags, @bitCast(@as(u5, @truncate(extended.small)))); |
| 20947 | const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as(u5, @truncate(extended.small))); |
| 20919 | 20948 | const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; |
| 20920 | 20949 | const src = LazySrcLoc.nodeOffset(extra.node); |
| 20921 | | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; |
| 20950 | const operand_src: LazySrcLoc = .{ .node_offset_ptrcast_operand = extra.node }; |
| 20922 | 20951 | const operand = try sema.resolveInst(extra.rhs); |
| 20923 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@ptrCast"); // TODO: better error message (builtin name) |
| 20952 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, flags.needResultTypeBuiltinName()); |
| 20924 | 20953 | return sema.ptrCastFull( |
| 20925 | 20954 | block, |
| 20926 | 20955 | flags, |
| ... | ... | @@ -20936,7 +20965,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 20936 | 20965 | const src = inst_data.src(); |
| 20937 | 20966 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 20938 | 20967 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 20939 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@ptrCast"); |
| 20968 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu, "@ptrCast"); |
| 20940 | 20969 | const operand = try sema.resolveInst(extra.rhs); |
| 20941 | 20970 | |
| 20942 | 20971 | return sema.ptrCastFull( |
| ... | ... | @@ -21325,7 +21354,7 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst |
| 21325 | 21354 | const flags = @as(Zir.Inst.FullPtrCastFlags, @bitCast(@as(u5, @truncate(extended.small)))); |
| 21326 | 21355 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 21327 | 21356 | const src = LazySrcLoc.nodeOffset(extra.node); |
| 21328 | | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; |
| 21357 | const operand_src: LazySrcLoc = .{ .node_offset_ptrcast_operand = extra.node }; |
| 21329 | 21358 | const operand = try sema.resolveInst(extra.operand); |
| 21330 | 21359 | const operand_ty = sema.typeOf(operand); |
| 21331 | 21360 | try sema.checkPtrOperand(block, operand_src, operand_ty); |
| ... | ... | @@ -21349,7 +21378,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 21349 | 21378 | const src = inst_data.src(); |
| 21350 | 21379 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 21351 | 21380 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 21352 | | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, "@truncate"); |
| 21381 | const dest_ty = try sema.resolveCastDestType(block, src, extra.lhs, .remove_eu_opt, "@truncate"); |
| 21353 | 21382 | const dest_scalar_ty = try sema.checkIntOrVectorAllowComptime(block, dest_ty, src); |
| 21354 | 21383 | const operand = try sema.resolveInst(extra.rhs); |
| 21355 | 21384 | const operand_ty = sema.typeOf(operand); |