From 69c806733519d3301f9c5ceb5b5c5891fd1b16a9 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 7 Feb 2025 11:38:17 +0100 Subject: [PATCH 1/3] Fix arity error message with optional args. Fixes https://github.com/rescript-lang/rescript/issues/6637 --- compiler/ml/typecore.ml | 15 ++++++++------- .../expected/opt_args_arity.res.expected | 9 +++++++++ .../super_errors/fixtures/opt_args_arity.res | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/build_tests/super_errors/expected/opt_args_arity.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/opt_args_arity.res diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index 8e60080d51..55806aa33b 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -3514,13 +3514,14 @@ and type_application ?type_clash_context total_app env funct (sargs : sargs) : | Some arity -> let newarity = arity - nargs in let fully_applied = newarity <= 0 in - if total_app && not fully_applied then - raise - (Error - ( funct.exp_loc, - env, - Uncurried_arity_mismatch - (funct.exp_type, arity, List.length sargs) )); + (if total_app && not fully_applied then + let required_args = List.length sargs in + raise + (Error + ( funct.exp_loc, + env, + Uncurried_arity_mismatch + (funct.exp_type, required_args + newarity, required_args) ))); let new_t = if fully_applied then new_t else diff --git a/tests/build_tests/super_errors/expected/opt_args_arity.res.expected b/tests/build_tests/super_errors/expected/opt_args_arity.res.expected new file mode 100644 index 0000000000..3d2fbdd0ff --- /dev/null +++ b/tests/build_tests/super_errors/expected/opt_args_arity.res.expected @@ -0,0 +1,9 @@ + + We've found a bug for you! + /.../fixtures/opt_args_arity.res:2:9 + + 1 │ let f = (~a=0, b, c) => a + b + c + 2 │ let x = f(42) + + This function has type (~a: int=?, int, int) => int + It is applied with 1 arguments but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/opt_args_arity.res b/tests/build_tests/super_errors/fixtures/opt_args_arity.res new file mode 100644 index 0000000000..98dc43e248 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/opt_args_arity.res @@ -0,0 +1,2 @@ +let f = (~a=0, b, c) => a + b + c +let x = f(42) \ No newline at end of file From 1607d1bafb0bf0386997b86601007fe7398406ad Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 7 Feb 2025 11:39:19 +0100 Subject: [PATCH 2/3] singular on 1 args --- compiler/ml/typecore.ml | 2 +- .../super_errors/expected/arity_mismatch.res.expected | 2 +- .../super_errors/expected/method_arity_mismatch.res.expected | 2 +- .../super_errors/expected/moreArguments1.res.expected | 2 +- .../super_errors/expected/moreArguments2.res.expected | 2 +- .../super_errors/expected/moreArguments3.res.expected | 2 +- .../super_errors/expected/moreArguments4.res.expected | 2 +- .../super_errors/expected/moreArguments5.res.expected | 2 +- .../super_errors/expected/opt_args_arity.res.expected | 3 ++- .../super_errors/expected/recursive_type.res.expected | 2 +- tests/build_tests/super_errors/expected/warnings1.res.expected | 2 +- tests/build_tests/super_errors/fixtures/opt_args_arity.res | 2 +- 12 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index 55806aa33b..bf53609b9f 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -4464,7 +4464,7 @@ let report_error env ppf error = "@ @[It is applied with @{%d@} argument%s but it requires \ @{%d@}.@]@]" args - (if args = 0 then "" else "s") + (if args = 1 then "" else "s") arity | Field_not_optional (name, typ) -> fprintf ppf "Field @{%s@} is not optional in type %a. Use without ?" diff --git a/tests/build_tests/super_errors/expected/arity_mismatch.res.expected b/tests/build_tests/super_errors/expected/arity_mismatch.res.expected index 0bef35b03c..40b7bc2359 100644 --- a/tests/build_tests/super_errors/expected/arity_mismatch.res.expected +++ b/tests/build_tests/super_errors/expected/arity_mismatch.res.expected @@ -7,4 +7,4 @@ 3 │ This function has type (~f: 'a => 'a, unit) => int - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/method_arity_mismatch.res.expected b/tests/build_tests/super_errors/expected/method_arity_mismatch.res.expected index 62429ee2f4..03f0671056 100644 --- a/tests/build_tests/super_errors/expected/method_arity_mismatch.res.expected +++ b/tests/build_tests/super_errors/expected/method_arity_mismatch.res.expected @@ -9,4 +9,4 @@ 5 │ This function has type (int, int) => unit - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/moreArguments1.res.expected b/tests/build_tests/super_errors/expected/moreArguments1.res.expected index 7741f1c020..4e97918003 100644 --- a/tests/build_tests/super_errors/expected/moreArguments1.res.expected +++ b/tests/build_tests/super_errors/expected/moreArguments1.res.expected @@ -7,4 +7,4 @@ 3 │ This function has type (~a: int, ~b: int) => int - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/moreArguments2.res.expected b/tests/build_tests/super_errors/expected/moreArguments2.res.expected index 4ce85c8818..90b03e1d46 100644 --- a/tests/build_tests/super_errors/expected/moreArguments2.res.expected +++ b/tests/build_tests/super_errors/expected/moreArguments2.res.expected @@ -7,4 +7,4 @@ 3 │ This function has type (int, int) => int - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/moreArguments3.res.expected b/tests/build_tests/super_errors/expected/moreArguments3.res.expected index bb9eec13f8..9bbbaca9a3 100644 --- a/tests/build_tests/super_errors/expected/moreArguments3.res.expected +++ b/tests/build_tests/super_errors/expected/moreArguments3.res.expected @@ -7,4 +7,4 @@ 3 │ This function has type (int, int, 'a, 'b) => int - It is applied with 1 arguments but it requires 4. \ No newline at end of file + It is applied with 1 argument but it requires 4. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/moreArguments4.res.expected b/tests/build_tests/super_errors/expected/moreArguments4.res.expected index ac90eb4320..17fa54bee7 100644 --- a/tests/build_tests/super_errors/expected/moreArguments4.res.expected +++ b/tests/build_tests/super_errors/expected/moreArguments4.res.expected @@ -7,4 +7,4 @@ 3 │ This function has type (int, ~b: int, ~c: 'a, ~d: 'b) => int - It is applied with 1 arguments but it requires 4. \ No newline at end of file + It is applied with 1 argument but it requires 4. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/moreArguments5.res.expected b/tests/build_tests/super_errors/expected/moreArguments5.res.expected index 0def8a7959..e0ec49e0d5 100644 --- a/tests/build_tests/super_errors/expected/moreArguments5.res.expected +++ b/tests/build_tests/super_errors/expected/moreArguments5.res.expected @@ -8,4 +8,4 @@ 6 │ This function has type (int, 'a, 'b, 'c) => Sub.a - It is applied with 1 arguments but it requires 4. \ No newline at end of file + It is applied with 1 argument but it requires 4. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/opt_args_arity.res.expected b/tests/build_tests/super_errors/expected/opt_args_arity.res.expected index 3d2fbdd0ff..67fc28c1ab 100644 --- a/tests/build_tests/super_errors/expected/opt_args_arity.res.expected +++ b/tests/build_tests/super_errors/expected/opt_args_arity.res.expected @@ -4,6 +4,7 @@ 1 │ let f = (~a=0, b, c) => a + b + c 2 │ let x = f(42) + 3 │ This function has type (~a: int=?, int, int) => int - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/recursive_type.res.expected b/tests/build_tests/super_errors/expected/recursive_type.res.expected index 9631bcfa3f..ebb72770eb 100644 --- a/tests/build_tests/super_errors/expected/recursive_type.res.expected +++ b/tests/build_tests/super_errors/expected/recursive_type.res.expected @@ -10,4 +10,4 @@ This function has type ((option<'a>, ([> #List(list<'b>)] as 'b)) => 'c, 'd) => 'c - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/warnings1.res.expected b/tests/build_tests/super_errors/expected/warnings1.res.expected index 3916a8625b..4b94a89f5a 100644 --- a/tests/build_tests/super_errors/expected/warnings1.res.expected +++ b/tests/build_tests/super_errors/expected/warnings1.res.expected @@ -9,4 +9,4 @@ 5 │ } This function has type (int, int) => int - It is applied with 1 arguments but it requires 2. \ No newline at end of file + It is applied with 1 argument but it requires 2. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/opt_args_arity.res b/tests/build_tests/super_errors/fixtures/opt_args_arity.res index 98dc43e248..14dc2994d8 100644 --- a/tests/build_tests/super_errors/fixtures/opt_args_arity.res +++ b/tests/build_tests/super_errors/fixtures/opt_args_arity.res @@ -1,2 +1,2 @@ let f = (~a=0, b, c) => a + b + c -let x = f(42) \ No newline at end of file +let x = f(42) From ca4667b3e79843f62958122bcb691b333f935307 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 7 Feb 2025 11:44:19 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a690a0d3e..999a7b82c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Fix `%external` extension. https://github.com/rescript-lang/rescript/pull/7272 - Fix issue with type environment for unified ops. https://github.com/rescript-lang/rescript/pull/7277 - Fix completion for application with tagged template. https://github.com/rescript-lang/rescript/pull/7278 +- Fix error message for arity in the presence of optional arguments. https://github.com/rescript-lang/rescript/pull/7284 # 12.0.0-alpha.8