Skip to content

Commit c5f6e7a

Browse files
committed
Clean up Record_optional_labels
Clean up Record_optional_labels: determine whether a field is optional directly.
1 parent 29ba26a commit c5f6e7a

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

0 commit comments

Comments
 (0)