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 diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index 8e60080d51..bf53609b9f 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 @@ -4463,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 new file mode 100644 index 0000000000..67fc28c1ab --- /dev/null +++ b/tests/build_tests/super_errors/expected/opt_args_arity.res.expected @@ -0,0 +1,10 @@ + + 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) + 3 │ + + This function has type (~a: int=?, int, int) => int + 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 new file mode 100644 index 0000000000..14dc2994d8 --- /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)