Skip to content

Commit def3a2a

Browse files
authored
Merge pull request #4940 from chenglou/arity-se
[Super errors] better arity mismatch error for uncurried function
2 parents dec7e86 + 124c091 commit def3a2a

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

jscomp/build_tests/super_errors/expected/arity_mismatch.re.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
2 │ let makeVar = (. ~f, ()) => 34;
77
3 │ let makeVariables = makeVar(. ~f=f => f);
88

9-
This function has arity2 but was expected arity1
9+
This function expected 2 arguments, but got 1

jscomp/build_tests/super_errors/expected/arity_mismatch2.re.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
2 │ let makeVar = (. f, ()) => 34;
77
3 │ let makeVariables = makeVar(. 1,2,3);
88

9-
This function has arity2 but was expected arity3
9+
This function expected 2 arguments, but got 3

jscomp/super_errors/super_typecore.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ let print_expr_type_clash env trace ppf = begin
124124
show_extra_help ppf env trace;
125125
end
126126

127+
let reportJsFnArityMismatch ~arityA ~arityB ppf =
128+
let extractArity s =
129+
if Ext_string.starts_with s "arity" then
130+
(* get the number part of e.g. arity12 *)
131+
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
132+
String.sub s 5 ((String.length s) - 5)
133+
else
134+
raise (Invalid_argument "Unrecognized arity type name.")
135+
in
136+
let firstNumber = extractArity arityA in
137+
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
138+
firstNumber
139+
(if firstNumber = "1" then "argument" else "arguments")
140+
(extractArity arityB)
141+
142+
127143
(* Pasted from typecore.ml. Needed for some cases in report_error below *)
128144
(* Records *)
129145
let label_of_kind kind =
@@ -169,10 +185,10 @@ let report_error env ppf = function
169185
) ->
170186
fprintf ppf "This function is a curried function where an uncurried function is expected"
171187
| Expr_type_clash (
172-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
173-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
174-
) when a <> b ->
175-
fprintf ppf "This function has %s but was expected %s" a b
188+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
189+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
190+
) ->
191+
reportJsFnArityMismatch ~arityA ~arityB ppf
176192
| Expr_type_clash (
177193
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
178194
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409843,6 +409843,22 @@ let print_expr_type_clash env trace ppf = begin
409843409843
show_extra_help ppf env trace;
409844409844
end
409845409845

409846+
let reportJsFnArityMismatch ~arityA ~arityB ppf =
409847+
let extractArity s =
409848+
if Ext_string.starts_with s "arity" then
409849+
(* get the number part of e.g. arity12 *)
409850+
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
409851+
String.sub s 5 ((String.length s) - 5)
409852+
else
409853+
raise (Invalid_argument "Unrecognized arity type name.")
409854+
in
409855+
let firstNumber = extractArity arityA in
409856+
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
409857+
firstNumber
409858+
(if firstNumber = "1" then "argument" else "arguments")
409859+
(extractArity arityB)
409860+
409861+
409846409862
(* Pasted from typecore.ml. Needed for some cases in report_error below *)
409847409863
(* Records *)
409848409864
let label_of_kind kind =
@@ -409888,10 +409904,10 @@ let report_error env ppf = function
409888409904
) ->
409889409905
fprintf ppf "This function is a curried function where an uncurried function is expected"
409890409906
| Expr_type_clash (
409891-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
409892-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
409893-
) when a <> b ->
409894-
fprintf ppf "This function has %s but was expected %s" a b
409907+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
409908+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
409909+
) ->
409910+
reportJsFnArityMismatch ~arityA ~arityB ppf
409895409911
| Expr_type_clash (
409896409912
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
409897409913
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _

lib/4.06.1/whole_compiler.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435726,6 +435726,22 @@ let print_expr_type_clash env trace ppf = begin
435726435726
show_extra_help ppf env trace;
435727435727
end
435728435728

435729+
let reportJsFnArityMismatch ~arityA ~arityB ppf =
435730+
let extractArity s =
435731+
if Ext_string.starts_with s "arity" then
435732+
(* get the number part of e.g. arity12 *)
435733+
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
435734+
String.sub s 5 ((String.length s) - 5)
435735+
else
435736+
raise (Invalid_argument "Unrecognized arity type name.")
435737+
in
435738+
let firstNumber = extractArity arityA in
435739+
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
435740+
firstNumber
435741+
(if firstNumber = "1" then "argument" else "arguments")
435742+
(extractArity arityB)
435743+
435744+
435729435745
(* Pasted from typecore.ml. Needed for some cases in report_error below *)
435730435746
(* Records *)
435731435747
let label_of_kind kind =
@@ -435771,10 +435787,10 @@ let report_error env ppf = function
435771435787
) ->
435772435788
fprintf ppf "This function is a curried function where an uncurried function is expected"
435773435789
| Expr_type_clash (
435774-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
435775-
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
435776-
) when a <> b ->
435777-
fprintf ppf "This function has %s but was expected %s" a b
435790+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
435791+
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
435792+
) ->
435793+
reportJsFnArityMismatch ~arityA ~arityB ppf
435778435794
| Expr_type_clash (
435779435795
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
435780435796
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _

0 commit comments

Comments
 (0)