Skip to content

Commit

Permalink
Raise an error when type nonrec is encountered
Browse files Browse the repository at this point in the history
This feature has two different implementations in the AST:

- in 4.02.3, it is an annotation on a type,
- in >= 4.03.0 it is straight in the constructor.

Note that this can break existing programs; however relying on
`ppx_deriving` to do the right thing seems dangerous.
Maybe an escape hatch could be added so that the recursive version is
derived nonetheless.

It is an intermediate solution to #116. Solving it is tricky because it
would be necessary to expose this to the plugins, breaking the API. It
is probably easier to do so once 4.02.3 support is dropped, since the
per-type semantics are difficult to handle.
  • Loading branch information
emillon authored and whitequark committed Dec 21, 2016
1 parent 214fa63 commit 5f7f280
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ppx_deriving.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,21 @@ let module_from_input_name () =
| "//toplevel//" -> []
| filename -> [String.capitalize (Filename.(basename (chop_suffix filename ".ml")))]

let pstr_desc_rec_flag pstr =
match pstr with
| Pstr_type(rec_flag, typ_decls) ->
#if OCAML_VERSION < (4, 03, 0)
begin
if List.exists (fun ty -> has_attr "nonrec" ty.ptype_attributes) typ_decls then
Nonrecursive
else
Recursive
end
#else
rec_flag
#endif
| _ -> assert false

let mapper =
let module_nesting = ref [] in
let with_module name f =
Expand Down Expand Up @@ -496,6 +511,10 @@ let mapper =
in
let structure mapper items =
match items with
| { pstr_desc = Pstr_type(_, typ_decls) as pstr_desc ; pstr_loc } :: rest when
List.exists (fun ty -> has_attr "deriving" ty.ptype_attributes) typ_decls
&& pstr_desc_rec_flag pstr_desc = Nonrecursive ->
raise_errorf ~loc:pstr_loc "The nonrec flag is not supported by ppx_deriving"
| { pstr_desc = Pstr_type(_, typ_decls); pstr_loc } as item :: rest when
List.exists (fun ty -> has_attr "deriving" ty.ptype_attributes) typ_decls ->
let derived =
Expand Down

0 comments on commit 5f7f280

Please sign in to comment.