Skip to content

Commit bef06f2

Browse files
authored
Merge pull request #3145 from BuckleScript/fix_3142
[Part one] fix #3142 and clean up attributes handling
2 parents 0d2c4c3 + 6839c3d commit bef06f2

15 files changed

+98
-91
lines changed

jscomp/common/ext_log.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ let iwarn b str f =
5454
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str
5555

5656
(* TODO: add {[@.]} later for all *)
57-
let dwarn str f =
57+
let dwarn ?(__POS__: (string * int * int * int) option) f =
5858
if Js_config.is_same_file () then
59-
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f ^^ "@.") str
59+
match __POS__ with
60+
| None -> Format.fprintf Format.err_formatter ("WARN: " ^^ f ^^ "@.")
61+
| Some (file,line,_,_) ->
62+
Format.fprintf Format.err_formatter ("WARN: %s,%d " ^^ f ^^ "@.") file line
6063
else
61-
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f ^^ "@.") str
64+
Format.ifprintf Format.err_formatter ("WARN: " ^^ f ^^ "@.")
6265

6366
let info str f =
6467
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str

jscomp/common/ext_log.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ val err : string -> 'a logging
4646
val ierr : bool -> string -> 'a logging
4747
val warn : string -> 'a logging
4848
val iwarn : bool -> string -> 'a logging
49-
val dwarn : string -> 'a logging
49+
50+
val dwarn :
51+
?__POS__:(string * int * int *int) ->
52+
'a logging
53+
5054
val info : string -> 'a logging
5155
val iinfo : bool -> string -> 'a logging

jscomp/core/js_implementation.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ let after_parsing_sig ppf sourcefile outputprefix ast =
7979
end
8080
end
8181
let interface ppf sourcefile outputprefix =
82+
Js_config.set_current_file sourcefile ;
8283
Compmisc.init_path false;
8384
Ocaml_parse.parse_interface ppf sourcefile
8485
|> print_if ppf Clflags.dump_parsetree Printast.interface
8586
|> print_if ppf Clflags.dump_source Pprintast.signature
8687
|> after_parsing_sig ppf sourcefile outputprefix
8788

8889
let interface_mliast ppf sourcefile outputprefix =
90+
Js_config.set_current_file sourcefile ;
8991
Compmisc.init_path false;
9092
Binary_ast.read_ast Mli sourcefile
9193
|> print_if ppf Clflags.dump_parsetree Printast.interface
9294
|> print_if ppf Clflags.dump_source Pprintast.signature
9395
|> after_parsing_sig ppf sourcefile outputprefix
9496

9597
let after_parsing_impl ppf sourcefile outputprefix ast =
98+
9699
if !Js_config.binary_ast then
97100
Binary_ast.write_ast ~fname:sourcefile
98101
Ml ~output:(outputprefix ^ Literals.suffix_mlast)
@@ -156,13 +159,15 @@ let after_parsing_impl ppf sourcefile outputprefix ast =
156159
end
157160
let implementation ppf sourcefile outputprefix =
158161
Compmisc.init_path false;
162+
Js_config.set_current_file sourcefile ;
159163
Ocaml_parse.parse_implementation ppf sourcefile
160164
|> print_if ppf Clflags.dump_parsetree Printast.implementation
161165
|> print_if ppf Clflags.dump_source Pprintast.structure
162166
|> after_parsing_impl ppf sourcefile outputprefix
163167

164168
let implementation_mlast ppf sourcefile outputprefix =
165169
Compmisc.init_path false;
170+
Js_config.set_current_file sourcefile ;
166171
Binary_ast.read_ast Ml sourcefile
167172
|> print_if ppf Clflags.dump_parsetree Printast.implementation
168173
|> print_if ppf Clflags.dump_source Pprintast.structure

jscomp/core/js_pass_debug.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let dump name (prog : J.program) =
3939
then
4040
begin
4141
incr log_counter ;
42-
Ext_log.dwarn __LOC__ "\n@[[TIME:]%s: %f@]@." name (Sys.time () *. 1000.);
42+
Ext_log.dwarn ~__POS__ "\n@[[TIME:]%s: %f@]@." name (Sys.time () *. 1000.);
4343
Ext_pervasives.with_file_as_chan
4444
(Ext_path.chop_extension ~loc:__LOC__ (Js_config.get_current_file()) ^
4545
(Printf.sprintf ".%02d.%s.jsx" !log_counter name)

jscomp/core/lam_compile_const.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ and translate (x : Lam_constant.t ) : J.expression =
8484
E.str i
8585
| Const_unicode i ->
8686
E.unicode i
87-
(* E.str i ~delimiter:Literals.escaped_j_delimiter *)
87+
8888

8989
| Const_pointer (c,pointer_info) ->
9090
E.int ?comment:(Lam_compile_util.comment_of_pointer_info pointer_info)

jscomp/core/lam_compile_main.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,17 @@ let compile ~filename (output_prefix : string) env _sigs
135135
let () =
136136
#if BS_DEBUG then
137137
export_idents |> List.iter
138-
(fun (id : Ident.t) -> Ext_log.dwarn __LOC__ "export: %s/%d" id.name id.stamp) ;
138+
(fun (id : Ident.t) -> Ext_log.dwarn ~__POS__ "export: %s/%d" id.name id.stamp) ;
139139
#end
140140
Lam_compile_env.reset () ;
141141
in
142142
let lam, may_required_modules = Lam_convert.convert export_ident_sets lam in
143143
let _d = fun s lam ->
144144
let result = Lam_util.dump env s lam in
145145
#if BS_DEBUG then
146-
Ext_log.dwarn __LOC__ "START CHECKING PASS %s@." s;
146+
Ext_log.dwarn ~__POS__ "START CHECKING PASS %s@." s;
147147
ignore @@ Lam_check.check (Js_config.get_current_file ()) lam;
148-
Ext_log.dwarn __LOC__ "FINISH CHECKING PASS %s@." s;
148+
Ext_log.dwarn ~__POS__ "FINISH CHECKING PASS %s@." s;
149149
#end
150150
result
151151
in
@@ -200,7 +200,7 @@ let compile ~filename (output_prefix : string) env _sigs
200200
#if BS_DEBUG then
201201
|> (fun lam ->
202202
let () =
203-
Ext_log.dwarn __LOC__ "Before coercion: %a@." Lam_stats.print meta in
203+
Ext_log.dwarn ~__POS__ "Before coercion: %a@." Lam_stats.print meta in
204204
Lam_check.check (Js_config.get_current_file ()) lam
205205
)
206206
#end
@@ -212,7 +212,7 @@ let compile ~filename (output_prefix : string) env _sigs
212212

213213
#if BS_DEBUG then
214214
let () =
215-
Ext_log.dwarn __LOC__ "After coercion: %a@." Lam_stats.print meta ;
215+
Ext_log.dwarn ~__POS__ "After coercion: %a@." Lam_stats.print meta ;
216216
if Js_config.is_same_file () then
217217
let f =
218218
Ext_path.chop_extension ~loc:__LOC__ filename ^ ".lambda" in
@@ -224,15 +224,15 @@ let compile ~filename (output_prefix : string) env _sigs
224224
#end
225225
let maybe_pure = no_side_effects groups in
226226
#if BS_DEBUG then
227-
let () = Ext_log.dwarn __LOC__ "\n@[[TIME:]Pre-compile: %f@]@." (Sys.time () *. 1000.) in
227+
let () = Ext_log.dwarn ~__POS__ "\n@[[TIME:]Pre-compile: %f@]@." (Sys.time () *. 1000.) in
228228
#end
229229
let body =
230230
Ext_list.map groups (fun group -> compile_group meta group)
231231
|> Js_output.concat
232232
|> Js_output.output_as_block
233233
in
234234
#if BS_DEBUG then
235-
let () = Ext_log.dwarn __LOC__ "\n@[[TIME:]Post-compile: %f@]@." (Sys.time () *. 1000.) in
235+
let () = Ext_log.dwarn ~__POS__ "\n@[[TIME:]Post-compile: %f@]@." (Sys.time () *. 1000.) in
236236
#end
237237
(* The file is not big at all compared with [cmo] *)
238238
(* Ext_marshal.to_file (Ext_path.chop_extension filename ^ ".mj") js; *)
@@ -295,7 +295,6 @@ let lambda_as_module
295295
(filename : string)
296296
(output_prefix : string)
297297
(lam : Lambda.lambda) =
298-
Js_config.set_current_file filename ;
299298
let lambda_output =
300299
compile ~filename output_prefix finalenv current_signature lam in
301300
let basename =

jscomp/core/lam_pass_lets_dce.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,6 @@ let apply_lets occ lambda =
261261
let simplify_lets (lam : Lam.t) =
262262
let occ = Lam_pass_count.collect_occurs lam in
263263
#if BS_DEBUG then
264-
Ext_log.dwarn "OCCTBL" "@[%a@]@." Lam_pass_count.pp_occ_tbl occ ;
264+
Ext_log.dwarn ~__POS__ "@[%a@]@." Lam_pass_count.pp_occ_tbl occ ;
265265
#end
266266
apply_lets occ lam

jscomp/core/lam_stats_export.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let values_of_export
8686
Ident_map.is_empty free_variables
8787
then
8888
begin
89-
Ext_log.dwarn __LOC__ "%s recorded for inlining @." x.name ;
89+
Ext_log.dwarn ~__POS__ "%s recorded for inlining @." x.name ;
9090
Some lambda
9191
end
9292
else None

jscomp/core/lam_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ let dump env ext lam =
233233
(* ATTENTION: easy to introduce a bug during refactoring when forgeting `begin` `end`*)
234234
begin
235235
incr log_counter;
236-
Ext_log.dwarn __LOC__ "\n@[[TIME:]%s: %f@]@." ext (Sys.time () *. 1000.);
236+
Ext_log.dwarn ~__POS__ "\n@[[TIME:]%s: %f@]@." ext (Sys.time () *. 1000.);
237237
Lam_print.seriaize env
238238
(Ext_path.chop_extension
239239
~loc:__LOC__

jscomp/syntax/ast_attributes.ml

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -226,54 +226,26 @@ let iter_process_bs_string_int_unwrap_uncurry attrs =
226226
) attrs;
227227
!st
228228

229-
(* let process_bs_string_int_unwrap_uncurry attrs =
230-
List.fold_left
231-
(fun (st,attrs)
232-
(({txt ; loc}, (payload : _ ) ) as attr : attr) ->
233-
match txt, st with
234-
| "bs.string", (`Nothing | `String)
235-
-> `String, attrs
236-
| "bs.int", (`Nothing | `Int)
237-
-> `Int, attrs
238-
| "bs.ignore", (`Nothing | `Ignore)
239-
-> `Ignore, attrs
240-
| "bs.unwrap", (`Nothing | `Unwrap)
241-
-> `Unwrap, attrs
242-
| "bs.uncurry", `Nothing
243-
->
244-
`Uncurry (Ast_payload.is_single_int payload), attrs
245-
(* Don't allow duplicated [bs.uncurry] since
246-
it may introduce inconsistency in arity
247-
*)
248-
| "bs.int", _
249-
| "bs.string", _
250-
| "bs.ignore", _
251-
| "bs.unwrap", _
252-
->
253-
Bs_syntaxerr.err loc Conflict_attributes
254-
| _ , _ -> st, (attr :: attrs )
255-
) (`Nothing, []) attrs *)
256-
257229

258230
let iter_process_bs_string_as (attrs : t) : string option =
259231
let st = ref None in
260-
List.iter
232+
Ext_list.iter attrs
261233
(fun
262-
(({txt ; loc}, payload ) as attr : attr) ->
234+
(({txt ; loc}, payload ) as attr ) ->
263235
match txt with
264236
| "bs.as"
265237
->
266238
if !st = None then
267239
match Ast_payload.is_single_string payload with
268240
| None ->
269241
Bs_syntaxerr.err loc Expect_string_literal
270-
| Some (v,_dec) ->
242+
| Some (v,_dec) ->
271243
Bs_ast_invariant.mark_used_bs_attribute attr ;
272244
st:= Some v
273245
else
274246
Bs_syntaxerr.err loc Duplicated_bs_as
275247
| _ -> ()
276-
) attrs;
248+
) ;
277249
!st
278250

279251
let has_bs_optional (attrs : t) : bool =

jscomp/syntax/ast_derive_js_mapper.ml

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -206,24 +206,22 @@ let init () =
206206
(PStr
207207
[Str.eval
208208
(Exp.record
209-
(List.map
210-
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
209+
(Ext_list.map label_declarations
210+
(fun {pld_name = {loc; txt } } ->
211211
let label =
212212
{Asttypes.loc; txt = Longident.Lident txt } in
213-
label,Exp.field exp_param label
214-
) label_declarations) None)]))) in
213+
label,Exp.field exp_param label) ) None)]))) in
215214
let toJs =
216215
toJsBody exp
217216
in
218217
let obj_exp =
219218
Exp.record
220-
(List.map
221-
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
219+
(Ext_list.map label_declarations
220+
(fun {pld_name = {loc; txt } } ->
222221
let label =
223222
{Asttypes.loc; txt = Longident.Lident txt } in
224223
label,
225-
js_field exp_param label
226-
) label_declarations) None in
224+
js_field exp_param label) ) None in
227225
let fromJs =
228226
Ast_comb.single_non_rec_value patFromJs
229227
(Ast_compatible.fun_ (Pat.var pat_param)
@@ -259,13 +257,14 @@ let init () =
259257
Ast_comb.single_non_rec_value
260258
{loc; txt = constantArray}
261259
(Exp.array
262-
(List.map (fun (i,str) ->
263-
Exp.tuple
264-
[
265-
Ast_compatible.const_exp_int i;
266-
Ast_compatible.const_exp_string str
267-
]
268-
) (List.sort (fun (a,_) (b,_) -> compare (a:int) b) result)));
260+
(Ext_list.map (List.sort (fun (a,_) (b,_) -> compare (a:int) b) result)
261+
(fun (i,str) ->
262+
Exp.tuple
263+
[
264+
Ast_compatible.const_exp_int i;
265+
Ast_compatible.const_exp_string str
266+
]
267+
) ));
269268
(
270269
toJsBody
271270
(coerceResultToNewType
@@ -320,7 +319,6 @@ let init () =
320319
Ast_comb.single_non_rec_value
321320
{loc; txt = constantArray}
322321
(Ast_compatible.const_exp_int_list_as_array xs)
323-
(* (Exp.array (List.map (fun i -> Ast_compatible.const_exp_int i) xs )) *)
324322
;
325323
toJsBody
326324
(
@@ -433,10 +431,9 @@ let init () =
433431
let objType flag =
434432
Ast_comb.to_js_type loc @@
435433
Ast_compatible.object_
436-
(List.map
437-
(fun ({pld_name = {loc; txt }; pld_type } : Parsetree.label_declaration) ->
438-
txt, [], pld_type
439-
) label_declarations)
434+
(Ext_list.map label_declarations
435+
(fun {pld_name = {loc; txt }; pld_type } ->
436+
txt, [], pld_type))
440437
flag in
441438
newTypeStr +?
442439
[
@@ -476,7 +473,7 @@ let init () =
476473
else Ast_literal.type_int() in
477474
let ty2 =
478475
if createType then core_type
479-
else Ast_core_type.lift_option_type core_type in
476+
else Ast_core_type.lift_option_type core_type (*-FIXME**) in
480477
newTypeStr +?
481478
[
482479
toJsType ty1;

jscomp/syntax/ast_polyvar.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ let map_row_fields_into_strings ptyp_loc
9797
Ext_list.fold_right row_fields (`Nothing, []) (fun tag (nullary, acc) ->
9898
match nullary, tag with
9999
| (`Nothing | `Null),
100-
Parsetree.Rtag (label, attrs, true, [])
100+
Rtag (label, attrs, true, [])
101101
->
102102
begin match Ast_attributes.iter_process_bs_string_as attrs with
103103
| Some name ->
@@ -106,7 +106,7 @@ let map_row_fields_into_strings ptyp_loc
106106
| None ->
107107
`Null, ((hash_label label, label_of_name label) :: acc )
108108
end
109-
| (`Nothing | `NonNull), Parsetree.Rtag(label, attrs, false, ([ _ ]))
109+
| (`Nothing | `NonNull), Rtag(label, attrs, false, ([ _ ]))
110110
->
111111
begin match Ast_attributes.iter_process_bs_string_as attrs with
112112
| Some name ->

0 commit comments

Comments
 (0)