Skip to content

Improve error when using '@deriving(accessors)` on a variant with a record arg #6712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@deriving(accessors)
type t = Struct({a: int})
16 changes: 12 additions & 4 deletions jscomp/frontend/ast_derive_projector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -55,15 +61,16 @@ let init () =
{
pcd_name = {loc; txt = con_name};
pcd_args;
pcd_loc = _;
pcd_loc;
pcd_res;
}
->
(* TODO: add type annotations *)
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
Expand Down Expand Up @@ -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 _ ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this appear twice?
Are there 2 different ways to trigger the error?
In any case better refactor the error message printing and avoid duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, sorry I just copy pasted 👍🏼, the one is for structure gen the other is for signature gen. If it hits the case in one it will also in the other so you will only see one message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristianoc I've refactored it. See the following commit. Thanks!

raise_unsupported_vaiant_record_arg pcd_loc
in
let arity = pcd_args |> List.length in
let annotate_type =
Expand Down