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...@@ -7808,9 +7808,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
78087808
7809 size_t next_proto_i = 0;7809 size_t next_proto_i = 0;
7810 if (first_arg_ptr) {7810 if (first_arg_ptr) {
7811 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);7811 IrInstruction *first_arg;
7812 if (first_arg->value.type->id == TypeTableEntryIdInvalid)7812 assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
7813 return ira->codegen->builtin_types.entry_invalid;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
7815 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i))7821 if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i))
7816 return ira->codegen->builtin_types.entry_invalid;7822 return ira->codegen->builtin_types.entry_invalid;
test/cases/undefined.zig+19
...@@ -27,6 +27,10 @@ fn initStaticArrayToUndefined() {...@@ -27,6 +27,10 @@ fn initStaticArrayToUndefined() {
2727
28const Foo = struct {28const Foo = struct {
29 x: i32,29 x: i32,
30
31 fn setFooXMethod(foo: &Foo) {
32 foo.x = 3;
33 }
30};34};
3135
32fn setFooX(foo: &Foo) {36fn setFooX(foo: &Foo) {
...@@ -47,3 +51,18 @@ fn assignUndefinedToStruct() {...@@ -47,3 +51,18 @@ fn assignUndefinedToStruct() {
47 assert(foo.x == 2);51 assert(foo.x == 2);
48 }52 }
49}53}
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}