From 57a0931eb7091c3cc0cee9cbd74f5a03a8be6a35 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 24 Oct 2022 14:08:15 +0200 Subject: [PATCH] :art: clean and test --- src/lib/object_op.c | 2 ++ src/parse/check.c | 20 -------------------- tests/ctor/parent_ctor_already_called.gw | 11 +++++++++++ tests/ctor/parent_ctor_not_this.gw | 11 +++++++++++ tests/ctor/parent_ctor_only_in_new.gw | 9 +++++++++ 5 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 tests/ctor/parent_ctor_already_called.gw create mode 100644 tests/ctor/parent_ctor_not_this.gw create mode 100644 tests/ctor/parent_ctor_only_in_new.gw diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 90f998b4..2ce91b7d 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -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; diff --git a/src/parse/check.c b/src/parse/check.c index 1fea7125..77d51256 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -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 index 00000000..13e52c08 --- /dev/null +++ b/tests/ctor/parent_ctor_already_called.gw @@ -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 index 00000000..78b1c005 --- /dev/null +++ b/tests/ctor/parent_ctor_not_this.gw @@ -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 index 00000000..5b0d2d2c --- /dev/null +++ b/tests/ctor/parent_ctor_only_in_new.gw @@ -0,0 +1,9 @@ +class C { + operator new() {} +} + +class D extends C{ +} + +var D d; +d.C(); -- 2.43.0