Skip to content

Commit 4c44e34

Browse files
committed
Clean up Record_optional_labels
Clean up Record_optional_labels: determine whether a field is optional directly.
1 parent a793751 commit 4c44e34

24 files changed

+154
-143
lines changed

analysis/src/CreateInterface.ml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ let printSignature ~extractor ~signature =
145145
let rec processSignature ~indent (signature : Types.signature) : unit =
146146
match signature with
147147
| Sig_type
148-
( propsId,
149-
{
150-
type_params;
151-
type_kind = Type_record (labelDecls, recordRepresentation);
152-
},
153-
_ )
148+
(propsId, {type_params; type_kind = Type_record (labelDecls, _)}, _)
154149
:: Sig_value (makeId (* make *), makeValueDesc)
155150
:: rest
156151
when Ident.name propsId = "props"
@@ -174,13 +169,9 @@ let printSignature ~extractor ~signature =
174169
labelDecl.ld_type
175170
in
176171
let lblName = labelDecl.ld_id |> Ident.name in
172+
let _ = 10 in
177173
let lbl =
178-
let optLbls =
179-
match recordRepresentation with
180-
| Record_optional_labels optLbls -> optLbls
181-
| _ -> []
182-
in
183-
if List.mem lblName optLbls then Asttypes.Optional lblName
174+
if labelDecl.ld_optional then Asttypes.Optional lblName
184175
else Labelled lblName
185176
in
186177
{retType with desc = Tarrow (lbl, propType, mkFunType rest, Cok)}

