Skip to content

Commit a7a8fc2

Browse files
committed
Get attr_replace test running
Signed-off-by: Jack Rickard <[email protected]>
1 parent 412d532 commit a7a8fc2

File tree

7 files changed

+78
-122
lines changed

7 files changed

+78
-122
lines changed

src/context_free.ml

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,25 @@ let rec handle_attr_replace_fix context attrs item base_ctxt =
527527
| Some item -> handle_attr_replace_fix context attrs item base_ctxt
528528
| None -> return item
529529

530+
let rec handle_attr_replace_str attrs item base_ctxt =
531+
(match item.pstr_desc with
532+
| Pstr_extension _ ->
533+
handle_attr_replace_once AC.Pstr_extension attrs item base_ctxt
534+
| Pstr_eval _ -> handle_attr_replace_once AC.Pstr_eval attrs item base_ctxt
535+
| _ -> return None)
536+
>>= function
537+
| Some item -> handle_attr_replace_str attrs item base_ctxt
538+
| None -> return item
539+
540+
let rec handle_attr_replace_sig attrs item base_ctxt =
541+
(match item.psig_desc with
542+
| Psig_extension _ ->
543+
handle_attr_replace_once AC.Psig_extension attrs item base_ctxt
544+
| _ -> return None)
545+
>>= function
546+
| Some item -> handle_attr_replace_sig attrs item base_ctxt
547+
| None -> return item
548+
530549
(* Returns the code generated by attribute handlers. We don't remove these attributes, as
531550
another pass might interpret them later. For instance both ppx_deriving and
532551
ppxlib_deriving interprets [@@deriving] attributes.
@@ -997,20 +1016,8 @@ class map_top_down ?(expect_mismatch_handler = Expect_mismatch_handler.nop)
9971016
match st with
9981017
| [] -> return []
9991018
| item :: rest -> (
1000-
let rec fix item =
1001-
(match item.pstr_desc with
1002-
| Pstr_extension _ ->
1003-
handle_attr_replace_once AC.Pstr_extension
1004-
attr_structure_item_replaces item base_ctxt
1005-
| Pstr_eval _ ->
1006-
handle_attr_replace_once AC.Pstr_eval
1007-
attr_structure_item_replaces item base_ctxt
1008-
| _ -> return None)
1009-
>>= function
1010-
| Some item -> fix item
1011-
| None -> return item
1012-
in
1013-
fix item >>= fun item ->
1019+
handle_attr_replace_str attr_structure_item_replaces item base_ctxt
1020+
>>= fun item ->
10141021
let loc = item.pstr_loc in
10151022
match item.pstr_desc with
10161023
| Pstr_extension (ext, attrs) -> (
@@ -1142,17 +1149,8 @@ class map_top_down ?(expect_mismatch_handler = Expect_mismatch_handler.nop)
11421149
match sg with
11431150
| [] -> return []
11441151
| item :: rest -> (
1145-
let rec fix item =
1146-
(match item.psig_desc with
1147-
| Psig_extension _ ->
1148-
handle_attr_replace_once AC.Psig_extension
1149-
attr_signature_item_replaces item base_ctxt
1150-
| _ -> return None)
1151-
>>= function
1152-
| Some item -> fix item
1153-
| None -> return item
1154-
in
1155-
fix item >>= fun item ->
1152+
handle_attr_replace_sig attr_signature_item_replaces item base_ctxt
1153+
>>= fun item ->
11561154
let loc = item.psig_loc in
11571155
match item.psig_desc with
11581156
| Psig_extension (ext, attrs) -> (

test/attr_replace/dune

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
(library
2-
(name ppxlib_test_attr_replace)
3-
(preprocess
4-
(pps ppxlib_test_attr_replace_ppx)))
1+
(rule
2+
(deps test.ml)
3+
(targets test.output.ml)
4+
(action
5+
(with-stdout-to
6+
%{targets}
7+
(run ./ppx/ppx_attr_replace.exe %{deps}))))
8+
9+
(rule
10+
(alias runtest)
11+
(action
12+
(diff test.expected.ml test.output.ml)))

test/attr_replace/ppx/dune

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
(library
2-
(name ppxlib_test_attr_replace_ppx)
3-
(kind ppx_rewriter)
4-
(libraries ppxlib))
1+
(executable
2+
(name ppx_attr_replace)
3+
(modules ppx_attr_replace)
4+
(libraries ppxlib)
5+
(preprocess
6+
(pps ppxlib.metaquot)))

test/attr_replace/ppx/ppxlib_test_attr_replace_ppx.ml renamed to test/attr_replace/ppx/ppx_attr_replace.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,5 @@ let () =
300300
let loc = { loc with loc_ghost = true } in
301301
[%stri let foo = ()]));
302302
]
303+
304+
let () = Driver.standalone ()

test/attr_replace/ppx/ppxlib_test_attr_replace_ppx.mli

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/attr_replace/test.expected.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class c__suffix = object end
2+
class type ct__suffix = object end
3+
type t__suffix = unit
4+
let foo__suffix () = ()
5+
module M__suffix = struct end
6+
module type S__suffix = sig end
7+
let prefix_foo_suffix = ()
8+
class class_ = c__suffix
9+
class class_field =
10+
object val foo__suffix = ()[@@alert "-1"][@@alert "-2"] end
11+
class type class_type = ct__suffix
12+
class type class_type_field =
13+
object val x__suffix : int[@@alert "-1"][@@alert "-2"] end
14+
let _ = ()
15+
let _e = ()
16+
let _ = ((foo__suffix)[@alert "-1"][@alert "-2"])
17+
let _ = ((foo__suffix)[@alert "-1"][@alert "-2"]) ()
18+
include ((M__suffix)[@alert "-1"][@alert "-2"])
19+
module F : ((S__suffix)[@alert "-1"][@alert "-2"]) = struct end
20+
let _ = match () with | a__suffix -> ignore a__suffix
21+
module type S = sig val foo : unit end
22+
let foo = ()
23+
module _ = struct ;;"" end
24+
let _ = ((prefix_foo_suffix)[@alert "-1"][@alert "-2"][@alert "-3"])

test/attr_replace/test.ml

Lines changed: 11 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,124 +12,47 @@ module type S__suffix = sig end
1212

1313
let prefix_foo_suffix = ()
1414

15-
[@@@expand_inline class class_ = c [@test.clx "suffix"]]
15+
class class_ = c [@test.clx "suffix"]
1616

17-
class class_ = c__suffix
18-
19-
[@@@end]
20-
21-
[@@@expand_inline
2217
class class_field =
2318
object
2419
val foo = () [@@alert "-1"] [@@test.clf "suffix"] [@@alert "-2"]
25-
end]
26-
27-
class class_field =
28-
object
29-
val foo__suffix = () [@@alert "-1"] [@@alert "-2"]
3020
end
3121

32-
[@@@end]
33-
[@@@expand_inline class type class_type = ct[@test.clt "suffix"]]
22+
class type class_type = ct[@test.clt "suffix"]
3423

35-
class type class_type = ct__suffix
36-
37-
[@@@end]
38-
39-
[@@@expand_inline
4024
class type class_type_field = object
4125
val x : int [@@alert "-1"] [@@test.ctf "suffix"] [@@alert "-2"]
42-
end]
43-
44-
class type class_type_field = object
45-
val x__suffix : int [@@alert "-1"] [@@alert "-2"]
4626
end
4727

48-
[@@@end]
49-
50-
[@@@expand_inline
51-
let _ : (t[@alert "-1"] [@test.typ "suffix"] [@alert "-2"]) = ()]
52-
53-
let _ : (t__suffix[@alert "-1"] [@alert "-2"]) = ()
54-
55-
[@@@end]
56-
[@@@expand_inline let _e : (t[@test.typ "suffix"]) = ()]
57-
58-
let _e : t__suffix = ()
59-
60-
[@@@end]
61-
[@@@expand_inline let _ = foo [@alert "-1"] [@test.exp "suffix"] [@alert "-2"]]
62-
63-
let _ = foo__suffix [@alert "-1"] [@alert "-2"]
64-
65-
[@@@end]
28+
let _ : (t[@alert "-1"] [@test.typ "suffix"] [@alert "-2"]) = ()
29+
let _e : (t[@test.typ "suffix"]) = ()
30+
let _ = foo [@alert "-1"] [@test.exp "suffix"] [@alert "-2"]
6631

6732
(* Explicit test for the ident in a function application because it acts differently due
6833
to "special functions". *)
69-
[@@@expand_inline
70-
let _ = (foo [@alert "-1"] [@test.exp "suffix"] [@alert "-2"]) ()]
71-
72-
let _ = (foo__suffix [@alert "-1"] [@alert "-2"]) ()
7334

74-
[@@@end]
35+
let _ = (foo [@alert "-1"] [@test.exp "suffix"] [@alert "-2"]) ()
7536

76-
[@@@expand_inline
77-
include M [@alert "-1"] [@test.mod_exp "suffix"] [@alert "-2"]]
37+
include M [@alert "-1"] [@test.mod_exp "suffix"] [@alert "-2"]
38+
module F : S [@alert "-1"] [@test.mod_typ "suffix"] [@alert "-2"] = struct end
7839

79-
include M__suffix [@alert "-1"] [@alert "-2"]
40+
let _ = match () with (a [@test.pat "suffix"]) -> ignore a__suffix
8041

81-
[@@@end]
82-
83-
[@@@expand_inline
84-
module F : S [@alert "-1"] [@test.mod_typ "suffix"] [@alert "-2"] = struct end]
85-
86-
module F : S__suffix [@alert "-1"] [@alert "-2"] = struct end
87-
88-
[@@@end]
89-
90-
[@@@expand_inline
91-
let _ = match () with (a [@test.pat "suffix"]) -> ignore a__suffix]
92-
93-
let _ = match () with a__suffix -> ignore a__suffix
94-
95-
[@@@end]
96-
97-
[@@@expand_inline
9842
module type S = sig
9943
[%%foo] [@@test.sig.ext "suffix"]
100-
end]
101-
102-
module type S = sig
103-
val foo : unit
10444
end
10545

106-
[@@@end]
107-
[@@@expand_inline [%%foo] [@@test.str.ext "suffix"]]
108-
109-
let foo = ()
46+
[%%foo] [@@test.str.ext "suffix"]
11047

111-
[@@@end]
112-
113-
[@@@expand_inline
11448
module _ = struct
11549
"" [@@test.str.evl "suffix"]
116-
end]
117-
118-
module _ = struct
119-
""
12050
end
12151

122-
[@@@end]
123-
124-
[@@@expand_inline
12552
let _ =
12653
foo
12754
[@alert "-1"]
12855
[@suffix "_suffix"]
12956
[@alert "-2"]
13057
[@prefix "prefix_"]
131-
[@alert "-3"]]
132-
133-
let _ = prefix_foo_suffix [@alert "-1"] [@alert "-2"] [@alert "-3"]
134-
135-
[@@@end]
58+
[@alert "-3"]

0 commit comments

Comments
 (0)