Skip to content

Commit 9ca9739

Browse files
authored
Fix arity error message with optional args. (#7284)
* Fix arity error message with optional args. Fixes #6637 * singular on 1 args * Update CHANGELOG.md
1 parent d153a91 commit 9ca9739

13 files changed

+31
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fix `%external` extension. https://github.com/rescript-lang/rescript/pull/7272
2323
- Fix issue with type environment for unified ops. https://github.com/rescript-lang/rescript/pull/7277
2424
- Fix completion for application with tagged template. https://github.com/rescript-lang/rescript/pull/7278
25+
- Fix error message for arity in the presence of optional arguments. https://github.com/rescript-lang/rescript/pull/7284
2526

2627
# 12.0.0-alpha.8
2728

compiler/ml/typecore.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,13 +3514,14 @@ and type_application ?type_clash_context total_app env funct (sargs : sargs) :
35143514
| Some arity ->
35153515
let newarity = arity - nargs in
35163516
let fully_applied = newarity <= 0 in
3517-
if total_app && not fully_applied then
3518-
raise
3519-
(Error
3520-
( funct.exp_loc,
3521-
env,
3522-
Uncurried_arity_mismatch
3523-
(funct.exp_type, arity, List.length sargs) ));
3517+
(if total_app && not fully_applied then
3518+
let required_args = List.length sargs in
3519+
raise
3520+
(Error
3521+
( funct.exp_loc,
3522+
env,
3523+
Uncurried_arity_mismatch
3524+
(funct.exp_type, required_args + newarity, required_args) )));
35243525
let new_t =
35253526
if fully_applied then new_t
35263527
else
@@ -4463,7 +4464,7 @@ let report_error env ppf error =
44634464
"@ @[It is applied with @{<error>%d@} argument%s but it requires \
44644465
@{<info>%d@}.@]@]"
44654466
args
4466-
(if args = 0 then "" else "s")
4467+
(if args = 1 then "" else "s")
44674468
arity
44684469
| Field_not_optional (name, typ) ->
44694470
fprintf ppf "Field @{<info>%s@} is not optional in type %a. Use without ?"

tests/build_tests/super_errors/expected/arity_mismatch.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │
88

99
This function has type (~f: 'a => 'a, unit) => int
10-
It is applied with 1 arguments but it requires 2.
10+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/method_arity_mismatch.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
5 │
1010

1111
This function has type (int, int) => unit
12-
It is applied with 1 arguments but it requires 2.
12+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/moreArguments1.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │
88

99
This function has type (~a: int, ~b: int) => int
10-
It is applied with 1 arguments but it requires 2.
10+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/moreArguments2.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │
88

99
This function has type (int, int) => int
10-
It is applied with 1 arguments but it requires 2.
10+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/moreArguments3.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │
88

99
This function has type (int, int, 'a, 'b) => int
10-
It is applied with 1 arguments but it requires 4.
10+
It is applied with 1 argument but it requires 4.

tests/build_tests/super_errors/expected/moreArguments4.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │
88

99
This function has type (int, ~b: int, ~c: 'a, ~d: 'b) => int
10-
It is applied with 1 arguments but it requires 4.
10+
It is applied with 1 argument but it requires 4.

tests/build_tests/super_errors/expected/moreArguments5.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
6 │
99

1010
This function has type (int, 'a, 'b, 'c) => Sub.a
11-
It is applied with 1 arguments but it requires 4.
11+
It is applied with 1 argument but it requires 4.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/opt_args_arity.res:2:9
4+
5+
1 │ let f = (~a=0, b, c) => a + b + c
6+
2 │ let x = f(42)
7+
3 │
8+
9+
This function has type (~a: int=?, int, int) => int
10+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/recursive_type.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
This function has type
1212
((option<'a>, ([> #List(list<'b>)] as 'b)) => 'c, 'd) => 'c
13-
It is applied with 1 arguments but it requires 2.
13+
It is applied with 1 argument but it requires 2.

tests/build_tests/super_errors/expected/warnings1.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
5 │ }
1010

1111
This function has type (int, int) => int
12-
It is applied with 1 arguments but it requires 2.
12+
It is applied with 1 argument but it requires 2.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let f = (~a=0, b, c) => a + b + c
2+
let x = f(42)

0 commit comments

Comments
 (0)