Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
2 │ let makeVar = (. ~f, ()) => 34;
3 │ let makeVariables = makeVar(. ~f=f => f);

This function has arity2 but was expected arity1
This function expected 2 arguments, but got 1
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
2 │ let makeVar = (. f, ()) => 34;
3 │ let makeVariables = makeVar(. 1,2,3);

This function has arity2 but was expected arity3
This function expected 2 arguments, but got 3
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

1 │ Belt.Array.mapU([||], (. a, b) => 1);

The field Js.Fn.I2 belongs to the record type Js.Fn.arity2
but a field was expected belonging to the record type Js.Fn.arity1
This function expected 1 argument, but got 2
38 changes: 34 additions & 4 deletions jscomp/super_errors/super_typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ let print_expr_type_clash env trace ppf = begin
show_extra_help ppf env trace;
end

let reportJsFnArityMismatch ~arityA ~arityB ppf =
let extractArity s =
if Ext_string.starts_with s "arity" then
(* get the number part of e.g. arity12 *)
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
String.sub s 5 ((String.length s) - 5)
else
raise (Invalid_argument "Unrecognized arity type name.")
in
let firstNumber = extractArity arityA in
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
firstNumber
(if firstNumber = "1" then "argument" else "arguments")
(extractArity arityB)


(* Pasted from typecore.ml. Needed for some cases in report_error below *)
(* Records *)
let label_of_kind kind =
Expand Down Expand Up @@ -169,10 +185,10 @@ let report_error env ppf = function
) ->
fprintf ppf "This function is a curried function where an uncurried function is expected"
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
) when a <> b ->
fprintf ppf "This function has %s but was expected %s" a b
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
) ->
reportJsFnArityMismatch ~arityA ~arityB ppf
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _
Expand Down Expand Up @@ -276,6 +292,20 @@ let report_error env ppf = function
name (*kind*) Printtyp.path p;
end;
spellcheck ppf name valid_names;
| Name_type_mismatch (
"record",
Ldot (Ldot ((Lident "Js"), "Fn"), _arityFieldName),
(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityA, _))
),
[(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityB, _))
)]
) ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is a bit unclear to me. Here's the original: https://github.com/rescript-lang/ocaml/blob/bc2625fe99e2e01700d1ec0fea8dcc0672c94564/typing/typecore.ml#L5011

tp is a tuple of Path.t, and in my test here, both items are the same. tpl is a list of such tuple, and in my test here, a list with a single item of a tuple.

From what I understand, the second item of the tuple is the expanded path... right? So I should be matching on that and ignoring the first item?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record:

fun[@bs] a0 a1 -> a0 + a1  

is expanded into

{Js.Fn.I2 = fun a0 a1 -> a0 + a1}

it does not come with an annotation like below:

({Js.Fn.I2 = fun a0 a1 -> a0 + a1} : _ Js.Fn.arity2)

With such annotation, the error message is better.
However, this is intentional, since adding such annotation will make this

data -> mapU (fun [@bs] x -> x.label) 

not work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To answer your question, tp is its type and expanded type (e.g, type aliases)
The changes are mostly correct, I will merge it and do some tweak later

(* modified *)
reportJsFnArityMismatch ~arityA ~arityB ppf
| anythingElse ->
Typecore.super_report_error_no_wrap_printing_env env ppf anythingElse

Expand Down
38 changes: 34 additions & 4 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -409843,6 +409843,22 @@ let print_expr_type_clash env trace ppf = begin
show_extra_help ppf env trace;
end

let reportJsFnArityMismatch ~arityA ~arityB ppf =
let extractArity s =
if Ext_string.starts_with s "arity" then
(* get the number part of e.g. arity12 *)
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
String.sub s 5 ((String.length s) - 5)
else
raise (Invalid_argument "Unrecognized arity type name.")
in
let firstNumber = extractArity arityA in
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
firstNumber
(if firstNumber = "1" then "argument" else "arguments")
(extractArity arityB)


(* Pasted from typecore.ml. Needed for some cases in report_error below *)
(* Records *)
let label_of_kind kind =
Expand Down Expand Up @@ -409888,10 +409904,10 @@ let report_error env ppf = function
) ->
fprintf ppf "This function is a curried function where an uncurried function is expected"
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
) when a <> b ->
fprintf ppf "This function has %s but was expected %s" a b
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
) ->
reportJsFnArityMismatch ~arityA ~arityB ppf
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _
Expand Down Expand Up @@ -409995,6 +410011,20 @@ let report_error env ppf = function
name (*kind*) Printtyp.path p;
end;
spellcheck ppf name valid_names;
| Name_type_mismatch (
"record",
Ldot (Ldot ((Lident "Js"), "Fn"), _arityFieldName),
(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityA, _))
),
[(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityB, _))
)]
) ->
(* modified *)
reportJsFnArityMismatch ~arityA ~arityB ppf
| anythingElse ->
Typecore.super_report_error_no_wrap_printing_env env ppf anythingElse

Expand Down
38 changes: 34 additions & 4 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -435726,6 +435726,22 @@ let print_expr_type_clash env trace ppf = begin
show_extra_help ppf env trace;
end

let reportJsFnArityMismatch ~arityA ~arityB ppf =
let extractArity s =
if Ext_string.starts_with s "arity" then
(* get the number part of e.g. arity12 *)
(* assumption: the module Js.Fn only contains types from arity0 to arity22 *)
String.sub s 5 ((String.length s) - 5)
else
raise (Invalid_argument "Unrecognized arity type name.")
in
let firstNumber = extractArity arityA in
fprintf ppf "This function expected @{<info>%s@} %s, but got @{<error>%s@}"
firstNumber
(if firstNumber = "1" then "argument" else "arguments")
(extractArity arityB)


(* Pasted from typecore.ml. Needed for some cases in report_error below *)
(* Records *)
let label_of_kind kind =
Expand Down Expand Up @@ -435771,10 +435787,10 @@ let report_error env ppf = function
) ->
fprintf ppf "This function is a curried function where an uncurried function is expected"
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),b,_),_,_)}) :: _
) when a <> b ->
fprintf ppf "This function has %s but was expected %s" a b
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityA,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js"},"Fn",_),arityB,_),_,_)}) :: _
) ->
reportJsFnArityMismatch ~arityA ~arityB ppf
| Expr_type_clash (
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),a,_),_,_)}) ::
(_, {desc = Tconstr (Pdot (Pdot(Pident {name = "Js_OO"},"Meth",_),b,_),_,_)}) :: _
Expand Down Expand Up @@ -435878,6 +435894,20 @@ let report_error env ppf = function
name (*kind*) Printtyp.path p;
end;
spellcheck ppf name valid_names;
| Name_type_mismatch (
"record",
Ldot (Ldot ((Lident "Js"), "Fn"), _arityFieldName),
(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityA, _))
),
[(
_,
(Pdot (Pdot ((Pident {name = "Js"}), "Fn", _), arityB, _))
)]
) ->
(* modified *)
reportJsFnArityMismatch ~arityA ~arityB ppf
| anythingElse ->
Typecore.super_report_error_no_wrap_printing_env env ppf anythingElse

Expand Down