Commit 8b1c6d8b76

Andrew Kelley <superjoe30@gmail.com>
2017-02-02 21:03:00
fix ability to call method on variable at compile time
1 parent 2b88441
Changed files (2)
src
test
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;
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);
+    }
+}