authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-02 15:03:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-02 15:03:21-05:00
log8b1c6d8b76ad1861f963fc7a2a079af5d8729a70
tree904a60a263aed9a8dfb5c02136cead6a66599591
parent2b88441295d39d3e988c0771b6ad64531948ff0a

fix ability to call method on variable at compile time


2 files changed, 28 insertions(+), 3 deletions(-)

src/ir.cpp+9-3
......@@ -7808,9 +7808,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
78087808
78097809 size_t next_proto_i = 0;
78107810 if (first_arg_ptr) {
7811 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7812 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7813 return ira->codegen->builtin_types.entry_invalid;
7811 IrInstruction *first_arg;
7812 assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
7813 if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
7814 first_arg = first_arg_ptr;
7815 } else {
7816 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7817 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7818 return ira->codegen->builtin_types.entry_invalid;
7819 }
78147820
78157821 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i))
78167822 return ira->codegen->builtin_types.entry_invalid;
test/cases/undefined.zig+19
......@@ -27,6 +27,10 @@ fn initStaticArrayToUndefined() {
2727
2828const Foo = struct {
2929 x: i32,
30
31 fn setFooXMethod(foo: &Foo) {
32 foo.x = 3;
33 }
3034};
3135
3236fn setFooX(foo: &Foo) {
......@@ -47,3 +51,18 @@ fn assignUndefinedToStruct() {
4751 assert(foo.x == 2);
4852 }
4953}
54
55fn assignUndefinedToStructWithMethod() {
56 @setFnTest(this);
57
58 comptime {
59 var foo: Foo = undefined;
60 foo.setFooXMethod();
61 assert(foo.x == 3);
62 }
63 {
64 var foo: Foo = undefined;
65 foo.setFooXMethod();
66 assert(foo.x == 3);
67 }
68}