Skip to content

Commit 50a4103

Browse files
cknittclaude
andauthored
Remove the unused restoring of the PPX context (#8859)
Every caller drops the context without restoring it, so the cookie store was never filled. PPXs still receive an empty cookies field. Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent aa2078e commit 50a4103

7 files changed

Lines changed: 73 additions & 123 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Remove the unused restoring of the PPX context, including the cookie store. https://github.com/rescript-lang/rescript/pull/8859

‎compiler/bsc/rescript_compiler_main.ml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ let reprint_source_file sourcefile =
116116
exit 1);
117117
Res_compmisc.init_path ();
118118
parse_result.parsetree
119-
|> Cmd_ppx_apply.apply_rewriters ~restore:false
120-
~tool_name:Js_config.tool_name Ml
119+
|> Cmd_ppx_apply.apply_rewriters ~tool_name:Js_config.tool_name Ml
121120
|> Ppx_entry.rewrite_implementation
122121
|> Res_printer.print_implementation ~width:100
123122
~comments:parse_result.comments
@@ -132,8 +131,7 @@ let reprint_source_file sourcefile =
132131
exit 1);
133132
Res_compmisc.init_path ();
134133
parse_result.parsetree
135-
|> Cmd_ppx_apply.apply_rewriters ~restore:false
136-
~tool_name:Js_config.tool_name Mli
134+
|> Cmd_ppx_apply.apply_rewriters ~tool_name:Js_config.tool_name Mli
137135
|> Ppx_entry.rewrite_signature
138136
|> Res_printer.print_interface ~width:100 ~comments:parse_result.comments
139137
|> print_endline

‎compiler/core/cmd_ppx_apply.ml‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ let rewrite kind ppxs ast =
9393
out
9494
| _ -> assert false
9595

96-
let apply_rewriters_str ?(restore = true) ~tool_name ast =
96+
let apply_rewriters_str ~tool_name ast =
9797
match !Clflags.all_ppx with
9898
| [] ->
9999
if !Js_config.test_ast_conversion then
@@ -103,9 +103,9 @@ let apply_rewriters_str ?(restore = true) ~tool_name ast =
103103
ast
104104
|> Ast_mapper.add_ppx_context_str ~tool_name
105105
|> rewrite Ml ppxs |> Ml_binary.ast0_to_structure
106-
|> Ast_mapper.drop_ppx_context_str ~restore
106+
|> Ast_mapper.drop_ppx_context_str
107107

108-
let apply_rewriters_sig ?(restore = true) ~tool_name ast =
108+
let apply_rewriters_sig ~tool_name ast =
109109
match !Clflags.all_ppx with
110110
| [] ->
111111
if !Js_config.test_ast_conversion then
@@ -115,10 +115,10 @@ let apply_rewriters_sig ?(restore = true) ~tool_name ast =
115115
ast
116116
|> Ast_mapper.add_ppx_context_sig ~tool_name
117117
|> rewrite Mli ppxs |> Ml_binary.ast0_to_signature
118-
|> Ast_mapper.drop_ppx_context_sig ~restore
118+
|> Ast_mapper.drop_ppx_context_sig
119119

120-
let apply_rewriters ?restore ~tool_name (type a) (kind : a Ml_binary.kind)
121-
(ast : a) : a =
120+
let apply_rewriters ~tool_name (type a) (kind : a Ml_binary.kind) (ast : a) : a
121+
=
122122
match kind with
123-
| Ml_binary.Ml -> apply_rewriters_str ?restore ~tool_name ast
124-
| Ml_binary.Mli -> apply_rewriters_sig ?restore ~tool_name ast
123+
| Ml_binary.Ml -> apply_rewriters_str ~tool_name ast
124+
| Ml_binary.Mli -> apply_rewriters_sig ~tool_name ast

‎compiler/core/js_implementation.ml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ let interface ~parser ppf ?outputprefix fname =
6464
in
6565
Res_compmisc.init_path ();
6666
parser fname
67-
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name
68-
Mli
67+
|> Cmd_ppx_apply.apply_rewriters ~tool_name:Js_config.tool_name Mli
6968
|> Ppx_entry.rewrite_signature
7069
|> print_if_pipe ppf Clflags.dump_parsetree Printast.interface
7170
|> print_if_pipe ppf Clflags.dump_source Pprintast.signature
@@ -151,8 +150,7 @@ let implementation ~parser ppf ?outputprefix fname =
151150
in
152151
Res_compmisc.init_path ();
153152
parser fname
154-
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name
155-
Ml
153+
|> Cmd_ppx_apply.apply_rewriters ~tool_name:Js_config.tool_name Ml
156154
|> Ppx_entry.rewrite_implementation
157155
|> print_if_pipe ppf Clflags.dump_parsetree Printast.implementation
158156
|> print_if_pipe ppf Clflags.dump_source Pprintast.structure

