Skip to content

Commit ef6bf7b

Browse files
committed
Fix issue with error messages for uncurried functions where expected and given type were swapped
The logic for unification for `Function$`, which unifies with a generic uncurried type, was swapped. The error message for arity mismatch was also swapped, so those errors were showing correctly. However, in the case of expected curried function, when given a curried function whose type is begin inferred, the error message was swapped. Also added a specialized error message when the uncurried type only contains a type variable, so `$function<'a, ...>` does not leak in the output. This happens as unification failt early, and the type of the function has not been determined yet.
1 parent 31280da commit ef6bf7b

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ These are only breaking changes for unformatted code.
5454
- Fix issue where error messages related to non-existent props were displayed without location information https://github.com/rescript-lang/rescript-compiler/pull/5960
5555
- Fix type inference issue with uncurried functions applied to a single unit argument. The issue was introduced in https://github.com/rescript-lang/rescript-compiler/pull/5907 when adding support to `foo()` when `foo` has only optional arguments. https://github.com/rescript-lang/rescript-compiler/pull/5970
5656
- Fix issue where uncurried functions were incorrectly converting the type of a prop given as a default value to curried https://github.com/rescript-lang/rescript-compiler/pull/5971
57+
- Fix issue with error messages for uncurried functions where expected and given type were swapped
5758

5859
#### :nail_care: Polish
5960

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/curried_expected.res:3:24-38
4+
5+
1 │ let expectCurried = f => f(1) + 2
6+
2 │
7+
3 │ let z1 = expectCurried((. x, y) => x+y)
8+
4 │
9+
10+
This function is an uncurried function where a curried function is expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let expectCurried = f => f(1) + 2
2+
3+
let z1 = expectCurried((. x, y) => x+y)

jscomp/ml/typecore.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,7 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
21072107
let state = Warnings.backup () in
21082108
let arity = Ast_uncurried.attributes_to_arity sexp.pexp_attributes in
21092109
let uncurried_typ = Ast_uncurried.make_uncurried_type ~env ~arity (newvar()) in
2110-
unify_exp_types loc env ty_expected uncurried_typ;
2110+
unify_exp_types loc env uncurried_typ ty_expected;
21112111
(* Disable Unerasable_optional_argument for uncurried functions *)
21122112
let unerasable_optional_argument = Warnings.number Unerasable_optional_argument in
21132113
Warnings.parse_options false ("-" ^ string_of_int unerasable_optional_argument);

jscomp/super_errors/super_typecore.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ end
121121

122122
let reportArityMismatch ~arityA ~arityB ppf =
123123
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
124-
arityA
125-
(if arityA = "1" then "argument" else "arguments")
126124
arityB
125+
(if arityB = "1" then "argument" else "arguments")
126+
arityA
127127

128128
(* Pasted from typecore.ml. Needed for some cases in report_error below *)
129129
(* Records *)
@@ -169,6 +169,11 @@ let report_error env ppf = function
169169
(_, {desc = Tconstr (Pident {name = "function$"},_,_)}) :: _
170170
) ->
171171
fprintf ppf "This function is a curried function where an uncurried function is expected"
172+
| Expr_type_clash (
173+
(_, {desc = Tconstr (Pident {name = "function$"}, [{desc=Tvar _}; _],_)}) ::
174+
(_, {desc = Tarrow _}) :: _
175+
) ->
176+
fprintf ppf "This function is an uncurried function where a curried function is expected"
172177
| Expr_type_clash (
173178
(_, {desc = Tconstr (Pident {name = "function$"},[_; tA],_)}) ::
174179
(_, {desc = Tconstr (Pident {name = "function$"},[_; tB],_)}) :: _

0 commit comments

Comments
 (0)