Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arity error message with optional args. #7284

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading