From 8b1c6d8b76ad1861f963fc7a2a079af5d8729a70 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 2 Feb 2017 15:03:00 -0500 Subject: [PATCH] fix ability to call method on variable at compile time --- src/ir.cpp | 12 +++++++++--- test/cases/undefined.zig | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index b0908dd7cf..e37cee2e07 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -7808,9 +7808,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal size_t next_proto_i = 0; if (first_arg_ptr) { - IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr); - if (first_arg->value.type->id == TypeTableEntryIdInvalid) - return ira->codegen->builtin_types.entry_invalid; + IrInstruction *first_arg; + assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer); + if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) { + first_arg = first_arg_ptr; + } else { + first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr); + if (first_arg->value.type->id == TypeTableEntryIdInvalid) + return ira->codegen->builtin_types.entry_invalid; + } if (!ir_analyze_fn_call_inline_arg(ira, fn_proto_node, first_arg, &exec_scope, &next_proto_i)) return ira->codegen->builtin_types.entry_invalid; diff --git a/test/cases/undefined.zig b/test/cases/undefined.zig index 6d8d7391b2..03e6cfb64f 100644 --- a/test/cases/undefined.zig +++ b/test/cases/undefined.zig @@ -27,6 +27,10 @@ fn initStaticArrayToUndefined() { const Foo = struct { x: i32, + + fn setFooXMethod(foo: &Foo) { + foo.x = 3; + } }; fn setFooX(foo: &Foo) { @@ -47,3 +51,18 @@ fn assignUndefinedToStruct() { assert(foo.x == 2); } } + +fn assignUndefinedToStructWithMethod() { + @setFnTest(this); + + comptime { + var foo: Foo = undefined; + foo.setFooXMethod(); + assert(foo.x == 3); + } + { + var foo: Foo = undefined; + foo.setFooXMethod(); + assert(foo.x == 3); + } +}