authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-19 11:35:49+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-19 20:21:19+02:00
loge02749224357a33cffcaf9970d14c199adc3d892
tree90970730e6d00583eeb8789e035d1f1dd8edc466
parent2f0204aca303daf899a97c740719a62398adc206

stage2: support anon init through error unions and optionals


5 files changed, 183 insertions(+), 14 deletions(-)

src/AstGen.zig+7-2
......@@ -1384,7 +1384,8 @@ fn arrayInitExprRlPtr(
13841384 array_ty: Zir.Inst.Ref,
13851385) InnerError!Zir.Inst.Ref {
13861386 if (array_ty == .none) {
1387 return arrayInitExprRlPtrInner(gz, scope, node, result_ptr, elements);
1387 const base_ptr = try gz.addUnNode(.array_base_ptr, result_ptr, node);
1388 return arrayInitExprRlPtrInner(gz, scope, node, base_ptr, elements);
13881389 }
13891390
13901391 var as_scope = try gz.makeCoercionScope(scope, array_ty, result_ptr);
......@@ -1493,6 +1494,7 @@ fn structInitExpr(
14931494
14941495 switch (rl) {
14951496 .discard => {
1497 // TODO if a type expr is given the fields should be validated for that type
14961498 if (struct_init.ast.type_expr != 0)
14971499 _ = try typeExpr(gz, scope, struct_init.ast.type_expr);
14981500 for (struct_init.ast.fields) |field_init| {
......@@ -1567,7 +1569,8 @@ fn structInitExprRlPtr(
15671569 result_ptr: Zir.Inst.Ref,
15681570) InnerError!Zir.Inst.Ref {
15691571 if (struct_init.ast.type_expr == 0) {
1570 return structInitExprRlPtrInner(gz, scope, node, struct_init, result_ptr);
1572 const base_ptr = try gz.addUnNode(.field_base_ptr, result_ptr, node);
1573 return structInitExprRlPtrInner(gz, scope, node, struct_init, base_ptr);
15711574 }
15721575 const ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
15731576
......@@ -2281,6 +2284,8 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
22812284 .ret_err_value_code,
22822285 .extended,
22832286 .closure_get,
2287 .array_base_ptr,
2288 .field_base_ptr,
22842289 => break :b false,
22852290
22862291 // ZIR instructions that are always `noreturn`.
src/Sema.zig+97-12
......@@ -739,6 +739,8 @@ fn analyzeBodyInner(
739739 .@"await" => try sema.zirAwait(block, inst, false),
740740 .await_nosuspend => try sema.zirAwait(block, inst, true),
741741 .extended => try sema.zirExtended(block, inst),
742 .array_base_ptr => try sema.zirArrayBasePtr(block, inst),
743 .field_base_ptr => try sema.zirFieldBasePtr(block, inst),
742744
743745 .clz => try sema.zirBitCount(block, inst, .clz, Value.clz),
744746 .ctz => try sema.zirBitCount(block, inst, .ctz, Value.ctz),
......@@ -2584,6 +2586,59 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
25842586 }
25852587}
25862588
2589fn zirArrayBasePtr(
2590 sema: *Sema,
2591 block: *Block,
2592 inst: Zir.Inst.Index,
2593) CompileError!Air.Inst.Ref {
2594 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2595 const src = inst_data.src();
2596
2597 const start_ptr = sema.resolveInst(inst_data.operand);
2598 var base_ptr = start_ptr;
2599 while (true) switch (sema.typeOf(base_ptr).childType().zigTypeTag()) {
2600 .ErrorUnion => base_ptr = try sema.analyzeErrUnionPayloadPtr(block, src, base_ptr, false),
2601 .Optional => base_ptr = try sema.analyzeOptionalPayloadPtr(block, src, base_ptr, false),
2602 else => break,
2603 };
2604
2605 const elem_ty = sema.typeOf(base_ptr).childType();
2606 switch (elem_ty.zigTypeTag()) {
2607 .Array, .Vector => return base_ptr,
2608 .Struct => if (elem_ty.isTuple()) return base_ptr,
2609 else => {},
2610 }
2611 return sema.fail(block, src, "type '{}' does not support array initialization syntax", .{
2612 sema.typeOf(start_ptr).childType(),
2613 });
2614}
2615
2616fn zirFieldBasePtr(
2617 sema: *Sema,
2618 block: *Block,
2619 inst: Zir.Inst.Index,
2620) CompileError!Air.Inst.Ref {
2621 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2622 const src = inst_data.src();
2623
2624 const start_ptr = sema.resolveInst(inst_data.operand);
2625 var base_ptr = start_ptr;
2626 while (true) switch (sema.typeOf(base_ptr).childType().zigTypeTag()) {
2627 .ErrorUnion => base_ptr = try sema.analyzeErrUnionPayloadPtr(block, src, base_ptr, false),
2628 .Optional => base_ptr = try sema.analyzeOptionalPayloadPtr(block, src, base_ptr, false),
2629 else => break,
2630 };
2631
2632 const elem_ty = sema.typeOf(base_ptr).childType();
2633 switch (elem_ty.zigTypeTag()) {
2634 .Struct, .Union => return base_ptr,
2635 else => {},
2636 }
2637 return sema.fail(block, src, "type '{}' does not support struct initialization syntax", .{
2638 sema.typeOf(start_ptr).childType(),
2639 });
2640}
2641
25872642fn zirValidateStructInit(
25882643 sema: *Sema,
25892644 block: *Block,
......@@ -4377,7 +4432,9 @@ fn analyzeCall(
43774432 if (payload.data.error_set.tag() == .error_set_inferred) {
43784433 const node = try sema.gpa.create(Module.Fn.InferredErrorSetListNode);
43794434 node.data = .{ .func = module_fn };
4380 parent_func.?.inferred_error_sets.prepend(node);
4435 if (parent_func) |some| {
4436 some.inferred_error_sets.prepend(node);
4437 }
43814438
43824439 const error_set_ty = try Type.Tag.error_set_inferred.create(sema.arena, &node.data);
43834440 break :blk try Type.Tag.error_union.create(sema.arena, .{
......@@ -5198,9 +5255,20 @@ fn zirOptionalPayloadPtr(
51985255
51995256 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
52005257 const optional_ptr = sema.resolveInst(inst_data.operand);
5258 const src = inst_data.src();
5259
5260 return sema.analyzeOptionalPayloadPtr(block, src, optional_ptr, safety_check);
5261}
5262
5263fn analyzeOptionalPayloadPtr(
5264 sema: *Sema,
5265 block: *Block,
5266 src: LazySrcLoc,
5267 optional_ptr: Air.Inst.Ref,
5268 safety_check: bool,
5269) CompileError!Air.Inst.Ref {
52015270 const optional_ptr_ty = sema.typeOf(optional_ptr);
52025271 assert(optional_ptr_ty.zigTypeTag() == .Pointer);
5203 const src = inst_data.src();
52045272
52055273 const opt_type = optional_ptr_ty.elemType();
52065274 if (opt_type.zigTypeTag() != .Optional) {
......@@ -5216,8 +5284,10 @@ fn zirOptionalPayloadPtr(
52165284
52175285 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {
52185286 if (try sema.pointerDeref(block, src, pointer_val, optional_ptr_ty)) |val| {
5219 if (val.isNull()) {
5220 return sema.fail(block, src, "unable to unwrap null", .{});
5287 if (safety_check) {
5288 if (val.isNull()) {
5289 return sema.fail(block, src, "unable to unwrap null", .{});
5290 }
52215291 }
52225292 // The same Value represents the pointer to the optional and the payload.
52235293 return sema.addConstant(
......@@ -5333,8 +5403,19 @@ fn zirErrUnionPayloadPtr(
53335403 defer tracy.end();
53345404
53355405 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5336 const src = inst_data.src();
53375406 const operand = sema.resolveInst(inst_data.operand);
5407 const src = inst_data.src();
5408
5409 return sema.analyzeErrUnionPayloadPtr(block, src, operand, safety_check);
5410}
5411
5412fn analyzeErrUnionPayloadPtr(
5413 sema: *Sema,
5414 block: *Block,
5415 src: LazySrcLoc,
5416 operand: Air.Inst.Ref,
5417 safety_check: bool,
5418) CompileError!Air.Inst.Ref {
53385419 const operand_ty = sema.typeOf(operand);
53395420 assert(operand_ty.zigTypeTag() == .Pointer);
53405421
......@@ -5350,9 +5431,12 @@ fn zirErrUnionPayloadPtr(
53505431
53515432 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
53525433 if (try sema.pointerDeref(block, src, pointer_val, operand_ty)) |val| {
5353 if (val.getError()) |name| {
5354 return sema.fail(block, src, "caught unexpected error '{s}'", .{name});
5434 if (safety_check) {
5435 if (val.getError()) |name| {
5436 return sema.fail(block, src, "caught unexpected error '{s}'", .{name});
5437 }
53555438 }
5439
53565440 return sema.addConstant(
53575441 operand_pointer_ty,
53585442 try Value.Tag.eu_payload_ptr.create(sema.arena, pointer_val),
......@@ -12859,7 +12943,7 @@ fn zirCUndef(
1285912943 extended: Zir.Inst.Extended.InstData,
1286012944) CompileError!Air.Inst.Ref {
1286112945 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
12862 const src: LazySrcLoc = .{ .node_offset = extra.node };
12946 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1286312947
1286412948 const name = try sema.resolveConstString(block, src, extra.operand);
1286512949 try block.c_import_buf.?.writer().print("#undefine {s}\n", .{name});
......@@ -12872,7 +12956,7 @@ fn zirCInclude(
1287212956 extended: Zir.Inst.Extended.InstData,
1287312957) CompileError!Air.Inst.Ref {
1287412958 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
12875 const src: LazySrcLoc = .{ .node_offset = extra.node };
12959 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1287612960
1287712961 const name = try sema.resolveConstString(block, src, extra.operand);
1287812962 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});
......@@ -12885,12 +12969,13 @@ fn zirCDefine(
1288512969 extended: Zir.Inst.Extended.InstData,
1288612970) CompileError!Air.Inst.Ref {
1288712971 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
12888 const src: LazySrcLoc = .{ .node_offset = extra.node };
12972 const name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
12973 const val_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
1288912974
12890 const name = try sema.resolveConstString(block, src, extra.lhs);
12975 const name = try sema.resolveConstString(block, name_src, extra.lhs);
1289112976 const rhs = sema.resolveInst(extra.rhs);
1289212977 if (sema.typeOf(rhs).zigTypeTag() != .Void) {
12893 const value = try sema.resolveConstString(block, src, extra.rhs);
12978 const value = try sema.resolveConstString(block, val_src, extra.rhs);
1289412979 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
1289512980 } else {
1289612981 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});
src/Zir.zig+16
......@@ -643,6 +643,18 @@ pub const Inst = struct {
643643 /// Result is a pointer to the value.
644644 /// Uses the `switch_capture` field.
645645 switch_capture_multi_ref,
646 /// Given a
647 /// *A returns *A
648 /// *E!A returns *A
649 /// *?A returns *A
650 /// Uses the `un_node` field.
651 array_base_ptr,
652 /// Given a
653 /// *S returns *S
654 /// *E!S returns *S
655 /// *?S returns *S
656 /// Uses the `un_node` field.
657 field_base_ptr,
646658 /// Given a set of `field_ptr` instructions, assumes they are all part of a struct
647659 /// initialization expression, and emits compile errors for duplicate fields
648660 /// as well as missing fields, if applicable.
......@@ -1087,6 +1099,8 @@ pub const Inst = struct {
10871099 .switch_block,
10881100 .switch_cond,
10891101 .switch_cond_ref,
1102 .array_base_ptr,
1103 .field_base_ptr,
10901104 .validate_struct_init,
10911105 .validate_struct_init_comptime,
10921106 .validate_array_init,
......@@ -1340,6 +1354,8 @@ pub const Inst = struct {
13401354 .switch_capture_ref = .switch_capture,
13411355 .switch_capture_multi = .switch_capture,
13421356 .switch_capture_multi_ref = .switch_capture,
1357 .array_base_ptr = .un_node,
1358 .field_base_ptr = .un_node,
13431359 .validate_struct_init = .pl_node,
13441360 .validate_struct_init_comptime = .pl_node,
13451361 .validate_array_init = .pl_node,
src/print_zir.zig+2
......@@ -235,6 +235,8 @@ const Writer = struct {
235235 .fence,
236236 .switch_cond,
237237 .switch_cond_ref,
238 .array_base_ptr,
239 .field_base_ptr,
238240 => try self.writeUnNode(stream, inst),
239241
240242 .ref,
test/behavior/struct.zig+61
......@@ -1199,3 +1199,64 @@ test "for loop over pointers to struct, getting field from struct pointer" {
11991199 };
12001200 try S.doTheTest();
12011201}
1202
1203test "anon init through error unions and optionals" {
1204 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1205 if (true) return error.SkipZigTest; // TODO
1206
1207 const S = struct {
1208 a: u32,
1209
1210 fn foo() anyerror!?anyerror!@This() {
1211 return @This(){ .a = 1 };
1212 }
1213 fn bar() ?anyerror![2]u8 {
1214 return [2]u8{ 1, 2 };
1215 }
1216
1217 fn doTheTest() !void {
1218 var a = ((foo() catch unreachable).?) catch unreachable;
1219 var b = (bar().?) catch unreachable;
1220 try expect(a.a + b[1] == 3);
1221 }
1222 };
1223
1224 try S.doTheTest();
1225 comptime try S.doTheTest();
1226}
1227
1228test "anon init through optional" {
1229 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1230 // not sure why this is needed, we only do the test at comptime
1231 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest;
1232
1233 const S = struct {
1234 a: u32,
1235
1236 fn doTheTest() !void {
1237 var s: ?@This() = null;
1238 s = .{ .a = 1 };
1239 try expect(s.?.a == 1);
1240 }
1241 };
1242 // try S.doTheTest(); // TODO
1243 comptime try S.doTheTest();
1244}
1245
1246test "anon init through error union" {
1247 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1248 // not sure why this is needed, we only do the test at comptime
1249 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest;
1250
1251 const S = struct {
1252 a: u32,
1253
1254 fn doTheTest() !void {
1255 var s: anyerror!@This() = error.Foo;
1256 s = .{ .a = 1 };
1257 try expect((try s).a == 1);
1258 }
1259 };
1260 // try S.doTheTest(); // TODO
1261 comptime try S.doTheTest();
1262}