Skip to content

Commit a5be80d

Browse files
authored
Merge pull request #594 from janestreet/ppxlib.end
Add `@@@ppxlib.inline.end`, deprecate `@@@deriving.end`
2 parents e0668a8 + 25b0486 commit a5be80d

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ unreleased
2323

2424
- Expose `Ppxlib.Location.Error.t = Astlib.Location.Error.t` (#593, @ceastlund)
2525

26+
- Add `@@@ppxlib.inline.end`, deprecate `@@@deriving.end`. (#594, @ceastlund)
27+
2628
0.36.1 (2025-07-10)
2729
-------------------
2830

src/code_matcher.ml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ open! Import
33
module Format = Stdlib.Format
44
module Filename = Stdlib.Filename
55

6-
(* TODO: make the "deriving." depend on the matching attribute name. *)
6+
let allow_deriving_end = ref true
7+
8+
(* TODO: make the "deriving." or other prefix depend on the matching attribute name. *)
79
let end_marker_sig =
8-
Attribute.Floating.declare "deriving.end" Signature_item
10+
Attribute.Floating.declare "ppxlib.inline.end" Signature_item
911
Ast_pattern.(pstr nil)
1012
()
1113

1214
let end_marker_str =
15+
Attribute.Floating.declare "ppxlib.inline.end" Structure_item
16+
Ast_pattern.(pstr nil)
17+
()
18+
19+
let deprecated_end_marker_sig =
20+
Attribute.Floating.declare "deriving.end" Signature_item
21+
Ast_pattern.(pstr nil)
22+
()
23+
24+
let deprecated_end_marker_str =
1325
Attribute.Floating.declare "deriving.end" Structure_item
1426
Ast_pattern.(pstr nil)
1527
()
@@ -24,6 +36,7 @@ module Make (M : sig
2436

2537
val get_loc : t -> Location.t
2638
val end_marker : (t, unit) Attribute.Floating.t
39+
val deprecated_end_marker : (t, unit) Attribute.Floating.t
2740

2841
module Transform (T : T1) : sig
2942
val apply :
@@ -51,10 +64,27 @@ struct
5164
[] )
5265
| x :: l -> (
5366
match Attribute.Floating.convert_res [ M.end_marker ] x with
54-
| Ok None -> loop (x :: acc) l
5567
| Ok (Some ()) -> Ok (List.rev acc, (M.get_loc x).loc_start)
5668
| Error e -> Error e
57-
| exception Failure _ -> loop (x :: acc) l)
69+
| (exception Failure _) | Ok None -> (
70+
match
71+
Attribute.Floating.convert_res [ M.deprecated_end_marker ] x
72+
with
73+
| Ok (Some ()) ->
74+
if !allow_deriving_end then
75+
Ok (List.rev acc, (M.get_loc x).loc_start)
76+
else
77+
Error
78+
( Location.Error.createf ~loc:(M.get_loc x)
79+
"ppxlib: [@@@@@@%s] is deprecated, please use \
80+
[@@@@@@%s]. If you need the deprecated attribute \
81+
temporarily, pass [-allow-deriving-end] to the ppx \
82+
driver)."
83+
(Attribute.Floating.name M.deprecated_end_marker)
84+
(Attribute.Floating.name M.end_marker),
85+
[] )
86+
| Error e -> Error e
87+
| (exception Failure _) | Ok None -> loop (x :: acc) l))
5888
in
5989
loop [] l
6090

@@ -170,6 +200,7 @@ module Str = Make (struct
170200

171201
let get_loc x = x.pstr_loc
172202
let end_marker = end_marker_str
203+
let deprecated_end_marker = deprecated_end_marker_str
173204

174205
module Transform (T : T1) = struct
175206
let apply o = o#structure_item
@@ -188,6 +219,7 @@ module Sig = Make (struct
188219

189220
let get_loc x = x.psig_loc
190221
let end_marker = end_marker_sig
222+
let deprecated_end_marker = deprecated_end_marker_sig
191223

192224
module Transform (T : T1) = struct
193225
let apply o = o#signature_item

src/code_matcher.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ val match_signature :
3939
signature ->
4040
unit
4141
(** Same for signatures *)
42+
43+
val allow_deriving_end : bool ref
44+
(** The legacy attribute [@@@deriving.end] is disabled by default, and
45+
superseded by [@@@ppxlib.inline.end]. It can be enabled by setting
46+
[allow_deriving_end := true]. See [-allow-deriving-end] in [Driver]. *)

src/driver.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,9 @@ let shared_args =
13391339
( "-raise-embedded-errors",
13401340
Arg.Set raise_embedded_errors_flag,
13411341
" Raise the first embedded error found in the processed AST" );
1342+
( "-allow-deriving-end",
1343+
Arg.Bool (( := ) Code_matcher.allow_deriving_end),
1344+
" Whether to allow [@@@deriving.end], which will soon be deprecated." );
13421345
]
13431346

13441347
let () =

test/deriving/inline/example/ppx_deriving_example.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ include struct
1313
[%foo]
1414
end [@@ocaml.doc "@inline"]
1515

16-
[@@@deriving.end]
16+
[@@@inline.end]

test/driver/run_as_ppx_rewriter/run.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The only possible usage is [extra_args] <infile> <outfile>...
6464
-cookie NAME=EXPR Set the cookie NAME to EXPR
6565
--cookie Same as -cookie
6666
-raise-embedded-errors Raise the first embedded error found in the processed AST
67+
-allow-deriving-end Whether to allow [@@@deriving.end], which will soon be deprecated.
6768
-help Display this list of options
6869
--help Display this list of options
6970
[2]
@@ -86,5 +87,6 @@ The only exception is consulting help
8687
-cookie NAME=EXPR Set the cookie NAME to EXPR
8788
--cookie Same as -cookie
8889
-raise-embedded-errors Raise the first embedded error found in the processed AST
90+
-allow-deriving-end Whether to allow [@@@deriving.end], which will soon be deprecated.
8991
-help Display this list of options
9092
--help Display this list of options

test/error_embedding/run.t

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,28 @@ Flag `-raise-embedded-errors` raises the first embedded error in the AST.
8787
^^^^^^^^^^^
8888
Error: error 1
8989
[1]
90+
91+
Undeprecated `[@@@deriving.end]` runs just fine.
92+
93+
$ echo "type t [@@deriving_inline a_string] [@@@deriving.end]" > embedded_error.ml
94+
$ ./deriver.exe embedded_error.ml -diff-cmd "diff -u --label source --label derived"
95+
type t[@@deriving_inline a_string]
96+
[@@@deriving.end ]
97+
--- source
98+
+++ derived
99+
@@ -1 +1,4 @@
100+
-type t [@@deriving_inline a_string] [@@@deriving.end]
101+
+type t [@@deriving_inline a_string]
102+
+let _ = fun (_ : t) -> ()
103+
+let _ = "derived_string"
104+
+[@@@deriving.end]
105+
[1]
106+
107+
Deprecated `[@@@deriving.end]` produces an error.
108+
109+
$ echo "type t [@@deriving_inline a_string] [@@@deriving.end]" > embedded_error.ml
110+
$ ./deriver.exe embedded_error.ml -allow-deriving-end false
111+
[%%ocaml.error
112+
"ppxlib: [@@@deriving.end] is deprecated, please use [@@@ppxlib.inline.end]. If you need the deprecated attribute temporarily, pass [-allow-deriving-end] to the ppx driver)."]
113+
type t[@@deriving_inline a_string]
114+
[@@@deriving.end ]

0 commit comments

Comments
 (0)