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..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" @@ -55,7 +61,7 @@ let init () = { pcd_name = {loc; txt = con_name}; pcd_args; - pcd_loc = _; + pcd_loc; pcd_res; } -> @@ -63,7 +69,8 @@ let init () = let pcd_args = match pcd_args with | Pcstr_tuple pcd_args -> pcd_args - | Pcstr_record _ -> assert false + | Pcstr_record _ -> + raise_unsupported_vaiant_record_arg pcd_loc in let little_con_name = Ext_string.uncapitalize_ascii con_name @@ -146,14 +153,15 @@ 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 _ -> + raise_unsupported_vaiant_record_arg pcd_loc in let arity = pcd_args |> List.length in let annotate_type =