Skip to content

Commit

Permalink
Improve some error messages
Browse files Browse the repository at this point in the history
Closes #12547: explain why arrays aren't allowed in packed structs.
Closes #13832: improve error message for a common mistake on the LHS of
the binary `!` operator.
  • Loading branch information
tau-dev committed Jul 16, 2024
1 parent 733aeef commit ae89705
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8666,16 +8666,20 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
const rhs_src = block.src(.{ .node_offset_bin_rhs = inst_data.src_node });
const error_set = try sema.resolveType(block, lhs_src, extra.lhs);
// Do not resolve as type yet, so that we can give a more useful message
// when someone writes `error.XYZ` instead of `error{XYZ}` as the LHS.
const error_set = try sema.resolveConstDefinedValue(block, lhs_src, try sema.resolveInst(extra.lhs), .{
.needed_comptime_reason = "types must be comptime-known",
});
const payload = try sema.resolveType(block, rhs_src, extra.rhs);

if (error_set.zigTypeTag(mod) != .ErrorSet) {
if (error_set.typeOf(mod).ip_index != .type_type or error_set.toType().zigTypeTag(mod) != .ErrorSet) {
return sema.fail(block, lhs_src, "expected error set type, found '{}'", .{
error_set.fmt(pt),
error_set.fmtValue(pt, sema),
});
}
try sema.validateErrorUnionPayloadType(block, payload, rhs_src);
const err_union_ty = try pt.errorUnionType(error_set, payload);
const err_union_ty = try pt.errorUnionType(error_set.toType(), payload);
return Air.internedToRef(err_union_ty.toIntern());
}

Expand Down Expand Up @@ -27071,8 +27075,8 @@ fn explainWhyTypeIsNotPacked(
.ErrorSet,
.AnyFrame,
.Optional,
.Array,
=> try sema.errNote(src_loc, msg, "type has no guaranteed in-memory representation", .{}),
.Array => try sema.errNote(src_loc, msg, "packed fields are ordered according to machine endianness, array elements are not", .{}),
.Pointer => if (ty.isSlice(mod)) {
try sema.errNote(src_loc, msg, "slices have no guaranteed in-memory representation", .{});
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
comptime {
const z = error.Foo!i32;
const x: z = undefined;
_ = x;
}
comptime {
const z = i32!i32;
const x: z = undefined;
Expand All @@ -8,4 +13,5 @@ comptime {
// backend=stage2
// target=native
//
// :2:15: error: expected error set type, found 'i32'
// :2:15: error: expected error set type, found 'error.Foo'
// :3:15: error: expected error set type, found 'i32'
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export fn entry14() void {
// :3:12: error: packed structs cannot contain fields of type 'anyerror'
// :3:12: note: type has no guaranteed in-memory representation
// :8:12: error: packed structs cannot contain fields of type '[2]u24'
// :8:12: note: type has no guaranteed in-memory representation
// :8:12: note: packed fields are ordered according to machine endianness, array elements are not
// :13:20: error: packed structs cannot contain fields of type 'anyerror!u32'
// :13:20: note: type has no guaranteed in-memory representation
// :18:12: error: packed structs cannot contain fields of type 'tmp.S'
Expand Down

0 comments on commit ae89705

Please sign in to comment.