Skip to content

Commit e9df0ee

Browse files
authored
Merge pull request #4559 from BuckleScript/disable_hash_collision
disable hash collison test for polyvar
2 parents 24e171b + a7f3228 commit e9df0ee

File tree

8 files changed

+160
-49
lines changed

8 files changed

+160
-49
lines changed

jscomp/test/build.ninja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ build test/gpr_return_type_unused_attribute.cmi test/gpr_return_type_unused_attr
340340
build test/gray_code_test.cmi test/gray_code_test.cmj : cc test/gray_code_test.ml | $stdlib
341341
build test/guide_for_ext.cmi test/guide_for_ext.cmj : cc test/guide_for_ext.ml | $stdlib
342342
build test/hamming_test.cmi test/hamming_test.cmj : cc test/hamming_test.ml | test/mt.cmj $stdlib
343+
build test/hash_collision.cmi test/hash_collision.cmj : cc test/hash_collision.ml | test/mt.cmj $stdlib
343344
build test/hash_test.cmi test/hash_test.cmj : cc test/hash_test.ml | test/mt.cmj test/mt_global.cmj $stdlib
344345
build test/hashtbl_test.cmi test/hashtbl_test.cmj : cc test/hashtbl_test.ml | test/mt.cmj $stdlib
345346
build test/hello.foo.cmi test/hello.foo.cmj : cc test/hello.foo.ml | $stdlib

jscomp/test/hash_collision.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var Mt = require("./mt.js");
4+
5+
var suites = {
6+
contents: /* [] */0
7+
};
8+
9+
var test_id = {
10+
contents: 0
11+
};
12+
13+
function eq(loc, x, y) {
14+
return Mt.eq_suites(test_id, suites, loc, x, y);
15+
}
16+
17+
function f0(x) {
18+
if (x === "azdwbie") {
19+
return 1;
20+
} else {
21+
return 0;
22+
}
23+
}
24+
25+
function f1(x) {
26+
if (x.NAME === "azdwbie") {
27+
return x.VAL + 2 | 0;
28+
} else {
29+
return x.VAL + 1 | 0;
30+
}
31+
}
32+
33+
var hi = [
34+
"Eric_Cooper",
35+
"azdwbie"
36+
];
37+
38+
eq("File \"hash_collision.ml\", line 24, characters 9-16", 1, 0);
39+
40+
eq("File \"hash_collision.ml\", line 25, characters 9-16", 1, 1);
41+
42+
eq("File \"hash_collision.ml\", line 27, characters 9-16", f1({
43+
NAME: "Eric_Cooper",
44+
VAL: -1
45+
}), 0);
46+
47+
eq("File \"hash_collision.ml\", line 29, characters 9-16", f1({
48+
NAME: "azdwbie",
49+
VAL: -2
50+
}), 0);
51+
52+
Mt.from_pair_suites("hash_collision.ml", suites.contents);
53+
54+
exports.suites = suites;
55+
exports.test_id = test_id;
56+
exports.eq = eq;
57+
exports.f0 = f0;
58+
exports.f1 = f1;
59+
exports.hi = hi;
60+
/* Not a pure module */

jscomp/test/hash_collision.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
let suites : Mt.pair_suites ref = ref []
3+
let test_id = ref 0
4+
let eq loc x y = Mt.eq_suites ~test_id ~suites loc x y
5+
6+
type collision = [`Eric_Cooper | `azdwbie];;
7+
8+
let f0 x =
9+
match x with
10+
| `Eric_Cooper -> 0
11+
| `azdwbie -> 1
12+
13+
14+
15+
16+
let f1 x =
17+
match x with
18+
| `Eric_Cooper x -> x + 1
19+
| `azdwbie x -> x + 2
20+
21+
22+
23+
let hi : collision array = [| `Eric_Cooper; `azdwbie |]
24+
;; eq __LOC__ (f0 `Eric_Cooper) 0
25+
;; eq __LOC__ (f0 `azdwbie) 1
26+
27+
;; eq __LOC__ (f1 (`Eric_Cooper (-1))) 0
28+
29+
;; eq __LOC__ (f1 (`azdwbie (-2))) 0
30+
31+
;; Mt.from_pair_suites __FILE__ !suites

