From 87b38a6ec41449f9f1bba236bb68c174934bb9eb Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Wed, 3 Apr 2024 10:59:33 +0200 Subject: [PATCH 1/2] Improve error when using '@deriving(accessors)` on a variant with a record arg --- CHANGELOG.md | 4 ++++ .../DerivingAccessorsRecordParam.res.expected | 9 +++++++++ .../fixtures/DerivingAccessorsRecordParam.res | 2 ++ jscomp/frontend/ast_derive_projector.ml | 18 ++++++++++++++---- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 jscomp/build_tests/super_errors/expected/DerivingAccessorsRecordParam.res.expected create mode 100644 jscomp/build_tests/super_errors/fixtures/DerivingAccessorsRecordParam.res diff --git a/CHANGELOG.md b/CHANGELOG.md index a06b8f3fff..aaf913f35a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 11.1.0-rc.8 (Unreleased) +#### :bug: Bug Fix + +- Improve error when using '@deriving(accessors)' on a variant with record arguments. https://github.com/rescript-lang/rescript-compiler/pull/6712 + # 11.1.0-rc.7 #### :bug: Bug Fix diff --git a/jscomp/build_tests/super_errors/expected/DerivingAccessorsRecordParam.res.expected b/jscomp/build_tests/super_errors/expected/DerivingAccessorsRecordParam.res.expected new file mode 100644 index 0000000000..2ac313b4af --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/DerivingAccessorsRecordParam.res.expected @@ -0,0 +1,9 @@ + + We've found a bug for you! + /.../fixtures/DerivingAccessorsRecordParam.res:2:10-25 + + 1 │ @deriving(accessors) + 2 │ type t = Struct({a: int}) + 3 │ + + @deriving(accessors) from a variant record argument is unsupported. Either define the record type separately from the variant type or use a positional argument. \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/DerivingAccessorsRecordParam.res b/jscomp/build_tests/super_errors/fixtures/DerivingAccessorsRecordParam.res new file mode 100644 index 0000000000..c86aa30c79 --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/DerivingAccessorsRecordParam.res @@ -0,0 +1,2 @@ +@deriving(accessors) +type t = Struct({a: int}) diff --git a/jscomp/frontend/ast_derive_projector.ml b/jscomp/frontend/ast_derive_projector.ml index 352aaeb177..da6b00c444 100644 --- a/jscomp/frontend/ast_derive_projector.ml +++ b/jscomp/frontend/ast_derive_projector.ml @@ -55,7 +55,7 @@ let init () = { pcd_name = {loc; txt = con_name}; pcd_args; - pcd_loc = _; + pcd_loc; pcd_res; } -> @@ -63,7 +63,12 @@ let init () = let pcd_args = match pcd_args with | Pcstr_tuple pcd_args -> pcd_args - | Pcstr_record _ -> assert false + | Pcstr_record _ -> + Location.raise_errorf ~loc:pcd_loc + "@deriving(accessors) from a variant record argument \ + is unsupported. Either define the record type \ + separately from the variant type or use a \ + positional argument." in let little_con_name = Ext_string.uncapitalize_ascii con_name @@ -146,14 +151,19 @@ let init () = { pcd_name = {loc; txt = con_name}; pcd_args; - pcd_loc = _; + pcd_loc; pcd_res; } -> let pcd_args = match pcd_args with | Pcstr_tuple pcd_args -> pcd_args - | Pcstr_record _ -> assert false + | Pcstr_record _ -> + Location.raise_errorf ~loc:pcd_loc + "@deriving(accessors) from a variant record argument \ + is unsupported. Either define the record type \ + separately from the variant type or use a \ + positional argument." in let arity = pcd_args |> List.length in let annotate_type = From 425515cf62a3df672d4359b7ac17e3f666f328b2 Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Wed, 3 Apr 2024 20:46:28 +0200 Subject: [PATCH 2/2] Refactor raise_unsuppored_variant_record_arg function --- jscomp/frontend/ast_derive_projector.ml | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/jscomp/frontend/ast_derive_projector.ml b/jscomp/frontend/ast_derive_projector.ml index da6b00c444..48ff7d6f85 100644 --- a/jscomp/frontend/ast_derive_projector.ml +++ b/jscomp/frontend/ast_derive_projector.ml @@ -4,6 +4,12 @@ let invalid_config (config : Parsetree.expression) = Location.raise_errorf ~loc:config.pexp_loc "such configuration is not supported" +let raise_unsupported_vaiant_record_arg loc = + Location.raise_errorf ~loc + "@deriving(accessors) from a variant record argument is unsupported. \ + Either define the record type separately from the variant type or use a \ + positional argument." + type tdcls = Parsetree.type_declaration list let derivingName = "accessors" @@ -64,11 +70,7 @@ let init () = match pcd_args with | Pcstr_tuple pcd_args -> pcd_args | Pcstr_record _ -> - Location.raise_errorf ~loc:pcd_loc - "@deriving(accessors) from a variant record argument \ - is unsupported. Either define the record type \ - separately from the variant type or use a \ - positional argument." + raise_unsupported_vaiant_record_arg pcd_loc in let little_con_name = Ext_string.uncapitalize_ascii con_name @@ -159,11 +161,7 @@ let init () = match pcd_args with | Pcstr_tuple pcd_args -> pcd_args | Pcstr_record _ -> - Location.raise_errorf ~loc:pcd_loc - "@deriving(accessors) from a variant record argument \ - is unsupported. Either define the record type \ - separately from the variant type or use a \ - positional argument." + raise_unsupported_vaiant_record_arg pcd_loc in let arity = pcd_args |> List.length in let annotate_type =