Skip to content

Commit 4a38f30

Browse files
authored
Improve error when using '@deriving(accessors)` on a variant with a record arg (rescript-lang#6712)
* Improve error when using '@deriving(accessors)` on a variant with a record arg * Refactor raise_unsuppored_variant_record_arg function
1 parent 77d234f commit 4a38f30

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.1.0-rc.8 (Unreleased)
1414

15+
#### :bug: Bug Fix
16+
17+
- Improve error when using '@deriving(accessors)' on a variant with record arguments. https://github.com/rescript-lang/rescript-compiler/pull/6712
18+
1519
# 11.1.0-rc.7
1620

1721
#### :bug: Bug Fix
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/DerivingAccessorsRecordParam.res:2:10-25
4+
5+
1 │ @deriving(accessors)
6+
2 │ type t = Struct({a: int})
7+
3 │
8+
9+
@deriving(accessors) from a variant record argument is unsupported. Either define the record type separately from the variant type or use a positional argument.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@deriving(accessors)
2+
type t = Struct({a: int})

jscomp/frontend/ast_derive_projector.ml

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ let invalid_config (config : Parsetree.expression) =
44
Location.raise_errorf ~loc:config.pexp_loc
55
"such configuration is not supported"
66

7+
let raise_unsupported_vaiant_record_arg loc =
8+
Location.raise_errorf ~loc
9+
"@deriving(accessors) from a variant record argument is unsupported. \
10+
Either define the record type separately from the variant type or use a \
11+
positional argument."
12+
713
type tdcls = Parsetree.type_declaration list
814

915
let derivingName = "accessors"
@@ -55,15 +61,16 @@ let init () =
5561
{
5662
pcd_name = {loc; txt = con_name};
5763
pcd_args;
58-
pcd_loc = _;
64+
pcd_loc;
5965
pcd_res;
6066
}
6167
->
6268
(* TODO: add type annotations *)
6369
let pcd_args =
6470
match pcd_args with
6571
| Pcstr_tuple pcd_args -> pcd_args
66-
| Pcstr_record _ -> assert false
72+
| Pcstr_record _ ->
73+
raise_unsupported_vaiant_record_arg pcd_loc
6774
in
6875
let little_con_name =
6976
Ext_string.uncapitalize_ascii con_name
@@ -146,14 +153,15 @@ let init () =
146153
{
147154
pcd_name = {loc; txt = con_name};
148155
pcd_args;
149-
pcd_loc = _;
156+
pcd_loc;
150157
pcd_res;
151158
}
152159
->
153160
let pcd_args =
154161
match pcd_args with
155162
| Pcstr_tuple pcd_args -> pcd_args
156-
| Pcstr_record _ -> assert false
163+
| Pcstr_record _ ->
164+
raise_unsupported_vaiant_record_arg pcd_loc
157165
in
158166
let arity = pcd_args |> List.length in
159167
let annotate_type =

0 commit comments

Comments
 (0)