From a8871fb1bee9f7b11669ee737daa3ef05157e6aa Mon Sep 17 00:00:00 2001 From: Harry Li Date: Fri, 7 Feb 2025 15:42:24 +0800 Subject: [PATCH] Validate number of arguments in application --- .../hkmc2/semantics/ImplicitResolver.scala | 16 +++++++--------- .../src/test/mlscript/backlog/ToTriage.mls | 19 +++++++++++-------- .../src/test/mlscript/basics/StrTest.mls | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ImplicitResolver.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ImplicitResolver.scala index d5653c53a0..d4f83e2e6e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ImplicitResolver.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ImplicitResolver.scala @@ -139,15 +139,13 @@ class ImplicitResolver(tl: TraceLogger) val paramss = tdf.params (paramss zip argss).foreach: case (ps, Term.Tup(args)) => - // * TODO: Check the arity. - // if ps.paramCountUB - // then if ps.paramCountLB != args.length then - // raise(ErrorReport(msg"Expected ${ps.paramCountLB.toString()} arguments, " + - // msg"got ${args.length.toString()}" -> base.toLoc :: Nil)) - // else if ps.paramCountLB > args.length then - // raise(ErrorReport(msg"Expected at least ${ps.paramCountLB.toString()} arguments, " + - // msg"got ${args.length.toString()}" -> base.toLoc :: Nil)) - assert(ps.params.sizeCompare(args) === 0) + if ps.paramCountUB + then if ps.paramCountLB != args.length then + raise(ErrorReport(msg"Expected ${ps.paramCountLB.toString()} arguments, " + + msg"got ${args.length.toString()}" -> base.toLoc :: Nil)) + else if ps.paramCountLB > args.length then + raise(ErrorReport(msg"Expected at least ${ps.paramCountLB.toString()} arguments, " + + msg"got ${args.length.toString()}" -> base.toLoc :: Nil)) (ps.params zip args).foreach((p, arg) => resolveArg(p, arg)(base)) case _ => lastWords("Other argument forms.") diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 3ac7fe664b..63ae3dce7b 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -89,6 +89,9 @@ set g.0 = g.0 + 1 :todo // TODO instrument Predef :re id(1, 2) +//│ ╔══[ERROR] Expected 1 arguments, got 2 +//│ ║ l.91: id(1, 2) +//│ ╙── ^^ //│ = 1 // ——— ——— ——— @@ -149,7 +152,7 @@ f of 2 of 3 //│ ╔══[PARSE ERROR] Expected end of input; found indented block instead -//│ ║ l.150: of 3 +//│ ║ l.153: of 3 //│ ╙── ^^ //│ = [function] @@ -180,7 +183,7 @@ object Oops with :e Oops.fakeField //│ ╔══[ERROR] Object 'Oops' does not contain member 'fakeField' -//│ ║ l.181: Oops.fakeField +//│ ║ l.184: Oops.fakeField //│ ╙── ^^^^^^^^^^ //│ = 1 @@ -246,7 +249,7 @@ object Cls(x) with :e Cls.x //│ ╔══[ERROR] Object 'Cls' does not contain member 'x' -//│ ║ l.247: Cls.x +//│ ║ l.250: Cls.x //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'x' yielded 'undefined' @@ -274,15 +277,15 @@ module Example with // whoops val a = this //│ ╔══[PARSE ERROR] Expected block after type declaration body; found newline instead -//│ ║ l.273: module Example with +//│ ║ l.276: module Example with //│ ║ ^ -//│ ║ l.274: // whoops +//│ ║ l.277: // whoops //│ ╙── //│ ╔══[PARSE ERROR] Expected an expression; found block instead -//│ ║ l.275: val a = this +//│ ║ l.278: val a = this //│ ╙── ^ //│ ╔══[PARSE ERROR] Expected end of input; found indented block instead -//│ ║ l.275: val a = this +//│ ║ l.278: val a = this //│ ╙── ^^ // ——— ——— ——— @@ -290,7 +293,7 @@ module Example with :fixme // ("Not in scope" error) let xs = new Oopsie //│ ╔══[ERROR] Name not found: Oopsie -//│ ║ l.291: let xs = new Oopsie +//│ ║ l.294: let xs = new Oopsie //│ ╙── ^^^^^^ //│ /!!!\ Uncaught error: hkmc2.InternalError: Not in scope: xs (class hkmc2.semantics.VarSymbol) diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index 5dd5ff6249..19d1a96663 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -17,24 +17,31 @@ concat of // TODO (~) should be sanitized +:e (~)("a", "b", "c") +//│ ╔══[ERROR] Expected 2 arguments, got 3 +//│ ║ l.21: (~)("a", "b", "c") +//│ ╙── ^ //│ = "ab" - +:e (~)("a") +//│ ╔══[ERROR] Expected 2 arguments, got 1 +//│ ║ l.28: (~)("a") +//│ ╙── ^ //│ = "aundefined" :ge ~"a" //│ ╔══[COMPILATION ERROR] Unexpected type annotations ~"a" -//│ ║ l.28: ~"a" +//│ ║ l.35: ~"a" //│ ╙── ^^^ :ge ~("a", "b") //│ ╔══[COMPILATION ERROR] Unexpected type annotations ~{ "a"; "b" } -//│ ║ l.35: ~("a", "b") +//│ ║ l.42: ~("a", "b") //│ ╙── ^^^ @@ -42,10 +49,10 @@ concat of :ge ~ of "a", "b" //│ ╔══[PARSE ERROR] Expected start of statement in this position; found 'of' keyword instead -//│ ║ l.43: ~ of "a", "b" +//│ ║ l.50: ~ of "a", "b" //│ ╙── ^^ //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.43: ~ of "a", "b" +//│ ║ l.50: ~ of "a", "b" //│ ╙── ^^^ //│ ═══[COMPILATION ERROR] Unexpected type annotations ~