Skip to content

Commit b38fde1

Browse files
authored
Merge branch 'master' into error-message-suggest-different-arity
2 parents 427f92c + fc69bf4 commit b38fde1

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Hint in error for string constants matching expected variant/polyvariant constructor. https://github.com/rescript-lang/rescript/pull/7711
3030
- Polish arity mismatch error message a bit. https://github.com/rescript-lang/rescript/pull/7709
3131
- Suggest related functions with the expected arity in errors when it makes sense. https://github.com/rescript-lang/rescript/pull/7712
32+
- Improve error when a constructor expects an inline record. https://github.com/rescript-lang/rescript/pull/7713
3233

3334
#### :house: Internal
3435

compiler/ml/typecore.ml

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ open Error_message_utils
2626

2727
type error =
2828
| Polymorphic_label of Longident.t
29-
| Constructor_arity_mismatch of Longident.t * int * int
29+
| Constructor_arity_mismatch of {
30+
name: Longident.t;
31+
constuctor: constructor_description;
32+
expected: int;
33+
provided: int;
34+
}
3035
| Label_mismatch of Longident.t * (type_expr * type_expr) list
3136
| Pattern_type_clash of (type_expr * type_expr) list
3237
| Or_pattern_type_clash of Ident.t * (type_expr * type_expr) list
@@ -1400,7 +1405,12 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
14001405
( loc,
14011406
!env,
14021407
Constructor_arity_mismatch
1403-
(lid.txt, constr.cstr_arity, List.length sargs) ));
1408+
{
1409+
name = lid.txt;
1410+
constuctor = constr;
1411+
expected = constr.cstr_arity;
1412+
provided = List.length sargs;
1413+
} ));
14041414
let ty_args, ty_res =
14051415
instance_constructor ~in_pattern:(env, get_newtype_level ()) constr
14061416
in
@@ -3758,7 +3768,12 @@ and type_construct ~context env loc lid sarg ty_expected attrs =
37583768
( loc,
37593769
env,
37603770
Constructor_arity_mismatch
3761-
(lid.txt, constr.cstr_arity, List.length sargs) ));
3771+
{
3772+
name = lid.txt;
3773+
constuctor = constr;
3774+
expected = constr.cstr_arity;
3775+
provided = List.length sargs;
3776+
} ));
37623777
let separate = Env.has_local_constraints env in
37633778
if separate then (
37643779
begin_def ();
@@ -4295,14 +4310,24 @@ let report_error env loc ppf error =
42954310
| Polymorphic_label lid ->
42964311
fprintf ppf "@[The record field %a is polymorphic.@ %s@]" longident lid
42974312
"You cannot instantiate it in a pattern."
4298-
| Constructor_arity_mismatch (lid, expected, provided) ->
4313+
| Constructor_arity_mismatch {name; constuctor; expected; provided} ->
42994314
(* modified *)
4300-
fprintf ppf
4301-
"@[This variant constructor, %a, expects %i %s; here, we've %sfound %i.@]"
4302-
longident lid expected
4303-
(if expected == 1 then "argument" else "arguments")
4304-
(if provided < expected then "only " else "")
4305-
provided
4315+
let is_inline_record = Option.is_some constuctor.cstr_inlined in
4316+
if is_inline_record && expected = 1 then
4317+
fprintf ppf
4318+
"@[This variant constructor @{<info>%a@} expects an inline record as \
4319+
payload%s.@]"
4320+
longident name
4321+
(if provided = 0 then ", but it's not being passed any arguments"
4322+
else "")
4323+
else
4324+
fprintf ppf
4325+
"@[This variant constructor @{<info>%a@} expects %i %s, but it's%s \
4326+
being passed %i.@]"
4327+
longident name expected
4328+
(if expected == 1 then "argument" else "arguments")
4329+
(if provided < expected then " only" else "")
4330+
provided
43064331
| Label_mismatch (lid, trace) ->
43074332
(* modified *)
43084333
super_report_unification_error ppf env trace

compiler/ml/typecore.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ val name_pattern : string -> Typedtree.case list -> Ident.t
5858

5959
type error =
6060
| Polymorphic_label of Longident.t
61-
| Constructor_arity_mismatch of Longident.t * int * int
61+
| Constructor_arity_mismatch of {
62+
name: Longident.t;
63+
constuctor: constructor_description;
64+
expected: int;
65+
provided: int;
66+
}
6267
| Label_mismatch of Longident.t * (type_expr * type_expr) list
6368
| Pattern_type_clash of (type_expr * type_expr) list
6469
| Or_pattern_type_clash of Ident.t * (type_expr * type_expr) list

tests/build_tests/super_errors/expected/primitives5.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
3 │ X(10)->ignore
88
4 │
99

10-
This variant constructor, X, expects 2 arguments; here, we've only found 1.
10+
This variant constructor X expects 2 arguments, but it's only being passed 1.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_constructor_expects_inline_record.res:2:9-11
4+
5+
1 │ type x = One({test: bool})
6+
2 │ let f = One
7+
3 │
8+
9+
This variant constructor One expects an inline record as payload, but it's not being passed any arguments.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type x = One({test: bool})
2+
let f = One

0 commit comments

Comments
 (0)