‎compiler/ml/ast_mapper.ml‎

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,6 @@ let rec extension_of_error {loc; msg; if_highlight; sub} =
612612
]
613613
@ List.map (fun ext -> Str.extension (extension_of_error ext)) sub) )
614614

615-
module String_map = Map.Make (struct
616-
type t = string
617-
let compare = compare
618-
end)
619-
620-
let cookies = ref String_map.empty
621-
622615
module Ppx_context = struct
623616
open Longident
624617
open Asttypes
@@ -641,18 +634,6 @@ module Ppx_context = struct
641634
Exp.construct (lid "::") (Location.mknoloc [f x; make_list f rest])
642635
| [] -> Exp.construct (lid "[]") (Location.mknoloc [])
643636

644-
let make_pair f1 f2 (x1, x2) = Exp.tuple [f1 x1; f2 x2]
645-
646-
let get_cookies () =
647-
{
648-
lid = lid "cookies";
649-
x =
650-
make_list
651-
(make_pair make_string (fun x -> x))
652-
(String_map.bindings !cookies);
653-
opt = false;
654-
}
655-
656637
let mk fields =
657638
( {txt = "ocaml.ppx.context"; loc = Location.none},
658639
Parsetree.PStr [Str.eval (Exp.record fields None)] )
@@ -677,98 +658,24 @@ module Ppx_context = struct
677658
opt = false;
678659
};
679660
{lid = lid "debug"; x = make_bool !Clflags.debug; opt = false};
680-
get_cookies ();
661+
(* The compiler doesn't read back the cookies a PPX sets. *)
662+
{lid = lid "cookies"; x = make_list Fun.id []; opt = false};
681663
]
682664
in
683665
mk fields
684-
685-
let get_fields = function
686-
| PStr
687-
[{pstr_desc = Pstr_eval ({pexp_desc = Pexp_record (fields, None)}, [])}]
688-
->
689-
fields
690-
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context] syntax"
691-
692-
let restore fields =
693-
let field name payload =
694-
let rec get_string = function
695-
| {pexp_desc = Pexp_constant (Pconst_string payload)} ->
696-
String_literal.string_semantic payload
697-
| _ ->
698-
raise_errorf
699-
"Internal error: invalid [@@@ocaml.ppx.context { %s }] string \
700-
syntax"
701-
name
702-
and get_bool pexp =
703-
match pexp with
704-
| {
705-
pexp_desc = Pexp_construct ({txt = Longident.Lident "true"}, {txt = []});
706-
} ->
707-
true
708-
| {
709-
pexp_desc =
710-
Pexp_construct ({txt = Longident.Lident "false"}, {txt = []});
711-
} ->
712-
false
713-
| _ ->
714-
raise_errorf
715-
"Internal error: invalid [@@@ocaml.ppx.context { %s }] bool syntax"
716-
name
717-
and get_list elem = function
718-
| {
719-
pexp_desc =
720-
Pexp_construct ({txt = Longident.Lident "::"}, {txt = [exp; rest]});
721-
} ->
722-
elem exp :: get_list elem rest
723-
| {
724-
pexp_desc =
725-
Pexp_construct ({txt = Longident.Lident "[]"}, {txt = []});
726-
} ->
727-
[]
728-
| _ ->
729-
raise_errorf
730-
"Internal error: invalid [@@@ocaml.ppx.context { %s }] list syntax"
731-
name
732-
and get_pair f1 f2 = function
733-
| {pexp_desc = Pexp_tuple [e1; e2]} -> (f1 e1, f2 e2)
734-
| _ ->
735-
raise_errorf
736-
"Internal error: invalid [@@@ocaml.ppx.context { %s }] pair syntax"
737-
name
738-
in
739-
match name with
740-
| "include_dirs" -> Clflags.include_dirs := get_list get_string payload
741-
| "load_path" -> Config.load_path := get_list get_string payload
742-
| "open_modules" -> Clflags.open_modules := get_list get_string payload
743-
| "debug" -> Clflags.debug := get_bool payload
744-
| "cookies" ->
745-
let l = get_list (get_pair get_string (fun x -> x)) payload in
746-
cookies :=
747-
List.fold_left
748-
(fun s (k, v) -> String_map.add k v s)
749-
String_map.empty l
750-
| _ -> ()
751-
in
752-
List.iter
753-
(function
754-
| {lid = {txt = Lident name}; x} -> field name x
755-
| _ -> ())
756-
fields
757666
end
758667

759668
let ppx_context = Ppx_context.make
760669