compiler/gentype/TranslateSignatureFromTypes.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ let translate_type_declaration_from_types ~config ~output_file_relative
1212
Log_.item "Translate Types.type_declaration %s\n" type_name;
1313
let declaration_kind =
1414
match type_kind with
15-
| Type_record (label_declarations, record_representation) ->
16-
TranslateTypeDeclarations.RecordDeclarationFromTypes
17-
(label_declarations, record_representation)
15+
| Type_record (label_declarations, _) ->
16+
TranslateTypeDeclarations.RecordDeclarationFromTypes label_declarations
1817
| Type_variant constructor_declarations
1918
when not
2019
(TranslateTypeDeclarations.has_some_gadt_leaf

compiler/gentype/TranslateTypeDeclarations.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
open GenTypeCommon
22

33
type declaration_kind =
4-
| RecordDeclarationFromTypes of
5-
Types.label_declaration list * Types.record_representation
4+
| RecordDeclarationFromTypes of Types.label_declaration list
65
| GeneralDeclaration of Typedtree.core_type option
76
| GeneralDeclarationFromTypes of Types.type_expr option
87
(** As the above, but from Types not Typedtree *)
@@ -86,16 +85,12 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
8685
in
8786
{CodeItem.import_types; export_from_type_declaration}
8887
in
89-
let translate_label_declarations ?(inline = false) ~record_representation
90-
label_declarations =
91-
let is_optional l =
92-
match record_representation with
93-
| Types.Record_optional_labels lbls -> List.mem l lbls
94-
| _ -> false
95-
in
88+
let translate_label_declarations ?(inline = false) label_declarations =
9689
let field_translations =
9790
label_declarations
98-
|> List.map (fun {Types.ld_id; ld_mutable; ld_type; ld_attributes} ->
91+
|> List.map
92+
(fun
93+
{Types.ld_id; ld_mutable; ld_optional; ld_type; ld_attributes} ->
9994
let name =
10095
rename_record_field ~attributes:ld_attributes
10196
~name:(ld_id |> Ident.name)
@@ -107,25 +102,32 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
107102
in
108103
( name,
109104
mutability,
105+
ld_optional,
110106
ld_type
111107
|> TranslateTypeExprFromTypes.translate_type_expr_from_types
112108
~config ~type_env,
113109
Annotation.doc_string_from_attrs ld_attributes ))
114110
in
115111
let dependencies =
116112
field_translations
117-
|> List.map (fun (_, _, {TranslateTypeExprFromTypes.dependencies}, _) ->
113+
|> List.map
114+
(fun (_, _, _, {TranslateTypeExprFromTypes.dependencies}, _) ->
118115
dependencies)
119116
|> List.concat
120117
in
121118
let fields =
122119
field_translations
123120
|> List.map
124121
(fun
125-
(name, mutable_, {TranslateTypeExprFromTypes.type_}, doc_string) ->
122+
( name,
123+
mutable_,
124+
optional_,
125+
{TranslateTypeExprFromTypes.type_},
126+
doc_string )
127+
->
126128
let optional, type1 =
127129
match type_ with
128-
| Option type1 when is_optional name -> (Optional, type1)
130+
| Option type1 when optional_ -> (Optional, type1)
129131
| _ -> (Mandatory, type_)
130132
in
131133
{mutable_; name_js = name; optional; type_ = type1; doc_string})
@@ -216,10 +218,9 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
216218
in
217219
{translation with type_} |> handle_general_declaration
218220
|> return_type_declaration
219-
| RecordDeclarationFromTypes (label_declarations, record_representation), None
220-
->
221+
| RecordDeclarationFromTypes label_declarations, None ->
221222
let {TranslateTypeExprFromTypes.dependencies; type_} =
222-
label_declarations |> translate_label_declarations ~record_representation
223+
label_declarations |> translate_label_declarations
223224
in
224225
let import_types =
225226
dependencies
@@ -250,8 +251,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
250251
| Cstr_record label_declarations ->
251252
[
252253
label_declarations
253-
|> translate_label_declarations ~inline:true
254-
~record_representation:Types.Record_regular;
254+
|> translate_label_declarations ~inline:true;
255255
]
256256
in
257257
let arg_types =
@@ -334,8 +334,8 @@ let translate_type_declaration ~config ~output_file_relative ~resolver ~type_env
334334
in
335335
let declaration_kind =
336336
match typ_type.type_kind with
337-
| Type_record (label_declarations, record_representation) ->
338-
RecordDeclarationFromTypes (label_declarations, record_representation)
337+
| Type_record (label_declarations, _) ->
338+
RecordDeclarationFromTypes label_declarations
339339
| Type_variant constructor_declarations ->
340340
VariantDeclarationFromTypes constructor_declarations
341341
| Type_abstract -> GeneralDeclaration typ_manifest

compiler/ml/ast_mapper_to0.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ let default_mapper =
468468
~mut:pld_mutable
469469
~loc:(this.location this pld_loc)
470470
~attrs:
471-
(Parsetree0.add_optional_attr ~optional:pld_optional (this.attributes this pld_attributes)));
471+
(Parsetree0.add_optional_attr ~optional:pld_optional
472+
(this.attributes this pld_attributes)));
472473
cases = (fun this l -> List.map (this.case this) l);
473474
case =
474475
(fun this {pc_lhs; pc_guard; pc_rhs} ->

compiler/ml/ctype.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3721,8 +3721,8 @@ let rec subtype_rec env trace t1 t2 cstrs =
37213721
(_, _, {type_kind = Type_record (fields2, repr2)}) ) ->
37223722
let same_repr =
37233723
match (repr1, repr2) with
3724-
| ( (Record_regular | Record_optional_labels _),
3725-
(Record_regular | Record_optional_labels _) ) ->
3724+
| ( (Record_regular | Record_optional_labels),
3725+
(Record_regular | Record_optional_labels) ) ->
37263726
true (* handled in the fields checks *)
37273727
| Record_unboxed b1, Record_unboxed b2 -> b1 = b2
37283728
| Record_inlined _, Record_inlined _ -> repr1 = repr2
@@ -3731,7 +3731,7 @@ let rec subtype_rec env trace t1 t2 cstrs =
37313731
in
37323732
if same_repr then
37333733
let violation, tl1, tl2 =
3734-
Record_coercion.check_record_fields ~repr1 ~repr2 fields1 fields2
3734+
Record_coercion.check_record_fields fields1 fields2
37353735
in
37363736
if violation then (trace, t1, t2, !univar_pairs) :: cstrs
37373737
else subtype_list env trace tl1 tl2 cstrs

compiler/ml/datarepr.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ let dummy_label =
232232
lbl_res = none;
233233
lbl_arg = none;
234234
lbl_mut = Immutable;
235+
lbl_optional = false;
235236
lbl_pos = -1;
236237
lbl_all = [||];
237238
lbl_repres = Record_regular;
@@ -251,6 +252,7 @@ let label_descrs ty_res lbls repres priv =
251252
lbl_res = ty_res;
252253
lbl_arg = l.ld_type;
253254
lbl_mut = l.ld_mutable;
255+
lbl_optional = l.ld_optional;
254256
lbl_pos = num;
255257
lbl_all = all_labels;
256258
lbl_repres = repres;

compiler/ml/includecore.ml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ type type_mismatch =
147147
| Variance
148148
| Field_type of Ident.t
149149
| Field_mutable of Ident.t
150+
| Field_optional of Ident.t
150151
| Field_arity of Ident.t
151152
| Field_names of int * string * string
152153
| Field_missing of bool * Ident.t
@@ -168,28 +169,17 @@ let report_type_mismatch0 first second decl ppf err =
168169
| Field_type s -> pr "The types for field %s are not equal" (Ident.name s)
169170
| Field_mutable s ->
170171
pr "The mutability of field %s is different" (Ident.name s)
172+
| Field_optional s ->
173+
pr "The optional attribute of field %s is different" (Ident.name s)
171174
| Field_arity s -> pr "The arities for field %s differ" (Ident.name s)
172175
| Field_names (n, name1, name2) ->
173176
pr "Fields number %i have different names, %s and %s" n name1 name2
174177
| Field_missing (b, s) ->
175178
pr "The field %s is only present in %s %s" (Ident.name s)
176179
(if b then second else first)
177180
decl
178-
| Record_representation (rep1, rep2) -> (
179-
let default () = pr "Their internal representations differ" in
180-
match (rep1, rep2) with
181-
| Record_optional_labels lbls1, Record_optional_labels lbls2 -> (
182-
let only_in_lhs =
183-
Ext_list.find_first lbls1 (fun l -> not (Ext_list.mem_string lbls2 l))
184-
in
185-
let only_in_rhs =
186-
Ext_list.find_first lbls2 (fun l -> not (Ext_list.mem_string lbls1 l))
187-
in
188-
match (only_in_lhs, only_in_rhs) with
189-
| Some l, _ -> pr "@optional label %s only in %s" l second
190-
| _, Some l -> pr "@optional label %s only in %s" l first
191-
| None, None -> default ())
192-
| _ -> default ())
181+
| Record_representation (_rep1, _rep2) ->
182+
pr "Their internal representations differ"
193183
| Unboxed_representation b ->
194184
pr "Their internal representations differ:@ %s %s %s"
195185
(if b then second else first)
@@ -280,6 +270,7 @@ and compare_records ~loc env params1_ params2_ n_
280270
if Ident.name ld1.ld_id <> Ident.name ld2.ld_id then
281271
[Field_names (n, ld1.ld_id.name, ld2.ld_id.name)]
282272
else if ld1.ld_mutable <> ld2.ld_mutable then [Field_mutable ld1.ld_id]
273+
else if ld1.ld_optional <> ld2.ld_optional then [Field_optional ld1.ld_id]
283274
else (
284275
Builtin_attributes.check_deprecated_mutable_inclusion ~def:ld1.ld_loc
285276
~use:ld2.ld_loc loc ld1.ld_attributes ld2.ld_attributes

compiler/ml/includecore.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type type_mismatch =
2929
| Variance
3030
| Field_type of Ident.t
3131
| Field_mutable of Ident.t
32+
| Field_optional of Ident.t
3233
| Field_arity of Ident.t
3334
| Field_names of int * string * string
3435
| Field_missing of bool * Ident.t

compiler/ml/matching.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ let make_record_matching loc all_labels def = function
15301530
let access =
15311531
match lbl.lbl_repres with
15321532
| Record_float_unused -> assert false
1533-
| Record_regular | Record_optional_labels _ ->
1533+
| Record_regular | Record_optional_labels ->
15341534
Lprim (Pfield (lbl.lbl_pos, Lambda.fld_record lbl), [arg], loc)
15351535
| Record_inlined _ ->
15361536
Lprim

compiler/ml/parmatch.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,24 +526,19 @@ let all_record_args lbls =
526526
in
527527
List.iter
528528
(fun ((id, lbl, pat) as x) ->
529-
let lbl_is_optional () =
530-
match lbl.lbl_repres with
531-
| Record_optional_labels labels -> List.mem lbl.lbl_name labels
532-
| _ -> false
533-
in
534529
let x =
535530
match pat.pat_desc with
536531
| Tpat_construct
537532
( {txt = Longident.Ldot (Longident.Lident "*predef*", "Some")},
538533
_,
539534
[({pat_desc = Tpat_constant _} as c)] )
540-
when lbl_is_optional () ->
535+
when lbl.lbl_optional ->
541536
(id, lbl, c)
542537
| Tpat_construct
543538
( {txt = Longident.Ldot (Longident.Lident "*predef*", "Some")},
544539
_,
545540
[({pat_desc = Tpat_construct (_, cd, _)} as pat_construct)] )
546-
when lbl_is_optional () -> (
541+
when lbl.lbl_optional -> (
547542
let cdecl =
548543
Ast_untagged_variants
549544
.constructor_declaration_from_constructor_description

compiler/ml/predef.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ let common_initial_env add_type add_extension empty_env =
316316
ld_type = newgenty (Tconstr (path_option, [tvar], ref Mnil));
317317
};
318318
],
319-
Record_optional_labels [Ident.name ident_dict_magic_field_name] );
319+
Record_optional_labels );
320320
}
321321
and decl_uncurried =
322322
let tvar1, tvar2 = (newgenvar (), newgenvar ()) in

