authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-13 01:58:01-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-13 01:58:01-05:00
logd4bd1b1a602355e9cab179289636f8236f66b6f4
tree07af471934307431b6da26d051bc2c94d501ff68
parent3c2a43fdcc2d9aeafafe7ef37c7b805e18fac351
parent28413ffcbad7817c57fd62d5b9908eabdd43147d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14618 from Vexu/fixes

small misc fixes

7 files changed, 71 insertions(+), 29 deletions(-)

lib/std/zig/render.zig-8
......@@ -2807,14 +2807,6 @@ fn nodeIsBlock(tag: Ast.Node.Tag) bool {
28072807 .block_semicolon,
28082808 .block_two,
28092809 .block_two_semicolon,
2810 .struct_init_dot,
2811 .struct_init_dot_comma,
2812 .struct_init_dot_two,
2813 .struct_init_dot_two_comma,
2814 .array_init_dot,
2815 .array_init_dot_comma,
2816 .array_init_dot_two,
2817 .array_init_dot_two_comma,
28182810 => true,
28192811 else => false,
28202812 };
src/AstGen.zig+2-1
......@@ -1730,7 +1730,8 @@ fn structInitExprRlNone(
17301730 .container_type = ty_inst,
17311731 .name_start = str_index,
17321732 }) } }
1733 else .{ .rl = .none };
1733 else
1734 .{ .rl = .none };
17341735 setExtra(astgen, extra_index, Zir.Inst.StructInitAnon.Item{
17351736 .field_name = str_index,
17361737 .init = try expr(gz, scope, sub_ri, field_init),
src/Sema.zig+37-13
......@@ -7669,17 +7669,21 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
76697669 error_set.fmt(sema.mod),
76707670 });
76717671 }
7672 if (payload.zigTypeTag() == .Opaque) {
7673 return sema.fail(block, rhs_src, "error union with payload of opaque type '{}' not allowed", .{
7674 payload.fmt(sema.mod),
7672 try sema.validateErrorUnionPayloadType(block, payload, rhs_src);
7673 const err_union_ty = try Type.errorUnion(sema.arena, error_set, payload, sema.mod);
7674 return sema.addType(err_union_ty);
7675}
7676
7677fn validateErrorUnionPayloadType(sema: *Sema, block: *Block, payload_ty: Type, payload_src: LazySrcLoc) !void {
7678 if (payload_ty.zigTypeTag() == .Opaque) {
7679 return sema.fail(block, payload_src, "error union with payload of opaque type '{}' not allowed", .{
7680 payload_ty.fmt(sema.mod),
76757681 });
7676 } else if (payload.zigTypeTag() == .ErrorSet) {
7677 return sema.fail(block, rhs_src, "error union with payload of error set type '{}' not allowed", .{
7678 payload.fmt(sema.mod),
7682 } else if (payload_ty.zigTypeTag() == .ErrorSet) {
7683 return sema.fail(block, payload_src, "error union with payload of error set type '{}' not allowed", .{
7684 payload_ty.fmt(sema.mod),
76797685 });
76807686 }
7681 const err_union_ty = try Type.errorUnion(sema.arena, error_set, payload, sema.mod);
7682 return sema.addType(err_union_ty);
76837687}
76847688
76857689fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -8639,6 +8643,7 @@ fn funcCommon(
86398643 const return_type = if (!inferred_error_set or ret_poison)
86408644 bare_return_type
86418645 else blk: {
8646 try sema.validateErrorUnionPayloadType(block, bare_return_type, ret_ty_src);
86428647 const node = try sema.gpa.create(Module.Fn.InferredErrorSetListNode);
86438648 node.data = .{ .func = new_func };
86448649 maybe_inferred_error_set_node = node;
......@@ -8650,15 +8655,15 @@ fn funcCommon(
86508655 });
86518656 };
86528657
8653 if (!bare_return_type.isValidReturnType()) {
8654 const opaque_str = if (bare_return_type.zigTypeTag() == .Opaque) "opaque " else "";
8658 if (!return_type.isValidReturnType()) {
8659 const opaque_str = if (return_type.zigTypeTag() == .Opaque) "opaque " else "";
86558660 const msg = msg: {
86568661 const msg = try sema.errMsg(block, ret_ty_src, "{s}return type '{}' not allowed", .{
8657 opaque_str, bare_return_type.fmt(sema.mod),
8662 opaque_str, return_type.fmt(sema.mod),
86588663 });
86598664 errdefer msg.destroy(sema.gpa);
86608665
8661 try sema.addDeclaredHereNote(msg, bare_return_type);
8666 try sema.addDeclaredHereNote(msg, return_type);
86628667 break :msg msg;
86638668 };
86648669 return sema.failWithOwnedErrorMsg(msg);
......@@ -21930,7 +21935,7 @@ fn zirCUndef(
2193021935 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
2193121936
2193221937 const name = try sema.resolveConstString(block, src, extra.operand, "name of macro being undefined must be comptime-known");
21933 try block.c_import_buf.?.writer().print("#undefine {s}\n", .{name});
21938 try block.c_import_buf.?.writer().print("#undef {s}\n", .{name});
2193421939 return Air.Inst.Ref.void_value;
2193521940}
2193621941
......@@ -29756,6 +29761,25 @@ fn resolvePeerTypes(
2975629761 continue;
2975729762 }
2975829763 },
29764 .ErrorSet => {
29765 chosen = candidate;
29766 chosen_i = candidate_i + 1;
29767 if (err_set_ty) |chosen_set_ty| {
29768 if (.ok == try sema.coerceInMemoryAllowedErrorSets(block, chosen_set_ty, chosen_ty, src, src)) {
29769 continue;
29770 }
29771 if (.ok == try sema.coerceInMemoryAllowedErrorSets(block, chosen_ty, chosen_set_ty, src, src)) {
29772 err_set_ty = chosen_ty;
29773 continue;
29774 }
29775
29776 err_set_ty = try chosen_set_ty.errorSetMerge(sema.arena, chosen_ty);
29777 continue;
29778 } else {
29779 err_set_ty = chosen_ty;
29780 continue;
29781 }
29782 },
2975929783 else => {},
2976029784 }
2976129785
src/codegen/llvm.zig+7-1
......@@ -2273,7 +2273,9 @@ pub const Object = struct {
22732273
22742274 const full_di_fields: [2]*llvm.DIType =
22752275 if (layout.tag_align >= layout.payload_align)
2276 .{ tag_di, payload_di } else .{ payload_di, tag_di };
2276 .{ tag_di, payload_di }
2277 else
2278 .{ payload_di, tag_di };
22772279
22782280 const full_di_ty = dib.createStructType(
22792281 compile_unit_scope,
......@@ -4162,6 +4164,10 @@ pub const DeclGen = struct {
41624164 if (func.data.owner_decl != decl_index) {
41634165 return self.lowerDeclRefValue(tv, func.data.owner_decl);
41644166 }
4167 } else if (decl.val.castTag(.extern_fn)) |func| {
4168 if (func.data.owner_decl != decl_index) {
4169 return self.lowerDeclRefValue(tv, func.data.owner_decl);
4170 }
41654171 }
41664172
41674173 const is_fn_body = decl.ty.zigTypeTag() == .Fn;
test/behavior/cast.zig+12
......@@ -1541,3 +1541,15 @@ test "single item pointer to pointer to array to slice" {
15411541 const z1 = @as([]const i32, @as(*[1]i32, &x));
15421542 try expect(z1[0] == 1234);
15431543}
1544
1545test "peer type resolution forms error union" {
1546 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1547
1548 var foo: i32 = 123;
1549 const result = if (foo < 0) switch (-foo) {
1550 0 => unreachable,
1551 42 => error.AccessDenied,
1552 else => unreachable,
1553 } else @intCast(u32, foo);
1554 try expect(try result == 123);
1555}
test/cases/compile_errors/function_returning_opaque_type.zig+6-6
......@@ -1,11 +1,11 @@
11const FooType = opaque {};
2export fn bar() !FooType {
2export fn bar() FooType {
33 return error.InvalidValue;
44}
5export fn bav() !@TypeOf(null) {
5export fn bav() @TypeOf(null) {
66 return error.InvalidValue;
77}
8export fn baz() !@TypeOf(undefined) {
8export fn baz() @TypeOf(undefined) {
99 return error.InvalidValue;
1010}
1111
......@@ -13,7 +13,7 @@ export fn baz() !@TypeOf(undefined) {
1313// backend=stage2
1414// target=native
1515//
16// :2:18: error: opaque return type 'tmp.FooType' not allowed
16// :2:17: error: opaque return type 'tmp.FooType' not allowed
1717// :1:17: note: opaque declared here
18// :5:18: error: return type '@TypeOf(null)' not allowed
19// :8:18: error: return type '@TypeOf(undefined)' not allowed
18// :5:17: error: return type '@TypeOf(null)' not allowed
19// :8:17: error: return type '@TypeOf(undefined)' not allowed
test/cases/compile_errors/invalid_error_union_payload_type.zig+7
......@@ -4,6 +4,12 @@ comptime {
44comptime {
55 _ = anyerror!anyerror;
66}
7fn someFunction() !anyerror {
8 return error.C;
9}
10comptime {
11 _ = someFunction;
12}
713
814// error
915// backend=stage2
......@@ -11,3 +17,4 @@ comptime {
1117//
1218// :2:18: error: error union with payload of opaque type 'anyopaque' not allowed
1319// :5:18: error: error union with payload of error set type 'anyerror' not allowed
20// :7:20: error: error union with payload of error set type 'anyerror' not allowed