Skip to content

Commit

Permalink
Fix arity error message with optional args. (#7284)
Browse files Browse the repository at this point in the history
* Fix arity error message with optional args.

Fixes #6637

* singular on 1 args

* Update CHANGELOG.md
  • Loading branch information
cristianoc authored Feb 7, 2025
1 parent d153a91 commit 9ca9739
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -4463,7 +4464,7 @@ let report_error env ppf error =
"@ @[It is applied with @{<error>%d@} argument%s but it requires \
@{<info>%d@}.@]@]"
args
(if args = 0 then "" else "s")
(if args = 1 then "" else "s")
arity
| Field_not_optional (name, typ) ->
fprintf ppf "Field @{<info>%s@} is not optional in type %a. Use without ?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
3 │

This function has type (~f: 'a => 'a, unit) => int
It is applied with 1 arguments but it requires 2.
It is applied with 1 argument but it requires 2.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
5 │

This function has type (int, int) => unit
It is applied with 1 arguments but it requires 2.
It is applied with 1 argument but it requires 2.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
3 │

This function has type (~a: int, ~b: int) => int
It is applied with 1 arguments but it requires 2.
It is applied with 1 argument but it requires 2.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
3 │

This function has type (int, int) => int
It is applied with 1 arguments but it requires 2.
It is applied with 1 argument but it requires 2.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
3 │

This function has type (int, int, 'a, 'b) => int
It is applied with 1 arguments but it requires 4.
It is applied with 1 argument but it requires 4.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
It is applied with 1 argument but it requires 4.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
It is applied with 1 argument but it requires 4.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
It is applied with 1 argument but it requires 2.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
5 │ }

This function has type (int, int) => int
It is applied with 1 arguments but it requires 2.
It is applied with 1 argument but it requires 2.
2 changes: 2 additions & 0 deletions tests/build_tests/super_errors/fixtures/opt_args_arity.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let f = (~a=0, b, c) => a + b + c
let x = f(42)

1 comment on commit 9ca9739

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 9ca9739 Previous: e1b7fb7 Ratio
Parse RedBlackTree.res - time/run 1.3614971533333333 ms 1.2123143266666667 ms 1.12
Parse Napkinscript.res - time/run 42.21679425333333 ms 39.28006235333333 ms 1.07
Parse HeroGraphic.res - time/run 5.7212339 ms 5.13472718 ms 1.11

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.