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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Better error message if platform binaries package is not found. https://github.com/rescript-lang/rescript/pull/7698
- Hint in error for string constants matching expected variant/polyvariant constructor. https://github.com/rescript-lang/rescript/pull/7711
- Polish arity mismatch error message a bit. https://github.com/rescript-lang/rescript/pull/7709
- Improve error when a constructor expects an inline record. https://github.com/rescript-lang/rescript/pull/7713

#### :house: Internal

Expand Down
45 changes: 35 additions & 10 deletions compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ open Error_message_utils

type error =
| Polymorphic_label of Longident.t
| Constructor_arity_mismatch of Longident.t * int * int
| Constructor_arity_mismatch of {
name: Longident.t;
constuctor: constructor_description;
expected: int;
provided: int;
}
| Label_mismatch of Longident.t * (type_expr * type_expr) list
| Pattern_type_clash of (type_expr * type_expr) list
| Or_pattern_type_clash of Ident.t * (type_expr * type_expr) list
Expand Down Expand Up @@ -1395,7 +1400,12 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
( loc,
!env,
Constructor_arity_mismatch
(lid.txt, constr.cstr_arity, List.length sargs) ));
{
name = lid.txt;
constuctor = constr;
expected = constr.cstr_arity;
provided = List.length sargs;
} ));
let ty_args, ty_res =
instance_constructor ~in_pattern:(env, get_newtype_level ()) constr
in
Expand Down Expand Up @@ -3742,7 +3752,12 @@ and type_construct ~context env loc lid sarg ty_expected attrs =
( loc,
env,
Constructor_arity_mismatch
(lid.txt, constr.cstr_arity, List.length sargs) ));
{
name = lid.txt;
constuctor = constr;
expected = constr.cstr_arity;
provided = List.length sargs;
} ));
let separate = Env.has_local_constraints env in
if separate then (
begin_def ();
Expand Down Expand Up @@ -4245,14 +4260,24 @@ let report_error env loc ppf error =
| Polymorphic_label lid ->
fprintf ppf "@[The record field %a is polymorphic.@ %s@]" longident lid
"You cannot instantiate it in a pattern."
| Constructor_arity_mismatch (lid, expected, provided) ->
| Constructor_arity_mismatch {name; constuctor; expected; provided} ->
(* modified *)
fprintf ppf
"@[This variant constructor, %a, expects %i %s; here, we've %sfound %i.@]"
longident lid expected
(if expected == 1 then "argument" else "arguments")
(if provided < expected then "only " else "")
provided
let is_inline_record = Option.is_some constuctor.cstr_inlined in
if is_inline_record && expected = 1 then
fprintf ppf
"@[This variant constructor @{<info>%a@} expects an inline record as \
payload%s.@]"
longident name
(if provided = 0 then ", but it's not being passed any arguments"
else "")
else
fprintf ppf
"@[This variant constructor @{<info>%a@} expects %i %s, but it's%s \
being passed %i.@]"
longident name expected
(if expected == 1 then "argument" else "arguments")
(if provided < expected then " only" else "")
provided
| Label_mismatch (lid, trace) ->
(* modified *)
super_report_unification_error ppf env trace
Expand Down
7 changes: 6 additions & 1 deletion compiler/ml/typecore.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ val name_pattern : string -> Typedtree.case list -> Ident.t

type error =
| Polymorphic_label of Longident.t
| Constructor_arity_mismatch of Longident.t * int * int
| Constructor_arity_mismatch of {
name: Longident.t;
constuctor: constructor_description;
expected: int;
provided: int;
}
| Label_mismatch of Longident.t * (type_expr * type_expr) list
| Pattern_type_clash of (type_expr * type_expr) list
| Or_pattern_type_clash of Ident.t * (type_expr * type_expr) list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
3 │ X(10)->ignore
4 │

This variant constructor, X, expects 2 arguments; here, we've only found 1.
This variant constructor X expects 2 arguments, but it's only being passed 1.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/variant_constructor_expects_inline_record.res:2:9-11

1 │ type x = One({test: bool})
2 │ let f = One
3 │

This variant constructor One expects an inline record as payload, but it's not being passed any arguments.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type x = One({test: bool})
let f = One