From 2c5479e7891edc2f9aae1f0daaa4555c9b60d22f Mon Sep 17 00:00:00 2001 From: James <101812526+WoodchuckXL@users.noreply.github.com> Date: Thu, 18 Jun 2026 16:34:11 -0400 Subject: [PATCH] Forced metafunction resolver to check arity on monoarity functions --- src/classes/metafunction.c | 12 ++++++++++-- test/matrix/identity.morpho | 2 +- test/string/err_split_args2.morpho | 3 +++ test/string/err_split_args3.morpho | 4 ++++ test/string/err_split_args4.morpho | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/string/err_split_args2.morpho create mode 100644 test/string/err_split_args3.morpho create mode 100644 test/string/err_split_args4.morpho diff --git a/src/classes/metafunction.c b/src/classes/metafunction.c index 7f5fa2ba..31099203 100644 --- a/src/classes/metafunction.c +++ b/src/classes/metafunction.c @@ -1175,6 +1175,14 @@ static bool mfcompiler_emitresolver(mfcompiler *compiler, int nresolutions, mfco exactpath.knownarity = resolutions[0].nparams; exactpath.aritychecked = true; path = &exactpath; + + // Add a sparse to verify that nargs matches the known arity + mfindx deflt; + mfcompiler_emitfail(compiler, &deflt); + mfcompilersparseentry correctarity; + correctarity.value = path->knownarity; + ERR_CHECK_RETURN(mfcompiler_emitresolver(compiler, nresolutions, resolutions, path, &correctarity.target)); + return mfcompiler_emitsparse(compiler, 1, &correctarity, deflt, entry); } /* For exact arity without typed params, emit the lone fixed-arity winner. */ @@ -1298,7 +1306,7 @@ void metafunction_disassemble(objectmetafunction *fn) { static bool metafunction_runresolver(objectmetafunction *fn, int nargs, value *args, error *err, value *out) { mfinstruction *instructions = fn->resolver.data; if (!instructions) return metafunction_resolveslow(fn, nargs, args, err, out); - + mfindx pc = fn->entry; int reg = nargs; // Single register initialized with nargs @@ -1307,7 +1315,7 @@ static bool metafunction_runresolver(objectmetafunction *fn, int nargs, value *a case MFOP_SLOW: return metafunction_resolveslow(fn, nargs, args, err, out); case MFOP_RESOLVE: { - pc++; *out=fn->fns.data[instructions[pc]]; + *out=fn->fns.data[instructions[++pc]]; return true; } case MFOP_FAIL: diff --git a/test/matrix/identity.morpho b/test/matrix/identity.morpho index ba47e170..be369c82 100644 --- a/test/matrix/identity.morpho +++ b/test/matrix/identity.morpho @@ -12,4 +12,4 @@ print a // expect: [ 0 0 1 ] a = IdentityMatrix() -// expect error 'MtrxIdnttyCns' +// expect error 'MltplDsptchFld' diff --git a/test/string/err_split_args2.morpho b/test/string/err_split_args2.morpho new file mode 100644 index 00000000..443c078d --- /dev/null +++ b/test/string/err_split_args2.morpho @@ -0,0 +1,3 @@ + +print "Hello world".split(2) +// expect error 'MltplDsptchFld' diff --git a/test/string/err_split_args3.morpho b/test/string/err_split_args3.morpho new file mode 100644 index 00000000..8e0cd01f --- /dev/null +++ b/test/string/err_split_args3.morpho @@ -0,0 +1,4 @@ +// You can not add extra arguments to a function + +print "Hello world".split(" ", 2) +// expect error 'MltplDsptchFld' diff --git a/test/string/err_split_args4.morpho b/test/string/err_split_args4.morpho new file mode 100644 index 00000000..a20dc5a9 --- /dev/null +++ b/test/string/err_split_args4.morpho @@ -0,0 +1,5 @@ +print "Hello world".split(" ") +// expect: [ Hello, world ] + +print "Hello world".split() +// expect error 'MltplDsptchFld'