authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-03-26 00:57:17+01:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-03-26 01:07:52+01:00
log607300a59bfa24fae19b85029065c4a3cc136692
tree57279be77a66da2c6b0af05d51740169816a64d7
parentdc5dc3ac59bb0f013a867b278b994b76df655480

sema: simplify @bitCast error messages


1 files changed, 30 insertions(+), 50 deletions(-)

src/Sema.zig+30-50
......@@ -6769,66 +6769,46 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
67696769
67706770 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
67716771 switch (dest_ty.zigTypeTag()) {
6772 .Type,
6773 .Void,
6774 .NoReturn,
6772 .AnyFrame,
67756773 .ComptimeFloat,
67766774 .ComptimeInt,
6777 .Undefined,
6778 .Null,
6779 .Optional,
6780 .ErrorUnion,
6781 .ErrorSet,
6782 .Opaque,
6783 .Frame,
6784 .AnyFrame,
6775 .Enum,
67856776 .EnumLiteral,
6786 .Union,
6777 .ErrorSet,
6778 .ErrorUnion,
67876779 .Fn,
6780 .Frame,
6781 .NoReturn,
6782 .Null,
6783 .Opaque,
6784 .Optional,
6785 .Type,
6786 .Undefined,
6787 .Void,
67886788 => return sema.fail(block, dest_ty_src, "invalid type '{}' for @bitCast", .{dest_ty.fmt(target)}),
67896789
6790 .Pointer => {
6791 const msg = msg: {
6792 const msg = try sema.errMsg(block, dest_ty_src, "cannot @bitCast to pointer type '{}'", .{dest_ty.fmt(target)});
6793 errdefer msg.destroy(sema.gpa);
6794
6795 const pointee_ty = dest_ty.ptrInfo().data.pointee_type;
6796 try sema.errNote(block, dest_ty_src, msg, "to cast to a pointer type, use @ptrCast({}, ...)", .{dest_ty.fmt(target)});
6797 try sema.errNote(block, dest_ty_src, msg, "to cast to a non-pointer type, use @bitCast({}, ...)", .{pointee_ty.fmt(target)});
6798 break :msg msg;
6790 .Pointer => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', use @ptrCast to cast to a pointer", .{
6791 dest_ty.fmt(target),
6792 }),
6793 .Struct, .Union => if (dest_ty.containerLayout() == .Auto) {
6794 const container = switch (dest_ty.zigTypeTag()) {
6795 .Struct => "struct",
6796 .Union => "union",
6797 else => unreachable,
67996798 };
6800 return sema.failWithOwnedErrorMsg(block, msg);
6801 },
6802 .Struct => {
6803 if (dest_ty.containerLayout() == .Auto) {
6804 const msg = msg: {
6805 const msg = try sema.errMsg(
6806 block,
6807 dest_ty_src,
6808 "cannot @bitCast to '{}', struct does not have a specified layout",
6809 .{dest_ty.fmt(target)},
6810 );
6811 errdefer msg.destroy(sema.gpa);
6812
6813 const ty_decl_src = dest_ty.declSrcLoc();
6814 try sema.mod.errNoteNonLazy(
6815 ty_decl_src,
6816 msg,
6817 "consider using 'packed struct' or 'extern struct' for a specified layout.",
6818 .{},
6819 );
6820 break :msg msg;
6821 };
6822 return sema.failWithOwnedErrorMsg(block, msg);
6823 }
6799 return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', {s} does not have a guaranteed in-memory layout", .{
6800 dest_ty.fmt(target), container,
6801 });
68246802 },
68256803 .BoundFn => @panic("TODO remove this type from the language and compiler"),
6826 else => {},
6827 }
68286804
6829 // When bitcasting we compare the bit size of the types, so we need to
6830 // fully resolve all composite types to finalize the layout.
6831 try sema.resolveTypeFully(block, dest_ty_src, dest_ty);
6805 .Array,
6806 .Bool,
6807 .Float,
6808 .Int,
6809 .Vector,
6810 => {},
6811 }
68326812
68336813 const operand = sema.resolveInst(extra.rhs);
68346814 return sema.bitCast(block, dest_ty, operand, operand_src);