compiler/ml/printtyped.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ let record_representation i ppf =
130130
function
131131
| Record_regular -> line i ppf "Record_regular\n"
132132
| Record_float_unused -> assert false
133-
| Record_optional_labels lbls ->
134-
line i ppf "Record_optional_labels %s\n" (lbls |> String.concat ", ")
133+
| Record_optional_labels -> line i ppf "Record_optional_labels\n"
135134
| Record_unboxed b -> line i ppf "Record_unboxed %b\n" b
136135
| Record_inlined {tag = i} -> line i ppf "Record_inlined %d\n" i
137136
| Record_extension -> line i ppf "Record_extension\n"

compiler/ml/rec_check.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ let rec expression : Env.env -> Typedtree.expression -> Use.t =
261261
match rep with
262262
| Record_unboxed _ -> fun x -> x
263263
| Record_float_unused -> assert false
264-
| Record_optional_labels _ | Record_regular | Record_inlined _
264+
| Record_optional_labels | Record_regular | Record_inlined _
265265
| Record_extension ->
266266
Use.guard
267267
in

compiler/ml/record_coercion.ml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
let check_record_fields ?repr1 ?repr2 (fields1 : Types.label_declaration list)
1+
let check_record_fields (fields1 : Types.label_declaration list)
22
(fields2 : Types.label_declaration list) =
3-
let field_is_optional id repr =
4-
match repr with
5-
| Some (Types.Record_optional_labels lbls) -> List.mem (Ident.name id) lbls
6-
| _ -> false
7-
in
83
let violation = ref false in
94
let label_decl_sub (acc1, acc2) (ld2 : Types.label_declaration) =
105
match
116
Ext_list.find_first fields1 (fun ld1 -> ld1.ld_id.name = ld2.ld_id.name)
127
with
138
| Some ld1 ->
14-
if field_is_optional ld1.ld_id repr1 <> field_is_optional ld2.ld_id repr2
15-
then (* optional field can't be modified *)
9+
if ld1.ld_optional <> ld2.ld_optional then
10+
(* optional field can't be modified *)
1611
violation := true;
1712
let get_as (({txt}, payload) : Parsetree.attribute) =
1813
if txt = "as" then Ast_payload.is_single_string payload else None

compiler/ml/translcore.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
923923
let targ = transl_exp arg in
924924
match lbl.lbl_repres with
925925
| Record_float_unused -> assert false
926-
| Record_regular | Record_optional_labels _ ->
926+
| Record_regular | Record_optional_labels ->
927927
Lprim (Pfield (lbl.lbl_pos, Lambda.fld_record lbl), [targ], e.exp_loc)
928928
| Record_inlined _ ->
929929
Lprim
@@ -938,7 +938,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
938938
let access =
939939
match lbl.lbl_repres with
940940
| Record_float_unused -> assert false
941-
| Record_regular | Record_optional_labels _ ->
941+
| Record_regular | Record_optional_labels ->
942942
Psetfield (lbl.lbl_pos, Lambda.fld_record_set lbl)
943943
| Record_inlined _ ->
944944
Psetfield (lbl.lbl_pos, Lambda.fld_record_inline_set lbl)
@@ -1171,7 +1171,7 @@ and transl_record loc env fields repres opt_init_expr =
11711171
|| size < 20
11721172
&&
11731173
match repres with
1174-
| Record_optional_labels _ -> false
1174+
| Record_optional_labels -> false
11751175
| _ -> true
11761176
(* TODO: More strategies
11771177
3 + 2 * List.length lbl_expr_list >= size (density)
@@ -1188,7 +1188,7 @@ and transl_record loc env fields repres opt_init_expr =
11881188
let access =
11891189
match repres with
11901190
| Record_float_unused -> assert false
1191-
| Record_regular | Record_optional_labels _ ->
1191+
| Record_regular | Record_optional_labels ->
11921192
Pfield (i, Lambda.fld_record lbl)
11931193
| Record_inlined _ -> Pfield (i, Lambda.fld_record_inline lbl)
11941194
| Record_unboxed _ -> assert false
@@ -1214,7 +1214,7 @@ and transl_record loc env fields repres opt_init_expr =
12141214
| Record_regular ->
12151215
Lconst
12161216
(Const_block (Lambda.blk_record fields mut Record_regular, cl))
1217-
| Record_optional_labels _ ->
1217+
| Record_optional_labels ->
12181218
Lconst
12191219
(Const_block (Lambda.blk_record fields mut Record_optional, cl))
12201220
| Record_inlined {tag; name; num_nonconsts; optional_labels; attrs} ->
@@ -1234,7 +1234,7 @@ and transl_record loc env fields repres opt_init_expr =
12341234
| Record_regular ->
12351235
Lprim
12361236
(Pmakeblock (Lambda.blk_record fields mut Record_regular), ll, loc)
1237-
| Record_optional_labels _ ->
1237+
| Record_optional_labels ->
12381238
Lprim
12391239
( Pmakeblock (Lambda.blk_record fields mut Record_optional),
12401240
ll,
@@ -1277,7 +1277,7 @@ and transl_record loc env fields repres opt_init_expr =
12771277
let upd =
12781278
match repres with
12791279
| Record_float_unused -> assert false
1280-
| Record_regular | Record_optional_labels _ ->
1280+
| Record_regular | Record_optional_labels ->
12811281
Psetfield (lbl.lbl_pos, Lambda.fld_record_set lbl)
12821282
| Record_inlined _ ->
12831283
Psetfield (lbl.lbl_pos, Lambda.fld_record_inline_set lbl)

0 commit comments

Comments
 (0)