Skip to content

Commit 7b979f3

Browse files
committed
share a few definitions
1 parent ea2aa81 commit 7b979f3

File tree

7 files changed

+27
-14
lines changed

7 files changed

+27
-14
lines changed

jscomp/build_tests/super_errors/expected/dict_coercion.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
We've found a bug for you!
33
/.../fixtures/dict_coercion.res:7:10-30
44

5-
5 │ type fakeDict<'t> = {anyOtherField?: 't}
5+
5 │ type fakeDict<'t> = {dictValuesType?: 't}
66
6 │
77
7 │ let d = (dict :> fakeDict<int>)
88
8 │

jscomp/build_tests/super_errors/expected/dict_pattern_inference_constrained.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
4 │ let _: dict<string> = dict
99
5 │ Js.log("one")
1010

11-
the following labels are not bound in this record pattern: anyOtherField
11+
the following labels are not bound in this record pattern: dictValuesType
1212
Either bind these labels explicitly or add ', _' to the pattern.
1313

1414

jscomp/build_tests/super_errors/fixtures/dict_coercion.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ let dict = Js.Dict.empty()
22
dict->Js.Dict.set("someKey1", 1)
33
dict->Js.Dict.set("someKey2", 2)
44

5-
type fakeDict<'t> = {anyOtherField?: 't}
5+
type fakeDict<'t> = {dictValuesType?: 't}
66

77
let d = (dict :> fakeDict<int>)

jscomp/build_tests/super_errors/fixtures/dict_magic_field_on_non_dict.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type fakeDict<'t> = {anyOtherField?: 't}
1+
type fakeDict<'t> = {dictValuesType?: 't}
22

33
let foo = (fakeDict: fakeDict<'a>) => {
44
switch fakeDict {

jscomp/ml/dict_type_helpers.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let dict_magic_field_name = "dictValuesType"
2+
13
let has_dict_pattern_attribute attrs =
24
attrs
35
|> List.find_opt (fun (({txt}, _) : Parsetree.attribute) ->
@@ -6,5 +8,11 @@ let has_dict_pattern_attribute attrs =
68

79
let has_dict_attribute attrs =
810
attrs
9-
|> List.find_opt (fun (({txt}, _) : Parsetree.attribute) -> txt = "res.dict")
10-
|> Option.is_some
11+
|> List.find_opt (fun (({txt}, _) : Parsetree.attribute) -> txt = "res.$dict")
12+
|> Option.is_some
13+
14+
let dict_attr : Parsetree.attribute =
15+
(Location.mknoloc "res.$dict", Parsetree.PStr [])
16+
17+
let dict_magic_field_attr : Parsetree.attribute =
18+
(Location.mknoloc "res.$dictMagicField", Parsetree.PStr [])

jscomp/ml/predef.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ and ident_failure = ident_create_predef_exn "Failure"
112112
and ident_ok = ident_create_predef_exn "Ok"
113113
and ident_error = ident_create_predef_exn "Error"
114114

115-
and ident_anyOtherField = ident_create "anyOtherField"
115+
and ident_dict_magic_field_name = ident_create Dict_type_helpers.dict_magic_field_name
116116

117117
and ident_js_error = ident_create_predef_exn "JsError"
118118
and ident_not_found = ident_create_predef_exn "Not_found"
@@ -220,19 +220,24 @@ let common_initial_env add_type add_extension empty_env =
220220
type_variance = [Variance.covariant; Variance.covariant]}
221221
and decl_dict =
222222
let tvar = newgenvar() in
223+
(* Dicts are implented as a as a single "magic" field record. This magic field
224+
is then leveraged to be able to piggy back on the existing record pattern
225+
matching mechanism. So, this definition is import for the dict pattern
226+
matching functionality, but not something intended to be exposed to the
227+
user. *)
223228
{decl_abstr with
224-
type_attributes = [(Location.mknoloc "res.dict", Parsetree.PStr [])];
229+
type_attributes = [Dict_type_helpers.dict_attr];
225230
type_params = [tvar];
226231
type_arity = 1;
227232
type_variance = [Variance.full];
228233
type_kind = Type_record ([{
229-
ld_id = ident_anyOtherField;
230-
ld_attributes = [(Location.mknoloc "res.optional", Parsetree.PStr []); (Location.mknoloc "res.dictMagicField", Parsetree.PStr [])];
234+
ld_id = ident_dict_magic_field_name;
235+
ld_attributes = [(Location.mknoloc "res.optional", Parsetree.PStr []); Dict_type_helpers.dict_magic_field_attr];
231236
ld_loc = Location.none;
232237
ld_mutable = Immutable; (* TODO(dict-pattern-matching) Should probably be mutable? *)
233238
ld_type = newgenty (Tconstr (path_option, [tvar], ref Mnil));
234239
}],
235-
Record_optional_labels [Ident.name ident_anyOtherField]);
240+
Record_optional_labels [Ident.name ident_dict_magic_field_name]);
236241
}
237242
and decl_uncurried =
238243
let tvar1, tvar2 = newgenvar(), newgenvar() in

jscomp/ml/typecore.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,12 @@ end) = struct
811811
Longident.Lident s_ -> begin
812812
let s =
813813
if List.exists (fun nd -> get_name nd = s_) descrs
814-
|| not (List.exists (fun nd -> get_name nd = "anyOtherField") descrs)
814+
|| not (List.exists (fun nd -> get_name nd = Dict_type_helpers.dict_magic_field_name) descrs)
815815
then s_
816-
else "anyOtherField" in
816+
else Dict_type_helpers.dict_magic_field_name in
817817
try
818818
let x = List.find (fun nd -> get_name nd = s) descrs in
819-
if s = "anyOtherField"
819+
if s = Dict_type_helpers.dict_magic_field_name
820820
then add_with_name x s_
821821
else x
822822
with Not_found ->

0 commit comments

Comments
 (0)