Skip to content

Commit 1cb8f08

Browse files
committed
Fix implementation of directives.
The compiler's ppx is only run when parsing the source file. For config settings such as directives, this is OK when `bsc` is invoked directly, but not when part of building a project. In the latter case, the ast is compiled separately, and the builtin ppx is not run again before emitting code.
1 parent d3848cf commit 1cb8f08

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ These are only breaking changes for unformatted code.
5151
- Parser: fix location of variable when function definition `{v => ...}` is enclosed in braces https://github.com/rescript-lang/rescript-compiler/pull/5949
5252
- Fix issue with error messages for uncurried functions where expected and given type were swapped https://github.com/rescript-lang/rescript-compiler/pull/5973
5353
- Fix issue with integer overflow check https://github.com/rescript-lang/rescript-compiler/pull/6028
54+
- Fix implementation of directives https://github.com/rescript-lang/rescript-compiler/pull/6052
5455

5556
#### :nail_care: Polish
5657

jscomp/core/js_implementation.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let process_with_gentype cmt_file =
4646

4747
let after_parsing_sig ppf outputprefix ast =
4848
if !Clflags.only_parse = false then (
49-
Ast_config.iter_on_bs_config_sigi ast;
49+
Ast_config.process_sig ast;
5050
if !Js_config.modules then
5151
output_deps_set !Location.input_name
5252
(Ast_extract.read_parse_and_extract Mli ast);
@@ -131,7 +131,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
131131
if !Clflags.only_parse = false then (
132132
Js_config.all_module_aliases :=
133133
!Clflags.assume_no_mli = Mli_non_exists && all_module_alias ast;
134-
Ast_config.iter_on_bs_config_stru ast;
134+
Ast_config.process_str ast;
135135
let ast = if !Js_config.no_export then no_export ast else ast in
136136
if !Js_config.modules then
137137
output_deps_set !Location.input_name

jscomp/frontend/ast_config.ml

+18-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ let signature_config_table : action_table ref = ref Map_string.empty
3838
let add_signature k v =
3939
signature_config_table := Map_string.add !signature_config_table k v
4040

41-
let rec iter_on_bs_config_stru (x : Parsetree.structure) =
41+
let process_directives str =
42+
Js_config.directives := []; (* Restt: multiple calls possible e.g. with bsc from the command-line *)
43+
str |> List.iter(fun (item : Parsetree.structure_item) -> match item.pstr_desc with
44+
| Pstr_attribute ({ txt = "directive" },
45+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
46+
Js_config.directives := !Js_config.directives @ [d]
47+
| _ -> ())
48+
49+
let rec iter_on_bs_config_str (x : Parsetree.structure) =
4250
match x with
4351
| [] -> ()
4452
| {
@@ -51,10 +59,14 @@ let rec iter_on_bs_config_stru (x : Parsetree.structure) =
5159
Ext_list.iter
5260
(Ast_payload.ident_or_record_as_config loc payload)
5361
(Ast_payload.table_dispatch !structural_config_table)
54-
| { pstr_desc = Pstr_attribute _ } :: rest -> iter_on_bs_config_stru rest
62+
| { pstr_desc = Pstr_attribute _ } :: rest -> iter_on_bs_config_str rest
5563
| _ :: _ -> ()
5664

57-
let rec iter_on_bs_config_sigi (x : Parsetree.signature) =
65+
let process_str str =
66+
iter_on_bs_config_str str;
67+
process_directives str
68+
69+
let rec iter_on_bs_config_sig (x : Parsetree.signature) =
5870
match x with
5971
| [] -> ()
6072
| {
@@ -67,5 +79,7 @@ let rec iter_on_bs_config_sigi (x : Parsetree.signature) =
6779
Ext_list.iter
6880
(Ast_payload.ident_or_record_as_config loc payload)
6981
(Ast_payload.table_dispatch !signature_config_table)
70-
| { psig_desc = Psig_attribute _ } :: rest -> iter_on_bs_config_sigi rest
82+
| { psig_desc = Psig_attribute _ } :: rest -> iter_on_bs_config_sig rest
7183
| _ :: _ -> ()
84+
85+
let process_sig s = iter_on_bs_config_sig s

jscomp/frontend/ast_config.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ val add_structure : string -> (Parsetree.expression option -> unit) -> unit
2626

2727
val add_signature : string -> (Parsetree.expression option -> unit) -> unit
2828

29-
val iter_on_bs_config_stru : Parsetree.structure -> unit
29+
val process_str : Parsetree.structure -> unit
3030

31-
val iter_on_bs_config_sigi : Parsetree.signature -> unit
31+
val process_sig : Parsetree.signature -> unit

jscomp/frontend/bs_builtin_ppx.ml

-4
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,6 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
431431
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
432432
})
433433
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
434-
| Pstr_attribute ({ txt = "directive" },
435-
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
436-
Js_config.directives := d :: !Js_config.directives;
437-
str
438434
| _ -> default_mapper.structure_item self str
439435

440436
let local_module_name =

jscomp/frontend/ppx_entry.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let unsafe_mapper = Bs_builtin_ppx.mapper
2626

2727
let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
2828
Bs_ast_invariant.iter_warnings_on_sigi ast;
29-
Ast_config.iter_on_bs_config_sigi ast;
29+
Ast_config.process_sig ast;
3030
let ast =
3131
match !Js_config.jsx_version with
3232
| None -> ast
@@ -46,7 +46,7 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
4646

4747
let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
4848
Bs_ast_invariant.iter_warnings_on_stru ast;
49-
Ast_config.iter_on_bs_config_stru ast;
49+
Ast_config.process_str ast;
5050
let ast =
5151
match !Js_config.jsx_version with
5252
| None -> ast

0 commit comments

Comments
 (0)