761-
let drop_ppx_context_str ~restore = function
762-
| {pstr_desc = Pstr_attribute ({Location.txt = "ocaml.ppx.context"}, a)}
670+
let drop_ppx_context_str = function
671+
| {pstr_desc = Pstr_attribute ({Location.txt = "ocaml.ppx.context"}, _)}
763672
:: items ->
764-
if restore then Ppx_context.restore (Ppx_context.get_fields a);
765673
items
766674
| items -> items
767675

768-
let drop_ppx_context_sig ~restore = function
769-
| {psig_desc = Psig_attribute ({Location.txt = "ocaml.ppx.context"}, a)}
676+
let drop_ppx_context_sig = function
677+
| {psig_desc = Psig_attribute ({Location.txt = "ocaml.ppx.context"}, _)}
770678
:: items ->
771-
if restore then Ppx_context.restore (Ppx_context.get_fields a);
772679
items
773680
| items -> items
774681

‎compiler/ml/ast_mapper.mli‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,9 @@ val add_ppx_context_sig :
114114
tool_name:string -> Parsetree.signature -> Parsetree.signature
115115
(** Same as [add_ppx_context_str], but for signatures. *)
116116

117-
val drop_ppx_context_str :
118-
restore:bool -> Parsetree.structure -> Parsetree.structure
119-
(** Drop the ocaml.ppx.context attribute from a structure. If
120-
[restore] is true, also restore the associated data in the current
121-
process. *)
122-
123-
val drop_ppx_context_sig :
124-
restore:bool -> Parsetree.signature -> Parsetree.signature
117+
val drop_ppx_context_str : Parsetree.structure -> Parsetree.structure
118+
(** Drop the ocaml.ppx.context attribute from a structure. The compiler does
119+
not take back any of its data, such as cookies a PPX set. *)
120+
121+
val drop_ppx_context_sig : Parsetree.signature -> Parsetree.signature
125122
(** Same as [drop_ppx_context_str], but for signatures. *)

‎tests/ounit_tests/ounit_ast_mapper0_tests.ml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,54 @@ let record_pat0 attrs =
2828
let map_pat0 pat =
2929
Ast_mapper_from0.default_mapper.pat Ast_mapper_from0.default_mapper pat
3030

31+
(* The [ocaml.ppx.context] attribute a PPX receives. *)
32+
let ppx_context_fields0 ast0 =
33+
match ast0 with
34+
| Ml_binary.Impl
35+
({
36+
Parsetree0.pstr_desc =
37+
Pstr_attribute
38+
( {txt = "ocaml.ppx.context"},
39+
PStr
40+
[
41+
{
42+
pstr_desc =
43+
Pstr_eval ({pexp_desc = Pexp_record (fields, None)}, _);
44+
};
45+
] );
46+
}
47+
:: _) ->
48+
fields
49+
| _ -> assert_failure "the structure starts with the PPX context"
50+
51+
let test_ppx_context_on_the_wire _ =
52+
let structure =
53+
Ast_mapper.add_ppx_context_str ~tool_name:"rescript"
54+
[
55+
Ast_helper.Str.eval
56+
(Ast_helper.Exp.constant (Pconst_integer ("1", None)));
57+
]
58+
in
59+
let fields =
60+
ppx_context_fields0 (Ml_binary.to_ast0 Ml_binary.Ml structure)
61+
|> List.map (fun (({txt} : Longident.t Location.loc), value) ->
62+
(Longident.last txt, value))
63+
in
64+
OUnit.assert_equal ~printer:(String.concat ", ")
65+
[
66+
"tool_name";
67+
"include_dirs";
68+
"load_path";
69+
"open_modules";
70+
"debug";
71+
"cookies";
72+
]
73+
(List.map fst fields);
74+
(match List.assoc "cookies" fields with
75+
| {Parsetree0.pexp_desc = Pexp_construct ({txt = Lident "[]"}, None)} -> ()
76+
| _ -> assert_failure "the PPX gets no cookies");
77+
OUnit.assert_equal 1 (List.length (Ast_mapper.drop_ppx_context_str structure))
78+
3179
let test_removed_open_pattern_is_rejected_from_ast0 _ =
3280
let pat =
3381
Ast_helper0.Pat.open_ ~loc
@@ -2126,4 +2174,5 @@ let suites =
21262174
>:: test_field_runtime_name_keeps_ppx_order_on_the_wire;
21272175
"constructor_runtime_tag_reaches_ast0_as_an_attribute"
21282176
>:: test_constructor_runtime_tag_reaches_ast0_as_an_attribute;
2177+
"ppx_context_on_the_wire" >:: test_ppx_context_on_the_wire;
21292178
]

0 commit comments

Comments
 (0)