jscomp/test/poly_variant_test.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,8 @@ let hey x =
157157
| `h as v ->
158158
Js.log "v";
159159
Js.log v
160+
;;
161+
162+
163+
160164
let () = Mt.from_pair_suites __MODULE__ !suites

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40976,10 +40976,11 @@ let rec class_type_arity =
4097640976
(*******************************************)
4097740977
(* Miscellaneous operations on row types *)
4097840978
(*******************************************)
40979+
type row_fields = (Asttypes.label * Types.row_field) list
40980+
type row_pairs = (Asttypes.label * Types.row_field * Types.row_field) list
40981+
let sort_row_fields : row_fields -> row_fields = List.sort (fun (p,_) (q,_) -> compare (p : string) q)
4097940982

40980-
let sort_row_fields = List.sort (fun (p,_) (q,_) -> compare p q)
40981-
40982-
let rec merge_rf r1 r2 pairs fi1 fi2 =
40983+
let rec merge_rf (r1 : row_fields) (r2 : row_fields) (pairs : row_pairs) (fi1 : row_fields) (fi2 : row_fields) =
4098340984
match fi1, fi2 with
4098440985
(l1,f1 as p1)::fi1', (l2,f2 as p2)::fi2' ->
4098540986
if l1 = l2 then merge_rf r1 r2 ((l1,f1,f2)::pairs) fi1' fi2' else
@@ -40988,7 +40989,7 @@ let rec merge_rf r1 r2 pairs fi1 fi2 =
4098840989
| [], _ -> (List.rev r1, List.rev_append r2 fi2, pairs)
4098940990
| _, [] -> (List.rev_append r1 fi1, List.rev r2, pairs)
4099040991

40991-
let merge_row_fields fi1 fi2 =
40992+
let merge_row_fields (fi1 : row_fields) (fi2 : row_fields) : row_fields * row_fields * row_pairs =
4099240993
match fi1, fi2 with
4099340994
[], _ | _, [] -> (fi1, fi2, [])
4099440995
| [p1], _ when not (List.mem_assoc (fst p1) fi2) -> (fi1, fi2, [])
@@ -43209,7 +43210,8 @@ and unify_row env row1 row2 =
4320943210
let rm1 = row_more row1 and rm2 = row_more row2 in
4321043211
if unify_eq rm1 rm2 then () else
4321143212
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
43212-
if r1 <> [] && r2 <> [] then begin
43213+
if not !Config.bs_only && (r1 <> [] && r2 <> []) then begin
43214+
(* pairs are the intersection, r1 , r2 should be disjoint *)
4321343215
let ht = Hashtbl.create (List.length r1) in
4321443216
List.iter (fun (l,_) -> Hashtbl.add ht (hash_variant l) l) r1;
4321543217
List.iter
@@ -57084,19 +57086,25 @@ and transl_type_aux env policy styp =
5708457086
row_bound=(); row_closed=true;
5708557087
row_fixed=false; row_name=None}) in
5708657088
let hfields = Hashtbl.create 17 in
57089+
let collection_detect = Hashtbl.create 17 in
5708757090
let add_typed_field loc l f =
57088-
let h = Btype.hash_variant l in
57091+
if not !Config.bs_only then begin
57092+
let h = Btype.hash_variant l in
57093+
if Hashtbl.mem collection_detect h then
57094+
let l' = Hashtbl.find collection_detect h in
57095+
(* Check for tag conflicts *)
57096+
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
57097+
else Hashtbl.add collection_detect h l
57098+
end ;
5708957099
try
57090-
let (l',f') = Hashtbl.find hfields h in
57091-
(* Check for tag conflicts *)
57092-
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
57100+
let (_,f') = Hashtbl.find hfields l in
5709357101
let ty = mkfield l f and ty' = mkfield l f' in
5709457102
if equal env false [ty] [ty'] then () else
5709557103
try unify env ty ty'
5709657104
with Unify _trace ->
5709757105
raise(Error(loc, env, Constructor_mismatch (ty,ty')))
5709857106
with Not_found ->
57099-
Hashtbl.add hfields h (l,f)
57107+
Hashtbl.add hfields l (l,f)
5710057108
in
5710157109
let add_field = function
5710257110
Rtag (l, attrs, c, stl) ->
@@ -57127,13 +57135,10 @@ and transl_type_aux env policy styp =
5712757135
{desc=Tconstr(p, tl, _)} -> Some(p, tl)
5712857136
| _ -> None
5712957137
in
57130-
begin try
57138+
begin
5713157139
(* Set name if there are no fields yet *)
57132-
Hashtbl.iter (fun _ _ -> raise Exit) hfields;
57133-
name := nm
57134-
with Exit ->
57135-
(* Unset it otherwise *)
57136-
name := None
57140+
if Hashtbl.length hfields <> 0 then name := None
57141+
else name := nm
5713757142
end;
5713857143
let fl = match expand_head env cty.ctyp_type, nm with
5713957144
{desc=Tvariant row}, _ when Btype.static_row row ->

lib/4.06.1/unstable/js_refmt_compiler.ml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40976,10 +40976,11 @@ let rec class_type_arity =
4097640976
(*******************************************)
4097740977
(* Miscellaneous operations on row types *)
4097840978
(*******************************************)
40979+
type row_fields = (Asttypes.label * Types.row_field) list
40980+
type row_pairs = (Asttypes.label * Types.row_field * Types.row_field) list
40981+
let sort_row_fields : row_fields -> row_fields = List.sort (fun (p,_) (q,_) -> compare (p : string) q)
4097940982

40980-
let sort_row_fields = List.sort (fun (p,_) (q,_) -> compare p q)
40981-
40982-
let rec merge_rf r1 r2 pairs fi1 fi2 =
40983+
let rec merge_rf (r1 : row_fields) (r2 : row_fields) (pairs : row_pairs) (fi1 : row_fields) (fi2 : row_fields) =
4098340984
match fi1, fi2 with
4098440985
(l1,f1 as p1)::fi1', (l2,f2 as p2)::fi2' ->
4098540986
if l1 = l2 then merge_rf r1 r2 ((l1,f1,f2)::pairs) fi1' fi2' else
@@ -40988,7 +40989,7 @@ let rec merge_rf r1 r2 pairs fi1 fi2 =
4098840989
| [], _ -> (List.rev r1, List.rev_append r2 fi2, pairs)
4098940990
| _, [] -> (List.rev_append r1 fi1, List.rev r2, pairs)
4099040991

40991-
let merge_row_fields fi1 fi2 =
40992+
let merge_row_fields (fi1 : row_fields) (fi2 : row_fields) : row_fields * row_fields * row_pairs =
4099240993
match fi1, fi2 with
4099340994
[], _ | _, [] -> (fi1, fi2, [])
4099440995
| [p1], _ when not (List.mem_assoc (fst p1) fi2) -> (fi1, fi2, [])
@@ -43209,7 +43210,8 @@ and unify_row env row1 row2 =
4320943210
let rm1 = row_more row1 and rm2 = row_more row2 in
4321043211
if unify_eq rm1 rm2 then () else
4321143212
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
43212-
if r1 <> [] && r2 <> [] then begin
43213+
if not !Config.bs_only && (r1 <> [] && r2 <> []) then begin
43214+
(* pairs are the intersection, r1 , r2 should be disjoint *)
4321343215
let ht = Hashtbl.create (List.length r1) in
4321443216
List.iter (fun (l,_) -> Hashtbl.add ht (hash_variant l) l) r1;
4321543217
List.iter
@@ -57084,19 +57086,25 @@ and transl_type_aux env policy styp =
5708457086
row_bound=(); row_closed=true;
5708557087
row_fixed=false; row_name=None}) in
5708657088
let hfields = Hashtbl.create 17 in
57089+
let collection_detect = Hashtbl.create 17 in
5708757090
let add_typed_field loc l f =
57088-
let h = Btype.hash_variant l in
57091+
if not !Config.bs_only then begin
57092+
let h = Btype.hash_variant l in
57093+
if Hashtbl.mem collection_detect h then
57094+
let l' = Hashtbl.find collection_detect h in
57095+
(* Check for tag conflicts *)
57096+
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
57097+
else Hashtbl.add collection_detect h l
57098+
end ;
5708957099
try
57090-
let (l',f') = Hashtbl.find hfields h in
57091-
(* Check for tag conflicts *)
57092-
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
57100+
let (_,f') = Hashtbl.find hfields l in
5709357101
let ty = mkfield l f and ty' = mkfield l f' in
5709457102
if equal env false [ty] [ty'] then () else
5709557103
try unify env ty ty'
5709657104
with Unify _trace ->
5709757105
raise(Error(loc, env, Constructor_mismatch (ty,ty')))
5709857106
with Not_found ->
57099-
Hashtbl.add hfields h (l,f)
57107+
Hashtbl.add hfields l (l,f)
5710057108
in
5710157109
let add_field = function
5710257110
Rtag (l, attrs, c, stl) ->
@@ -57127,13 +57135,10 @@ and transl_type_aux env policy styp =
5712757135
{desc=Tconstr(p, tl, _)} -> Some(p, tl)
5712857136
| _ -> None
5712957137
in
57130-
begin try
57138+
begin
5713157139
(* Set name if there are no fields yet *)
57132-
Hashtbl.iter (fun _ _ -> raise Exit) hfields;
57133-
name := nm
57134-
with Exit ->
57135-
(* Unset it otherwise *)
57136-
name := None
57140+
if Hashtbl.length hfields <> 0 then name := None
57141+
else name := nm
5713757142
end;
5713857143
let fl = match expand_head env cty.ctyp_type, nm with
5713957144
{desc=Tvariant row}, _ when Btype.static_row row ->

lib/4.06.1/whole_compiler.ml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325881,10 +325881,11 @@ let rec class_type_arity =
325881325881
(*******************************************)
325882325882
(* Miscellaneous operations on row types *)
325883325883
(*******************************************)
325884+
type row_fields = (Asttypes.label * Types.row_field) list
325885+
type row_pairs = (Asttypes.label * Types.row_field * Types.row_field) list
325886+
let sort_row_fields : row_fields -> row_fields = List.sort (fun (p,_) (q,_) -> compare (p : string) q)
325884325887

325885-
let sort_row_fields = List.sort (fun (p,_) (q,_) -> compare p q)
325886-
325887-
let rec merge_rf r1 r2 pairs fi1 fi2 =
325888+
let rec merge_rf (r1 : row_fields) (r2 : row_fields) (pairs : row_pairs) (fi1 : row_fields) (fi2 : row_fields) =
325888325889
match fi1, fi2 with
325889325890
(l1,f1 as p1)::fi1', (l2,f2 as p2)::fi2' ->
325890325891
if l1 = l2 then merge_rf r1 r2 ((l1,f1,f2)::pairs) fi1' fi2' else
@@ -325893,7 +325894,7 @@ let rec merge_rf r1 r2 pairs fi1 fi2 =
325893325894
| [], _ -> (List.rev r1, List.rev_append r2 fi2, pairs)
325894325895
| _, [] -> (List.rev_append r1 fi1, List.rev r2, pairs)
325895325896

325896-
let merge_row_fields fi1 fi2 =
325897+
let merge_row_fields (fi1 : row_fields) (fi2 : row_fields) : row_fields * row_fields * row_pairs =
325897325898
match fi1, fi2 with
325898325899
[], _ | _, [] -> (fi1, fi2, [])
325899325900
| [p1], _ when not (List.mem_assoc (fst p1) fi2) -> (fi1, fi2, [])
@@ -328114,7 +328115,8 @@ and unify_row env row1 row2 =
328114328115
let rm1 = row_more row1 and rm2 = row_more row2 in
328115328116
if unify_eq rm1 rm2 then () else
328116328117
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
328117-
if r1 <> [] && r2 <> [] then begin
328118+
if not true && (r1 <> [] && r2 <> []) then begin
328119+
(* pairs are the intersection, r1 , r2 should be disjoint *)
328118328120
let ht = Hashtbl.create (List.length r1) in
328119328121
List.iter (fun (l,_) -> Hashtbl.add ht (hash_variant l) l) r1;
328120328122
List.iter
@@ -341304,19 +341306,25 @@ and transl_type_aux env policy styp =
341304341306
row_bound=(); row_closed=true;
341305341307
row_fixed=false; row_name=None}) in
341306341308
let hfields = Hashtbl.create 17 in
341309+
let collection_detect = Hashtbl.create 17 in
341307341310
let add_typed_field loc l f =
341308-
let h = Btype.hash_variant l in
341311+
if not true then begin
341312+
let h = Btype.hash_variant l in
341313+
if Hashtbl.mem collection_detect h then
341314+
let l' = Hashtbl.find collection_detect h in
341315+
(* Check for tag conflicts *)
341316+
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
341317+
else Hashtbl.add collection_detect h l
341318+
end ;
341309341319
try
341310-
let (l',f') = Hashtbl.find hfields h in
341311-
(* Check for tag conflicts *)
341312-
if l <> l' then raise(Error(styp.ptyp_loc, env, Variant_tags(l, l')));
341320+
let (_,f') = Hashtbl.find hfields l in
341313341321
let ty = mkfield l f and ty' = mkfield l f' in
341314341322
if equal env false [ty] [ty'] then () else
341315341323
try unify env ty ty'
341316341324
with Unify _trace ->
341317341325
raise(Error(loc, env, Constructor_mismatch (ty,ty')))
341318341326
with Not_found ->
341319-
Hashtbl.add hfields h (l,f)
341327+
Hashtbl.add hfields l (l,f)
341320341328
in
341321341329
let add_field = function
341322341330
Rtag (l, attrs, c, stl) ->
@@ -341347,13 +341355,10 @@ and transl_type_aux env policy styp =
341347341355
{desc=Tconstr(p, tl, _)} -> Some(p, tl)
341348341356
| _ -> None
341349341357
in
341350-
begin try
341358+
begin
341351341359
(* Set name if there are no fields yet *)
341352-
Hashtbl.iter (fun _ _ -> raise Exit) hfields;
341353-
name := nm
341354-
with Exit ->
341355-
(* Unset it otherwise *)
341356-
name := None
341360+
if Hashtbl.length hfields <> 0 then name := None
341361+
else name := nm
341357341362
end;
341358341363
let fl = match expand_head env cty.ctyp_type, nm with
341359341364
{desc=Tvariant row}, _ when Btype.static_row row ->

ocaml

Submodule ocaml updated from 270ab07 to 92e58be

0 commit comments

Comments
 (0)