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 {...@@ -2807,14 +2807,6 @@ fn nodeIsBlock(tag: Ast.Node.Tag) bool {
2807 .block_semicolon,2807 .block_semicolon,
2808 .block_two,2808 .block_two,
2809 .block_two_semicolon,2809 .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,
2818 => true,2810 => true,
2819 else => false,2811 else => false,
2820 };2812 };
src/AstGen.zig+2-1
...@@ -1730,7 +1730,8 @@ fn structInitExprRlNone(...@@ -1730,7 +1730,8 @@ fn structInitExprRlNone(
1730 .container_type = ty_inst,1730 .container_type = ty_inst,
1731 .name_start = str_index,1731 .name_start = str_index,
1732 }) } }1732 }) } }
1733 else .{ .rl = .none };1733 else
1734 .{ .rl = .none };
1734 setExtra(astgen, extra_index, Zir.Inst.StructInitAnon.Item{1735 setExtra(astgen, extra_index, Zir.Inst.StructInitAnon.Item{
1735 .field_name = str_index,1736 .field_name = str_index,
1736 .init = try expr(gz, scope, sub_ri, field_init),1737 .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...@@ -7669,17 +7669,21 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
7669 error_set.fmt(sema.mod),7669 error_set.fmt(sema.mod),
7670 });7670 });
7671 }7671 }
7672 if (payload.zigTypeTag() == .Opaque) {7672 try sema.validateErrorUnionPayloadType(block, payload, rhs_src);
7673 return sema.fail(block, rhs_src, "error union with payload of opaque type '{}' not allowed", .{7673 const err_union_ty = try Type.errorUnion(sema.arena, error_set, payload, sema.mod);
7674 payload.fmt(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),
7675 });7681 });
7676 } else if (payload.zigTypeTag() == .ErrorSet) {7682 } else if (payload_ty.zigTypeTag() == .ErrorSet) {
7677 return sema.fail(block, rhs_src, "error union with payload of error set type '{}' not allowed", .{7683 return sema.fail(block, payload_src, "error union with payload of error set type '{}' not allowed", .{
7678 payload.fmt(sema.mod),7684 payload_ty.fmt(sema.mod),
7679 });7685 });
7680 }7686 }
7681 const err_union_ty = try Type.errorUnion(sema.arena, error_set, payload, sema.mod);
7682 return sema.addType(err_union_ty);
7683}7687}
76847688
7685fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {7689fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -8639,6 +8643,7 @@ fn funcCommon(...@@ -8639,6 +8643,7 @@ fn funcCommon(
8639 const return_type = if (!inferred_error_set or ret_poison)8643 const return_type = if (!inferred_error_set or ret_poison)
8640 bare_return_type8644 bare_return_type
8641 else blk: {8645 else blk: {
8646 try sema.validateErrorUnionPayloadType(block, bare_return_type, ret_ty_src);
8642 const node = try sema.gpa.create(Module.Fn.InferredErrorSetListNode);8647 const node = try sema.gpa.create(Module.Fn.InferredErrorSetListNode);
8643 node.data = .{ .func = new_func };8648 node.data = .{ .func = new_func };
8644 maybe_inferred_error_set_node = node;8649 maybe_inferred_error_set_node = node;
...@@ -8650,15 +8655,15 @@ fn funcCommon(...@@ -8650,15 +8655,15 @@ fn funcCommon(
8650 });8655 });
8651 };8656 };
86528657
8653 if (!bare_return_type.isValidReturnType()) {8658 if (!return_type.isValidReturnType()) {
8654 const opaque_str = if (bare_return_type.zigTypeTag() == .Opaque) "opaque " else "";8659 const opaque_str = if (return_type.zigTypeTag() == .Opaque) "opaque " else "";
8655 const msg = msg: {8660 const msg = msg: {
8656 const msg = try sema.errMsg(block, ret_ty_src, "{s}return type '{}' not allowed", .{8661 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),
8658 });8663 });
8659 errdefer msg.destroy(sema.gpa);8664 errdefer msg.destroy(sema.gpa);
86608665
8661 try sema.addDeclaredHereNote(msg, bare_return_type);8666 try sema.addDeclaredHereNote(msg, return_type);
8662 break :msg msg;8667 break :msg msg;
8663 };8668 };
8664 return sema.failWithOwnedErrorMsg(msg);8669 return sema.failWithOwnedErrorMsg(msg);
...@@ -21930,7 +21935,7 @@ fn zirCUndef(...@@ -21930,7 +21935,7 @@ fn zirCUndef(
21930 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };21935 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
2193121936
21932 const name = try sema.resolveConstString(block, src, extra.operand, "name of macro being undefined must be comptime-known");21937 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});
21934 return Air.Inst.Ref.void_value;21939 return Air.Inst.Ref.void_value;
21935}21940}
2193621941
...@@ -29756,6 +29761,25 @@ fn resolvePeerTypes(...@@ -29756,6 +29761,25 @@ fn resolvePeerTypes(
29756 continue;29761 continue;
29757 }29762 }
29758 },29763 },
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 },
29759 else => {},29783 else => {},
29760 }29784 }
2976129785
src/codegen/llvm.zig+7-1
...@@ -2273,7 +2273,9 @@ pub const Object = struct {...@@ -2273,7 +2273,9 @@ pub const Object = struct {
22732273
2274 const full_di_fields: [2]*llvm.DIType =2274 const full_di_fields: [2]*llvm.DIType =
2275 if (layout.tag_align >= layout.payload_align)2275 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
2278 const full_di_ty = dib.createStructType(2280 const full_di_ty = dib.createStructType(
2279 compile_unit_scope,2281 compile_unit_scope,
...@@ -4162,6 +4164,10 @@ pub const DeclGen = struct {...@@ -4162,6 +4164,10 @@ pub const DeclGen = struct {
4162 if (func.data.owner_decl != decl_index) {4164 if (func.data.owner_decl != decl_index) {
4163 return self.lowerDeclRefValue(tv, func.data.owner_decl);4165 return self.lowerDeclRefValue(tv, func.data.owner_decl);
4164 }4166 }
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 }
4165 }4171 }
41664172
4167 const is_fn_body = decl.ty.zigTypeTag() == .Fn;4173 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" {...@@ -1541,3 +1541,15 @@ test "single item pointer to pointer to array to slice" {
1541 const z1 = @as([]const i32, @as(*[1]i32, &x));1541 const z1 = @as([]const i32, @as(*[1]i32, &x));
1542 try expect(z1[0] == 1234);1542 try expect(z1[0] == 1234);
1543}1543}
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 @@...@@ -1,11 +1,11 @@
1const FooType = opaque {};1const FooType = opaque {};
2export fn bar() !FooType {2export fn bar() FooType {
3 return error.InvalidValue;3 return error.InvalidValue;
4}4}
5export fn bav() !@TypeOf(null) {5export fn bav() @TypeOf(null) {
6 return error.InvalidValue;6 return error.InvalidValue;
7}7}
8export fn baz() !@TypeOf(undefined) {8export fn baz() @TypeOf(undefined) {
9 return error.InvalidValue;9 return error.InvalidValue;
10}10}
1111
...@@ -13,7 +13,7 @@ export fn baz() !@TypeOf(undefined) {...@@ -13,7 +13,7 @@ export fn baz() !@TypeOf(undefined) {
13// backend=stage213// backend=stage2
14// target=native14// target=native
15//15//
16// :2:18: error: opaque return type 'tmp.FooType' not allowed16// :2:17: error: opaque return type 'tmp.FooType' not allowed
17// :1:17: note: opaque declared here17// :1:17: note: opaque declared here
18// :5:18: error: return type '@TypeOf(null)' not allowed18// :5:17: error: return type '@TypeOf(null)' not allowed
19// :8:18: error: return type '@TypeOf(undefined)' not allowed19// :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 {...@@ -4,6 +4,12 @@ comptime {
4comptime {4comptime {
5 _ = anyerror!anyerror;5 _ = anyerror!anyerror;
6}6}
7fn someFunction() !anyerror {
8 return error.C;
9}
10comptime {
11 _ = someFunction;
12}
713
8// error14// error
9// backend=stage215// backend=stage2
...@@ -11,3 +17,4 @@ comptime {...@@ -11,3 +17,4 @@ comptime {
11//17//
12// :2:18: error: error union with payload of opaque type 'anyopaque' not allowed18// :2:18: error: error union with payload of opaque type 'anyopaque' not allowed
13// :5:18: error: error union with payload of error set type 'anyerror' not allowed19// :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