]> Nishi Git Mirror - gwion.git/commitdiff
:art: clean and test
authorfennecdjay <fennecdjay@gmail.com>
Mon, 24 Oct 2022 12:08:15 +0000 (14:08 +0200)
committerfennecdjay <fennecdjay@gmail.com>
Mon, 24 Oct 2022 12:08:15 +0000 (14:08 +0200)
src/lib/object_op.c
src/parse/check.c
tests/ctor/parent_ctor_already_called.gw [new file with mode: 0644]
tests/ctor/parent_ctor_not_this.gw [new file with mode: 0644]
tests/ctor/parent_ctor_only_in_new.gw [new file with mode: 0644]

index 90f998b48d92e73b7e24c61a92a37f418398dcbd..2ce91b7dda2e6d32f979a0dc35018860e337ee9e 100644 (file)
@@ -227,6 +227,8 @@ OP_CHECK(opck_object_dot) {
                member->base->d.prim.prim_type != ae_prim_id ||
                strcmp(s_name(member->base->d.prim.d.var), "this"))
               ERR_N(exp_self(member)->pos, "calling a parent constructor is only allowed with `this`");
+            if(GET_FLAG(env->func, const))
+              ERR_N(exp_self(member)->pos, "parent constructor already called");
             SET_FLAG(env->func, const); // mark the function as ctor_ok
             const Value ret = nspc_lookup_value1(parent->nspc, sym);
             member->xid = sym;
index 1fea7125ec60dfb4863d4797178ee449698010d7..77d51256ec0535ad9894b623c8703d50ee25d2e2 100644 (file)
@@ -1870,31 +1870,11 @@ ANN m_bool _check_func_def(const Env env, const Func_Def f) {
   return ret;
 }
 
-ANN m_bool check_new(const Env env, const Func_Def fdef) {
-  if(!fdef->builtin && !GET_FLAG(fdef->base->func, const) &&
-    GET_FLAG(env->class_def->info->parent, abstract)) {
-    // we can probably do simpler, but this may require fixing new@ index
-    const Value v = nspc_lookup_value0(env->class_def->info->parent->nspc, fdef->base->xid);
-    if(v) {
-      Func f = v->d.func_ref;
-      while(f) {
-        if(compat_func(fdef, f->def)) break;
-        f = f->next;
-      }
-      if(f && GET_FLAG(f, abstract)) ERR_B(fdef->base->pos, "What what?");
-    }
-  }
-  return GW_OK;
-}
-
 ANN m_bool check_func_def(const Env env, const Func_Def fdef) {
   const uint16_t depth = env->scope->depth;
   env->scope->depth = 0;
   const m_bool ret = _check_func_def(env, fdef);
   env->scope->depth = depth;
-  // we need to find matching parent and see if abstract
-  if(!strcmp(s_name(fdef->base->xid), "new"))
-    CHECK_BB(check_new(env, fdef));
   return ret;
 }
 
diff --git a/tests/ctor/parent_ctor_already_called.gw b/tests/ctor/parent_ctor_already_called.gw
new file mode 100644 (file)
index 0000000..13e52c0
--- /dev/null
@@ -0,0 +1,11 @@
+#! [contains] parent constructor already called
+class C {
+  operator new() {}
+}
+
+class D extends C {
+  operator new() {
+    this.C();
+    this.C();
+  }
+}
diff --git a/tests/ctor/parent_ctor_not_this.gw b/tests/ctor/parent_ctor_not_this.gw
new file mode 100644 (file)
index 0000000..78b1c00
--- /dev/null
@@ -0,0 +1,11 @@
+#! [contains] calling a parent constructor is only allowed with
+class C {
+  operator new() {}
+}
+
+class D extends C {
+  operator new() {
+    var D d;
+    d.C();
+  }
+}
diff --git a/tests/ctor/parent_ctor_only_in_new.gw b/tests/ctor/parent_ctor_only_in_new.gw
new file mode 100644 (file)
index 0000000..5b0d2d2
--- /dev/null
@@ -0,0 +1,9 @@
+class C {
+  operator new() {}
+}
+
+class D extends C{
+}
+
+var D d;
+d.C();