authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2023-04-22 20:44:57-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-04-24 09:35:54+03:00
log2be347a2c88e435079333773fc30b8e967cc7068
tree6cc383bfa7dff357fcbcb0085f76f46b070ac999
parentc75e11bf6aa67f2ca62b9b6677d134592777bfec

Sema: allow method calls on optional pointers


2 files changed, 40 insertions(+), 12 deletions(-)

src/Sema.zig+23-12
......@@ -23738,7 +23738,6 @@ fn fieldCallBind(
2373823738 {
2373923739 const first_param_type = decl_type.fnParamType(0);
2374023740 const first_param_tag = first_param_type.tag();
23741 var opt_buf: Type.Payload.ElemType = undefined;
2374223741 // zig fmt: off
2374323742 if (first_param_tag == .var_args_param or
2374423743 first_param_tag == .generic_poison or (
......@@ -23764,17 +23763,29 @@ fn fieldCallBind(
2376423763 .arg0_inst = deref,
2376523764 });
2376623765 return sema.addConstant(ty, value);
23767 } else if (first_param_tag != .generic_poison and first_param_type.zigTypeTag() == .Optional and
23768 first_param_type.optionalChild(&opt_buf).eql(concrete_ty, sema.mod))
23769 {
23770 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
23771 const ty = Type.Tag.bound_fn.init();
23772 const value = try Value.Tag.bound_fn.create(arena, .{
23773 .func_inst = decl_val,
23774 .arg0_inst = deref,
23775 });
23776 return sema.addConstant(ty, value);
23777 } else if (first_param_tag != .generic_poison and first_param_type.zigTypeTag() == .ErrorUnion and
23766 } else if (first_param_type.zigTypeTag() == .Optional) {
23767 var opt_buf: Type.Payload.ElemType = undefined;
23768 const child = first_param_type.optionalChild(&opt_buf);
23769 if (child.eql(concrete_ty, sema.mod)) {
23770 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
23771 const ty = Type.Tag.bound_fn.init();
23772 const value = try Value.Tag.bound_fn.create(arena, .{
23773 .func_inst = decl_val,
23774 .arg0_inst = deref,
23775 });
23776 return sema.addConstant(ty, value);
23777 } else if (child.zigTypeTag() == .Pointer and
23778 child.ptrSize() == .One and
23779 child.childType().eql(concrete_ty, sema.mod))
23780 {
23781 const ty = Type.Tag.bound_fn.init();
23782 const value = try Value.Tag.bound_fn.create(arena, .{
23783 .func_inst = decl_val,
23784 .arg0_inst = object_ptr,
23785 });
23786 return sema.addConstant(ty, value);
23787 }
23788 } else if (first_param_type.zigTypeTag() == .ErrorUnion and
2377823789 first_param_type.errorUnionPayload().eql(concrete_ty, sema.mod))
2377923790 {
2378023791 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
test/behavior/fn.zig+17
......@@ -455,6 +455,23 @@ test "method call with optional and error union first param" {
455455 try s.errUnion();
456456}
457457
458test "method call with optional pointer first param" {
459 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
460 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
461
462 const S = struct {
463 x: i32 = 1234,
464
465 fn method(s: ?*@This()) !void {
466 try expect(s.?.x == 1234);
467 }
468 };
469 var s: S = .{};
470 try s.method();
471 const s_ptr = &s;
472 try s_ptr.method();
473}
474
458475test "using @ptrCast on function pointers" {
459476 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
460477 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO