Skip to content

Commit 4d462a8

Browse files
committed
Flip fold_right2 order
1 parent d1e2dab commit 4d462a8

18 files changed

+224
-150
lines changed

jscomp/bin/all_ounit_tests.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -6150,7 +6150,11 @@ val map_append : ('b -> 'a) -> 'b list -> 'a list -> 'a list
61506150

61516151
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
61526152

6153-
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
6153+
val fold_right2 :
6154+
'a list ->
6155+
'b list ->
6156+
'c ->
6157+
('a -> 'b -> 'c -> 'c) -> 'c
61546158

61556159
val map2 :
61566160
('a -> 'b -> 'c) ->
@@ -6159,10 +6163,11 @@ val map2 :
61596163
'c list
61606164

61616165
val fold_left_with_offset :
6162-
(int -> 'acc -> 'a -> 'acc) ->
6163-
int ->
6166+
'a list ->
61646167
'acc ->
6165-
'a list -> 'acc
6168+
int ->
6169+
('a -> 'acc -> int -> 'acc) ->
6170+
'acc
61666171

61676172

61686173
(** @unused *)
@@ -6511,7 +6516,7 @@ let rec fold_right f l acc =
65116516
| a0::a1::a2::a3::a4::rest ->
65126517
f a0 (f a1 (f a2 (f a3 (f a4 (fold_right f rest acc)))))
65136518

6514-
let rec fold_right2 f l r acc =
6519+
let rec fold_right2 l r acc f =
65156520
match l,r with
65166521
| [],[] -> acc
65176522
| [a0],[b0] -> f a0 b0 acc
@@ -6522,7 +6527,7 @@ let rec fold_right2 f l r acc =
65226527
| [a0;a1;a2;a3;a4], [b0;b1;b2;b3;b4] ->
65236528
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 acc))))
65246529
| a0::a1::a2::a3::a4::arest, b0::b1::b2::b3::b4::brest ->
6525-
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 f arest brest acc)))))
6530+
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 arest brest acc f )))))
65266531
| _, _ -> invalid_arg "Ext_list.fold_right2"
65276532

65286533
let rec map2 f l r =
@@ -6560,10 +6565,15 @@ let rec map2 f l r =
65606565
c0::c1::c2::c3::c4::map2 f arest brest
65616566
| _, _ -> invalid_arg "Ext_list.map2"
65626567

6563-
let rec fold_left_with_offset f i accu l =
6568+
let rec fold_left_with_offset l accu i f =
65646569
match l with
65656570
| [] -> accu
6566-
| a::l -> fold_left_with_offset f (succ i) (f i accu a) l
6571+
| a::l ->
6572+
fold_left_with_offset
6573+
l
6574+
(f a accu i)
6575+
(i + 1)
6576+
f
65676577

65686578

65696579
let rec filter_map (f: 'a -> 'b option) xs =

jscomp/core/js_pass_tailcall_inline.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ let subst name export_set stats =
212212
Js_op_util.update_used_stats v.ident_info Dead_pure;
213213
let block =
214214
Ext_list.fold_right2
215+
params args ( self#block block) (* see #278 before changes*)
215216
(fun param arg acc ->
216-
S.define_variable ~kind:Variable param arg :: acc)
217-
params args ( self#block block) (* see #278 before changes*)
218-
217+
S.define_variable ~kind:Variable param arg :: acc)
219218
in
220219
(* Mark a function as dead means it will never be scanned,
221220
here we inline the function

jscomp/core/lam_coercion.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ let handle_exports (meta : Lam_stats.t)
9292
let len = List.length original_exports in
9393
let tbl = String_hash_set.create len in
9494
let ({export_list ; export_set ; groups = coercion_groups } as result) =
95-
Ext_list.fold_right2
95+
Ext_list.fold_right2 original_exports
96+
lambda_exports
97+
{export_list = []; export_set = original_export_set; export_map = Ident_map.empty; groups = []}
9698
(fun (original_export_id : Ident.t) (lam : Lam.t) (acc : t) ->
9799
let original_name = original_export_id.name in
98100
if not @@ String_hash_set.check_add tbl original_name then
@@ -151,9 +153,7 @@ let handle_exports (meta : Lam_stats.t)
151153
groups = Single(Strict, newid, lam) :: acc.groups
152154
})
153155
)
154-
original_exports
155-
lambda_exports
156-
{export_list = []; export_set = original_export_set; export_map = Ident_map.empty; groups = []}
156+
157157

158158
in
159159

jscomp/core/lam_compile.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1315,8 +1315,10 @@ and
13151315
(* [i] is the jump table, [largs] is the arguments passed to [Lstaticcatch]*)
13161316
begin
13171317
match Lam_compile_context.find_exn i cxt with
1318-
| {exit_id; args ; order_id} ->
1319-
Ext_list.fold_right2
1318+
| {exit_id; bindings ; order_id} ->
1319+
Ext_list.fold_right2 largs bindings
1320+
(Js_output.make [S.assign exit_id (E.small_int order_id)]
1321+
~value:E.undefined)
13201322
(fun (x : Lam.t) (arg: Ident.t) acc ->
13211323
let new_output =
13221324
match x with
@@ -1327,9 +1329,7 @@ and
13271329
compile_lambda {cxt with st = Assign arg ; should_return = ReturnFalse} x
13281330
in Js_output.append_output new_output acc
13291331
)
1330-
largs args
1331-
(Js_output.make [S.assign exit_id (E.small_int order_id)]
1332-
~value:E.undefined)
1332+
13331333
| exception Not_found ->
13341334
assert false
13351335
(* staticraise is always enclosed by catch *)

jscomp/core/lam_compile_context.ml

+11-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type jbl_label = int
3030
module HandlerMap = Int_map
3131
type value = {
3232
exit_id : Ident.t ;
33-
args : Ident.t list ;
33+
bindings : Ident.t list ;
3434
order_id : int
3535
}
3636

@@ -85,17 +85,20 @@ type handler = {
8585

8686
(* always keep key id positive, specifically no [0] generated *)
8787
let add_jmps
88-
m
88+
(m : jmp_table)
8989
exit_id code_table
9090
=
9191
let map, handlers =
92-
Ext_list.fold_left_with_offset
93-
(fun order_id (acc,handlers)
94-
{ label = l; handler = lam; bindings = args}
92+
Ext_list.fold_left_with_offset
93+
code_table (m,[])
94+
(HandlerMap.cardinal m + 1 )
95+
(fun { label; handler; bindings}
96+
(acc,handlers)
97+
order_id
9598
->
96-
HandlerMap.add l {exit_id;args; order_id } acc,
97-
(order_id,lam)::handlers
98-
) (HandlerMap.cardinal m + 1 ) (m,[]) code_table in
99+
HandlerMap.add label {exit_id; bindings; order_id } acc,
100+
(order_id,handler)::handlers
101+
) in
99102
map, List.rev handlers
100103

101104

jscomp/core/lam_compile_context.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type jbl_label = int
4242

4343
type value = {
4444
exit_id : Ident.t ;
45-
args : Ident.t list ;
45+
bindings : Ident.t list ;
4646
order_id : int
4747
}
4848

jscomp/core/lam_pass_deep_flatten.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,29 @@ let deep_flatten
155155
..
156156
]}
157157
*)
158-
let (res,l) = flatten acc arg in
158+
let (res,accux) = flatten acc arg in
159159
begin match id.name, str, res with
160160
| ("match" | "include"| "param"),
161161
(Alias | Strict | StrictOpt),
162162
Lprim {primitive = Pmakeblock(_,_, Immutable); args} ->
163163
begin match eliminate_tuple id body Int_map.empty with
164164
| Some (tuple_mapping, body) ->
165165
flatten (
166-
Ext_list.fold_left_with_offset
167-
(fun i acc (arg : Lam.t) ->
166+
Ext_list.fold_left_with_offset args accux 0
167+
(fun arg acc i ->
168168
match Int_map.find_opt i tuple_mapping with
169169
| None ->
170170
Lam_group.nop_cons arg acc
171171
| Some key ->
172172
Lam_group.single str key arg :: acc
173173
)
174-
0
175-
l args
174+
175+
176176
) body
177177
| None ->
178-
flatten (Single(str, id, res ) :: l) body
178+
flatten (Single(str, id, res ) :: accux) body
179179
end
180-
| _ -> flatten (Single(str, id, res ) :: l) body
180+
| _ -> flatten (Single(str, id, res ) :: accux) body
181181
end
182182
| Lletrec (bind_args, body) ->
183183

jscomp/core/lam_pass_exits.ml

+3-5
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,11 @@ let subst_helper (subst : subst_tbl) (query : int -> int) lam =
247247
let handler = to_lam handler in
248248
let ys = Ext_list.map xs Ident.rename in
249249
let env =
250-
Ext_list.fold_right2
250+
Ext_list.fold_right2 xs ys Ident_map.empty
251251
(fun x y t -> Ident_map.add x (Lam.var y) t)
252-
xs ys Ident_map.empty in
253-
Ext_list.fold_right2
252+
in
253+
Ext_list.fold_right2 ys ls (Lam_subst.subst env handler)
254254
(fun y l r -> Lam.let_ Strict y l r)
255-
ys ls
256-
(Lam_subst.subst env handler)
257255
| None -> Lam.staticraise i ls
258256
end
259257
| Lstaticcatch (l1,(i,xs),l2) ->

jscomp/ext/ext_list.ml

+9-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ let rec fold_right f l acc =
190190
| a0::a1::a2::a3::a4::rest ->
191191
f a0 (f a1 (f a2 (f a3 (f a4 (fold_right f rest acc)))))
192192

193-
let rec fold_right2 f l r acc =
193+
let rec fold_right2 l r acc f =
194194
match l,r with
195195
| [],[] -> acc
196196
| [a0],[b0] -> f a0 b0 acc
@@ -201,7 +201,7 @@ let rec fold_right2 f l r acc =
201201
| [a0;a1;a2;a3;a4], [b0;b1;b2;b3;b4] ->
202202
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 acc))))
203203
| a0::a1::a2::a3::a4::arest, b0::b1::b2::b3::b4::brest ->
204-
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 f arest brest acc)))))
204+
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 arest brest acc f )))))
205205
| _, _ -> invalid_arg "Ext_list.fold_right2"
206206

