Skip to content

Commit 2fb38ce

Browse files
authored
Uncurried internal representation escapes in error message. (#5892)
* Uncurried internal representation escapes in error message. This is an old standing issue still present. When something which is not a function is applied in an uncurried way, the error message leaks the internal representation of uncurried types. See #5888 * rename * Handle error message where non-function is used in function application. Curried application treats specially the case where the type used is not a function. Adapt uncurried application to do the same. * Update CHANGELOG.md
1 parent 55627e3 commit 2fb38ce

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These are only breaking changes for unformatted code.
4444
- Fix formatting uncurried functions with attributes https://github.com/rescript-lang/rescript-compiler/pull/5829
4545
- Fix parsing/printing uncurried functions with type parameters https://github.com/rescript-lang/rescript-compiler/pull/5849
4646
- Fix compiler ppx issue when combining `async` and uncurried application https://github.com/rescript-lang/rescript-compiler/pull/5856
47+
- Fix issue where the internal representation of uncurried types would leak when a non-function is applied in a curried way https://github.com/rescript-lang/rescript-compiler/pull/5892
4748

4849
#### :nail_care: Polish
4950

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/non_function_uncurried_apply.res:2:9-14
4+
5+
1 │ let nonfun = 2
6+
2 │ let _ = nonfun(. 3)
7+
3 │
8+
9+
This expression has type int
10+
It is not a function.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let nonfun = 2
2+
let _ = nonfun(. 3)

jscomp/ml/ast_uncurried.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let type_to_arity (tArity : Types.type_expr) =
9595
| Tvariant { row_fields = [ (label, _) ] } -> decode_arity_string label
9696
| _ -> assert false
9797

98-
let mk_js_fn ~env ~arity t =
98+
let make_uncurried_type ~env ~arity t =
9999
let typ_arity = arity_to_type arity in
100100
let lid : Longident.t = Lident "function$" in
101101
let path = Env.lookup_type lid env in

jscomp/ml/typecore.ml

+10-4
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,7 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
21072107
(match lid.txt with
21082108
| Lident "Function$" ->
21092109
let arity = Ast_uncurried.attributes_to_arity sexp.pexp_attributes in
2110-
let uncurried_typ = Ast_uncurried.mk_js_fn ~env ~arity (newvar()) in
2110+
let uncurried_typ = Ast_uncurried.make_uncurried_type ~env ~arity (newvar()) in
21112111
unify_exp_types loc env ty_expected uncurried_typ
21122112
| _ -> ());
21132113
type_construct env loc lid sarg ty_expected sexp.pexp_attributes
@@ -2992,8 +2992,14 @@ and type_application uncurried env funct (sargs : sargs) : targs * Types.type_ex
29922992
match has_uncurried_type funct.exp_type with
29932993
| None ->
29942994
let arity = List.length sargs in
2995-
let js_fn = Ast_uncurried.mk_js_fn ~env ~arity (newvar()) in
2996-
unify_exp env funct js_fn
2995+
let uncurried_typ = Ast_uncurried.make_uncurried_type ~env ~arity (newvar()) in
2996+
begin
2997+
match (expand_head env funct.exp_type).desc with
2998+
| Tvar _ | Tarrow _ ->
2999+
unify_exp env funct uncurried_typ
3000+
| _ ->
3001+
raise(Error(funct.exp_loc, env, Apply_non_function (expand_head env funct.exp_type)))
3002+
end
29973003
| Some _ -> () in
29983004
let extract_uncurried_type t =
29993005
match has_uncurried_type t with
@@ -3011,7 +3017,7 @@ and type_application uncurried env funct (sargs : sargs) : targs * Types.type_ex
30113017
if uncurried && not fully_applied then
30123018
raise(Error(funct.exp_loc, env,
30133019
Uncurried_arity_mismatch (t, arity, List.length sargs)));
3014-
let newT = if fully_applied then newT else Ast_uncurried.mk_js_fn ~env ~arity:newarity newT in
3020+
let newT = if fully_applied then newT else Ast_uncurried.make_uncurried_type ~env ~arity:newarity newT in
30153021
(fully_applied, newT)
30163022
| _ -> (false, newT)
30173023
in

0 commit comments

Comments
 (0)