207207
let rec map2 f l r =
@@ -239,10 +239,15 @@ let rec map2 f l r =
239239
c0::c1::c2::c3::c4::map2 f arest brest
240240
| _, _ -> invalid_arg "Ext_list.map2"
241241

242-
let rec fold_left_with_offset f i accu l =
242+
let rec fold_left_with_offset l accu i f =
243243
match l with
244244
| [] -> accu
245-
| a::l -> fold_left_with_offset f (succ i) (f i accu a) l
245+
| a::l ->
246+
fold_left_with_offset
247+
l
248+
(f a accu i)
249+
(i + 1)
250+
f
246251

247252

248253
let rec filter_map (f: 'a -> 'b option) xs =

jscomp/ext/ext_list.mli

+9-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ val map_append : ('b -> 'a) -> 'b list -> 'a list -> 'a list
4646

4747
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
4848

49-
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
49+
val fold_right2 :
50+
'a list ->
51+
'b list ->
52+
'c ->
53+
('a -> 'b -> 'c -> 'c) -> 'c
5054

5155
val map2 :
5256
('a -> 'b -> 'c) ->
@@ -55,10 +59,11 @@ val map2 :
5559
'c list
5660

5761
val fold_left_with_offset :
58-
(int -> 'acc -> 'a -> 'acc) ->
59-
int ->
62+
'a list ->
6063
'acc ->
61-
'a list -> 'acc
64+
int ->
65+
('a -> 'acc -> int -> 'acc) ->
66+
'acc
6267

6368

6469
(** @unused *)

jscomp/syntax/ast_core_type.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ let from_labels ~loc arity labels
131131
(Ast_compatible.object_ ~loc
132132
(Ext_list.map2 (fun x y -> x.Asttypes.txt ,[], y) labels tyvars) Closed)
133133
in
134-
Ext_list.fold_right2
135-
(fun {Asttypes.loc ; txt = label }
136-
tyvar acc -> Ast_compatible.label_arrow ~loc label tyvar acc) labels tyvars result_type
134+
Ext_list.fold_right2 labels tyvars result_type
135+
(fun label (* {loc ; txt = label }*)
136+
tyvar acc ->
137+
Ast_compatible.label_arrow ~loc:label.loc label.txt tyvar acc)
137138

138139

139140
let make_obj ~loc xs =

jscomp/syntax/ast_tuple_pattern_flatten.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ let flattern_tuple_pattern_vb
100100
Ext_list.same_length es xs
101101
->
102102
Bs_ast_invariant.warn_unused_attributes tuple_attributes ; (* will be dropped*)
103-
(Ext_list.fold_right2 (fun pat exp acc->
103+
Ext_list.fold_right2 xs es acc (fun pat exp acc->
104104
{Parsetree.
105105
pvb_pat =
106106
pat;
@@ -120,7 +120,7 @@ let flattern_tuple_pattern_vb
120120
pvb_attributes;
121121
pvb_loc ;
122122
} :: acc
123-
) xs es) acc
123+
)
124124
| _ ->
125125
{pvb_pat ;
126126
pvb_expr ;

jscomp/syntax/ast_util.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,13 @@ let ocaml_obj_as_js_object
609609
Location.raise_errorf ~loc "Only method support currently"
610610
) clfs ([], [], [], false) in
611611
let pval_type =
612-
Ext_list.fold_right2
612+
Ext_list.fold_right2 labels label_types public_obj_type
613613
(fun label label_type acc ->
614614
Ast_compatible.label_arrow
615615
~loc:label.Asttypes.loc
616616
label.Asttypes.txt
617617
label_type acc
618-
) labels label_types public_obj_type in
618+
) in
619619
Ast_external_mk.local_extern_cont
620620
loc
621621
~pval_prim:(External_process.pval_prim_of_labels labels)

lib/bsb.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,11 @@ val map_append : ('b -> 'a) -> 'b list -> 'a list -> 'a list
23632363

23642364
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
23652365

2366-
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
2366+
val fold_right2 :
2367+
'a list ->
2368+
'b list ->
2369+
'c ->
2370+
('a -> 'b -> 'c -> 'c) -> 'c
23672371

23682372
val map2 :
23692373
('a -> 'b -> 'c) ->
@@ -2372,10 +2376,11 @@ val map2 :
23722376
'c list
23732377

23742378
val fold_left_with_offset :
2375-
(int -> 'acc -> 'a -> 'acc) ->
2376-
int ->
2379+
'a list ->
23772380
'acc ->
2378-
'a list -> 'acc
2381+
int ->
2382+
('a -> 'acc -> int -> 'acc) ->
2383+
'acc
23792384

23802385

23812386
(** @unused *)
@@ -2724,7 +2729,7 @@ let rec fold_right f l acc =
27242729
| a0::a1::a2::a3::a4::rest ->
27252730
f a0 (f a1 (f a2 (f a3 (f a4 (fold_right f rest acc)))))
27262731

2727-
let rec fold_right2 f l r acc =
2732+
let rec fold_right2 l r acc f =
27282733
match l,r with
27292734
| [],[] -> acc
27302735
| [a0],[b0] -> f a0 b0 acc
@@ -2735,7 +2740,7 @@ let rec fold_right2 f l r acc =
27352740
| [a0;a1;a2;a3;a4], [b0;b1;b2;b3;b4] ->
27362741
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 acc))))
27372742
| a0::a1::a2::a3::a4::arest, b0::b1::b2::b3::b4::brest ->
2738-
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 f arest brest acc)))))
2743+
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 arest brest acc f )))))
27392744
| _, _ -> invalid_arg "Ext_list.fold_right2"
27402745

27412746
let rec map2 f l r =
@@ -2773,10 +2778,15 @@ let rec map2 f l r =
27732778
c0::c1::c2::c3::c4::map2 f arest brest
27742779
| _, _ -> invalid_arg "Ext_list.map2"
27752780

2776-
let rec fold_left_with_offset f i accu l =
2781+
let rec fold_left_with_offset l accu i f =
27772782
match l with
27782783
| [] -> accu
2779-
| a::l -> fold_left_with_offset f (succ i) (f i accu a) l
2784+
| a::l ->
2785+
fold_left_with_offset
2786+
l
2787+
(f a accu i)
2788+
(i + 1)
2789+
f
27802790

27812791

27822792
let rec filter_map (f: 'a -> 'b option) xs =

0 commit comments

Comments
 (0)